user3392186
user3392186

Reputation: 23

polymorphic array overridding entries after taking in entries

i have a polymorphic array storing entries taken from a user. when the user is finished entering valid options they system.out.print the array to display the details stored. but these details all end up being the last entry in the array repeated.

public static void main(String args[]) {
    System.out.println("\nWelcome to AIB Bank");
    System.out.println("___________________________");

    for(i = 0;i<5;i++){
        OverDraft = false;
        //prompt user for inputs and validate entries
        System.out.println("\nUser " + (i+1));
        System.out.print("Please enter name: ");
        name = input.next();
        System.out.print("\nPlease Enter Age: ");
        strAge = input.next();
        Age = Validate(strAge, 1, null);
        System.out.print("\nPlease Enter Salary: ");
        strSalary = input.next();
        Salary = Validate(strSalary, 2, null);
        System.out.print("\nPlease Enter Account Number: ");
        strAccNum = input.next();
        AccNum = Validate(strAccNum, 3, null);
        System.out.print("\nPlease Enter Balance: ");
        strBalance = input.next();
        Balance = Validate(strBalance, 4, null);
        System.out.print("\nIs This Person A Premium Customer(y/n): ");
        strodCheck = input.next();
        ODCheck = Validate(null, 5, strodCheck);
        if(ODCheck == 0){
            OverDraft = false;
        }//end if for overdraft
        else{
            OverDraft = true;
        }//end else for overdraft

        if(OverDraft == true){
            System.out.print("\nPlease Enter OverDraft Limit: ");
            strODLimit = input.next();
            ODLimit = Validate(strODLimit, 6, null);
        }//end if for ODLimit

        AccNums[i] = AccNum;

        if(OverDraft == true){
            PersonalDetails p1 = new PersonalDetails(name, Age);
            CustomerArray[i] = new PremiumCustomer(Salary, AccNum, Balance, OverDraft, ODLimit, p1);
        }
        else{
            PersonalDetails p1 = new PersonalDetails(name, Age);
            CustomerArray[i] = new Customer(Salary, AccNum, Balance, p1);
        }

        System.out.println(CustomerArray[i].toString());

    }//end for loop

    for(i=0;i<5;i++){
        System.out.println("\n" + i + "\n" + AccNums[i]);
        System.out.println(CustomerArray[i]);
    }

    login();

}//end main method

the AccNum array is working fine and displays correct information.

Upvotes: 0

Views: 37

Answers (1)

Smutje
Smutje

Reputation: 18143

Your code looks like your Age, Salary etc. variables are all stored as references to the same variables in your array, so that every iteration of the for loop actually accesses the same references and therefore at the end you have five entries in the array which point at the same values in the memory and changing one actually changes all. Try to create local variables which are scoped to the for loop to populate the array.

Upvotes: 2

Related Questions