2013-11-28 84 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

相關問題