每次创建帐户时,私有createAccountID()
方法都应创建一个新的唯一 ID,即创建的第一个帐户的 ID 为“A1000”。每个新帐户的帐户 ID 将增加 1,即第二个帐户的 ID 为“A1001”等,并且不应有任何重复。
我不太确定如何启动这个方法,除了我创建了一个名为 counter 的静态变量,它设置为 0。我想我必须使用一种.equals()
方法或其他方法来确保第一个不等于新的否则它将打印一个错误消息,如 ("Duplicate")
我也不确定是去public void createAccountID()
还是
public String createAccountID()
这是代码:
public class Account
{
private Customer customer;
private String accountID;
private double balance;
private static int counter = 0;
private static final double OPENING_BALANCE = 0.0;
public Account()
{
setCustomer(new Customer());
setBalance(OPENING_BALANCE);
createAccountID();
}
public Account(Customer theCustomer, double theBalance)
{
setCustomer(theCustomer);
setBalance(theBalance);
createAccountID();
}
private void createAccountID()<------------------------------------------------
{
accountID = "A";
for(counter = 0; counter >= 0; ++counter){
accountID+=counter;//stuck in loop doesnt work.
//just thought id try it
}
}
public void setCustomer(Customer theCustomer)
{
if(theCustomer == null){
customer = new Customer();
}
else{
customer = theCustomer;
}
}
public Customer getCustomer()
{
return customer;
}
public void setBalance(double theBalance)
{
if(theBalance <= OPENING_BALANCE){
System.out.println("Error. No Negative values");
}
else{
balance = theBalance;
}
}
public double getBalance()
{
return balance;
}
}
任何帮助表示感谢干杯