开发者

modifying fields of an object

I am trying to create code that will change the firstname and lastname fields of a customer object that is referenced by multiple accounts. i.e. each customer can have more that one account. However, it appears that the names are being changed only for one account and the change is not appearing in the other accounts that are connected to the same customer. Perhaps someone can pinpoint the error.

see code below:

excerpt from the main method

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{
                    Sy开发者_运维技巧stem.out.println("Account not found");
                }
            }
            catch(Exception e){
                System.out.println("Unable to process request.\n" + e.getMessage());
            }

applicable bank class methods:

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");

    }

applicable account class methods

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);
    }
}

applicable customer class methods

private Customer(Customer c){
    //copy constructor
    this.customerNumber=c.customerNumber;
    this.socialSecurityNo=c.socialSecurityNo;
    this.firstName=c.firstName;
    this.lastName=c.lastName;
}


It looks like your code shouldn't work at all, because you clone the Customer object, then modify the clone. The same goes for accounts.

Some simplification might help with debugging. Much of this code can probably be simplified using the standard collections, rather than iterating and using compareTo.

The logic is also a little strange - if the task is to modify the customer details, then why start with an account number and then go to the customer?

if (getAccount(accountNumber)!=null){
        accounts.get(accounts.indexOf(getAccount(accountNumber))).getCustomer().modifyName(firstName, lastName);
    }
    else{
        throw new Exception("Account not found");
    }

could be simplified to something like:

getAccount(accountNumber).getCustomer().setName(firstName, lastName);

if getAccount and getCustomer threw an exception if the item wasn't found.

The getAccount method could be reduced to something like:

public Account getAccount(long accountNumber )  {
        return accounts.get(accountNumber )
    }

(barely worth having a method for!) if accounts was a Map<Long,Account>


Since you use .clone in the getter of the account and mainly the customer you do get only a copy of the customer and change the names there. Since String is an immutable object you replace the names in the copy and only there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜