2013-11-28 58 views
-2

问题是我需要为每个新客户创建新的增量ID并将其添加到Set,我试图用while循环来做,但它似乎不能是正确的创建唯一的增量ID并将其添加到集

public class Bank { 

    private String name; 

    private Set<Customer> customers = new HashSet<Customer>(); 


    private Set<AbstractAccount> accounts = new HashSet<AbstractAccount>(); 

    private Integer lastCustomerId = 0; 

    private Integer lastAccountId = 0; 

    public Integer addCustomer(String firstName, String lastName) { 
     // generate id from lastCustomerId and increment it 
     // add to Set 
     return lastCustomerId++; 
    } 

    public Integer addAccount(Integer ownerId, AccountType type) { 
     // add to Set 
    }  



} 
+0

哪里是你的'while'循环? –

+0

你已经声明了lastCustomerId和lastAccountId,我猜就是出于这个原因。所以用它们。添加新客户时,增加lastCustomerId并返回。添加新帐户时,请按照类似的方式进行。真的有什么问题? –

+0

一分钟 - 我会添加它 – user3047466

回答

0

林不知道,如果是你想要的...

public class Bank 
{ 

    private String name; 

    private Set<Customer> customers = new HashSet<Customer>(); 


    private Set<AbstractAccount> accounts = new HashSet<AbstractAccount>(); 


    private static int lastCustomerId = 0; 
    private static int lastAccountId = 0; 

    public static int GetNextCustomerID() 
    { 
     lastCustomerId++; 
     return lastCustomerId; 
    } 

    public static int GetNextAccountID() 
    { 
     lastAccountId++; 
     return lastAccountId; 
    } 

    public int addCustomer(String firstName, String lastName) 
    { 
     // generate id from lastCustomerId and increment it 
     int customerId = GetNextCustomerID(); 
     // add to Set 
    } 

    public int addAccount(int ownerId, AccountType type) 
    { 
     // add to Set 
     int accountId = GetNextAccountID(); 
    } 
} 
+0

是的,这就是我正在寻找的,只是应该添加返回customerId。然后我需要将它添加到集合中,但我尝试添加(新客户(customerId,firstName,lastName)); - 但它不起作用 – user3047466

相关问题