1

So I was wondering if I can use the same variable for different users added in the atm interface. In order for the code not to get too messy. If I can just store different people's data in to one variable

Here is the code

import java.util.Scanner;
public class ATM {

    public static void main(String []args){
        Scanner sc = new Scanner(System.in);

        Bank theBank = new Bank("B-Cash");

        //add a user
        User aUser = theBank.addUser("Carl", "Bustamante", "0605");
        User bUser = theBank.addUser("Justin", "Borns", "3213");

        Account newAccount = new Account("Checking", aUser, theBank);
        aUser.addAccount(newAccount);
        theBank.addAccount(newAccount);

        Account newAccount1 = new Account("Checking", bUser, theBank);
        bUser.addAccount(newAccount);
        theBank.addAccount(newAccount);


    }
}

1 Answers1

1

If I can just store different people's data in to one variable

You cannot reference multiple object(Users) to a single object variable.

User user = new User();

In this statement, the user object variable stores a reference to a new User object.

If you would like to store multiple Users objects, try:

class Bank {
    List<String> users;

    public List<String> getUsers() {
        return users;
    }

    public void setUsers(List<String> users) {
        this.users = users;
    }
}

How to use an array list in Java?

lkatiforis
  • 5,703
  • 2
  • 16
  • 35