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

First of all static methods are called this way: BankAccount.setName(accountName)
I hope that your methods are not static.

Second with this code, you create 2 new instances, when you need 1. You will need to have 1 instance and modify its values.

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

        }

Second of all the set methods are supposed to be void and this should be wrong:
BankAccount ba1 = BankAccount.setName(accountName)

This is the right way:

void setName(String accountName) {
this.accountName = accountName;
}

Now of with the code:

With this code you create new instances: ba1, and you do nothing with them. Perhaps you should add them to an ArrayList as you progress:

ArrayList list=new ArrayList();

for(i = 1; i < totalAccounts; i++) {


	accountName = JOptionPane.showInputDialog("What is the name for the this account?: ");

//You don't need to create this instance and then create again a new one	
//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);

list.add(ba1);
        }

Now the list will have each bank account. If I understood correct, you want to loop through and modify them. Then do this:

for(i = 0; i < list.size(); i++) {


	accountName = JOptionPane.showInputDialog("What is the name of the "+i+" account?: ");

	deposit = JOptionPane.showInputDialog("What is the balance for the "+i+" account?: ");
	BankAccount ba1 = (BankAccount)list.get(i);
        ba1.setAccountName(accountName );
ba1.setDeposit(deposit ); 
        }

Edit: PS: Sorry about the double posting. I must have pressed something accidentally on my keyboard. Read the 3rd post

First of all static methods are called this way: BankAccount.setName(accountName)
I hope that your methods are not static.

Second with this code, you create 2 new instances, when you need 1. You will need to have 1 instance and modify its values.

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

        }

Second of all the set methods are supposed to be void and this should be wrong:
BankAccount ba1 = BankAccount.setName(accountName)

This is the right way:

void setName(String accountName) {
this.accountName = accountName;
}

Now of with the code:

With this code you create new instances: ba1, and you do nothing with them. Perhaps you should add them to an ArrayList as you progress:

ArrayList list=new ArrayList();

for(i = 1; i < totalAccounts; i++) {


	accountName = JOptionPane.showInputDialog("What is the name for the this account?: ");

//You don't need to create this instance and then create again a new one	
//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);

list.add(ba1);
        }

Now the list will have each bank account. If I understood correct, you want to loop through and modify them. Then do this:

for(i = 0; i < list.size(); i++) {


	accountName = JOptionPane.showInputDialog("What is the name of the "+i+" account?: ");

	deposit = JOptionPane.showInputDialog("What is the balance for the "+i+" account?: ");
	BankAccount ba1 = (BankAccount)list.get(i);
        ba1.setAccountName(accountName );
        ba1.setDeposit(deposit ); 

//OR
((BankAccount)list.get(i) ).setAccountName(accountName );
((BankAccount)list.get(i) ).setDeposit(deposit ); 
        }

You don't need to add again anything to the list or set, because when you do a get() from the list any changes done to the object you get from the list will be applied to the object in the list, since they are the same instance in the memory


Edit: PS: Sorry about the double posting. I must have pressed something accidentally on my keyboard. Read the 3rd post

Many Thanks, javaAddict! Thats exactly the guidance I was looking for.

Thanks again and have a great week!

... Tyster

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.