I cannot seem to get my saved accounts stored inside my ArrayList. I think the latest one overwrites the oldest one, but I need them all to stack up in the ArrayList.
When I run the program, I create (add to ArrayList) a new Account, say Greg. Then I go on using that account and later on, add another account, say Bob. Then I click on "search for account" and type in Greg, it doesn't find it, but it finds Bob. HOWEVER, if I search for Greg BEFORE I create Bob, it FINDS IT!
Here are the relevant parts of my code:
////////// DECLARATIONS /////////
public static ArrayList dataStore;
public static int index;
dataStore = new ArrayList(10);
index = 0;
/////////////////////////////// ADD NEW ACCOUNT ////////////////////////////////
else if(source.equals("Add new account")) {
//************* Output
accountName = JOptionPane.showInputDialog("Enter the account name: ");
account.setName(accountName);
String strInitialBalance = JOptionPane.showInputDialog("Enter your initial
balance: ");
initialBalance = Double.parseDouble(strInitialBalance);
account.setBalance(initialBalance);
account = new CheckingAccount(initialBalance, accountName);
dataStore.add(index++, account);
}
//////////////////////////////// FIND ACCOUNT /////////////////////////////////
else if(source.equals("Find an account")) {
String str, name;
str = JOptionPane.showInputDialog("Enter the Account name: ");
for (int index = 0; index < dataStore.size(); index++) {
Account datum = (Account)dataStore.get(index);
if (str.equals(datum.getName())) {
name = datum.getName();
textArea.setText("Found Account for " + name);
}
else
textArea.setText("No Accounts found!");
} // for
}
This "account" object they are referring to is a class I have where all the data gets stored. Here is its header:
public class CheckingAccount extends Account implements Serializable {
// ...
// Methods
// ...
}