我試圖創建代碼來更改多個帳戶引用的客戶對象的名字和姓氏字段。即每個客戶可以有更多的那個賬戶。但是,看起來名稱僅針對一個帳戶進行了更改,並且更改未顯示在連接到同一客戶的其他帳戶中。也許有人可以指出錯誤。修改對象的字段
見下面的代碼:從主方法
摘錄
System.out.println("Enter the number of the account that you would like to modify:");
number=keyboard.nextLong();
keyboard.nextLine();
firstName=null;
lastName=null;
try{
if(aBank.getAccount(number)!=null){
System.out.println("Account information is listed below");
System.out.println(aBank.getAccount(number).toString());
System.out.println("Modify first name y or n");
answer=keyboard.nextLine();
if(answer.equals("Y")||answer.equals("y")){
System.out.println("Enter first name:");
firstName=keyboard.nextLine();
}
System.out.println("Modify last name y or n");
answer=keyboard.nextLine();
if(answer.equals("Y")|| answer.equals("y")){
System.out.println("Enter last name:");
lastName=keyboard.nextLine();
}
aBank.changeName(number,firstName,lastName);
}
else{
System.out.println("Account not found");
}
}
catch(Exception e){
System.out.println("Unable to process request.\n" + e.getMessage());
}
適用的銀行類方法:
public Account getAccount(long accountNumber) throws Exception {
boolean found=false;
for(int i=0;i<accounts.size();i++){
if(accounts.get(i).getAccountNumber().compareTo(accountNumber)==0){
found=true;
return accounts.get(i).clone();
}
}
public void changeName(Long accountNumber, String firstName, String lastName) throws Exception{
if (getAccount(accountNumber)!=null){
accounts.get(accounts.indexOf(getAccount(accountNumber))).getCustomer().modifyName(firstName, lastName);
}
else{
throw new Exception("Account not found");
}
適用帳戶類方法
private Account (Account a){
//copy constructor
this.accountNumber=a.accountNumber;
this.startBalance=a.startBalance;
this.customer=a.customer;
this.trans=a.trans;
}
public Customer getCustomer() {
return this.customer.clone();
}
public void modifyName(String firstName, String lastName){
if(firstName!=null){
customer.setFirstName(firstName);
}
if(lastName!=null){
customer.setLastName(lastName);
}
}
應用licable客戶類方法
private Customer(Customer c){
//copy constructor
this.customerNumber=c.customerNumber;
this.socialSecurityNo=c.socialSecurityNo;
this.firstName=c.firstName;
this.lastName=c.lastName;
}
Customer對象如何與Account對象關聯?你確定對於每個邏輯客戶,你只有一個客戶類的一個對象嗎? – Olaf 2011-06-15 19:47:28