I have a problem that I've been poking at for 2 weeks and I'm stumped.
The basic problem is this: If you have an array of objects (an ArrayList in this case), and each of these objects has more than one instance variable (3 variables in this case), how the heck can you use a for loop to update the instance variables, one object after another? For example...
I have an array of bank accounts. Each account has a name, an account ID number and a balance. I'm trying to loop through the accounts, first updating the name, then updating the ID number, then the balance. Then I move on to the next object in the array.
I've tried doing this several ways, first by updating the names of each account, then the other variables, but that doesn't seem to be working. I think the easiest approach would be to update all 3 variables for each account, then move on to the next object in the array. Here's what I'm having trouble with...
I have a BankAccount class and a BankAccountTester class. The BankAccount class has the following constructor...
//Instance variables.
private int accountId;
private double balance;
private String name;
/**
* @param accountId
* @param balance
* @param name
*/
public BankAccount2(int accountId, double balance, String name) {
super();
this.accountId = accountId;
this.balance = balance;
this.name = name;
... then a series of setters and getters for each parameter, such as...
setName()
getName()
setBalance()
getBalance()
setaccountId()
getaccountId()
//
I'm stumped on the tester class. I can't get it to work. Here is my latest attempt...
NOTE that the account ID (the first argument) increments automatically so I'm not too worried about that part.
for(i = 1; i < totalAccounts; i++) {
accountName = JOptionPane.showInputDialog("What is the name for the this account?: ");
BankAccount ba1 = new BankAccount(i, 0, accountName);
deposit = JOptionPane.showInputDialog("What is the balance for the this account?: ");
BankAccount ba1 = new BankAccount(i, deposit, accountName);
}
How do I update the balance without messing with the other variables, like accountName? Should I instead be doing...
accountName = JOptionPane.showInputDialog("What is the name for the this account?: ");
BankAccount ba1 = BankAccount.setName(accountName);
deposit = JOptionPane.showInputDialog("What is the balance for the this account?: ");
BankAccount ba1 = BankAccount.setDeposit(deposit);
One problem I have with this approach is that I don't know ahead of time how many accounts will be needed (hence the ArrayList). So while I am using reference 'ba1' above I don't know how many total I'll need.
Any advice anyone can suggest is greatly appreaciated. Many, many thanks!
...Tyster