-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
}
}
哪里是你的'while'循环? –
你已经声明了lastCustomerId和lastAccountId,我猜就是出于这个原因。所以用它们。添加新客户时,增加lastCustomerId并返回。添加新帐户时,请按照类似的方式进行。真的有什么问题? –
一分钟 - 我会添加它 – user3047466