-2

I've pair the code down to the methods I am having a problem, with. It 'seems' to work until I try to load the file again, and it comes up with nothing in it. (I have not fully understood how to clear the ArrayList before performing the 2nd load, but that is for later).

I am sorry if this is hidden somewhere under some other nomenclature I also have not learned yet, but this is a project that is due tomorrow and I am at my wit's end.

import java.util.*;
import java.io.*;

public class MainATM3 {

  public static ArrayList<ClientAccount> accounts = new ArrayList<ClientAccount>();
  public static ClientAccount editBankAccount = new ClientAccount("placeholder",1234,1);;

  public static void main(String[] args) {

// Create ATM account ArrayList
    ArrayList<ClientAccount> accounts = new ArrayList<ClientAccount>();
// Get Account data from files
initialLoadATMAccounts(accounts);
System.out.println("Loaded "+accounts.size());

System.out.println("before Array "+(accounts.size()));
    accounts.add(0,new ClientAccount("Jess",500,1830));
    accounts.add(1,new ClientAccount("Mary",1111.11,7890));
System.out.println("after Array "+(accounts.size()));

saveATMAccounts(accounts);  
System.out.println("saved "+(accounts.size()));

initialLoadATMAccounts(accounts);
System.out.println("Loaded "+accounts.size());

  System.out.println("Logged Out");

  }       

// Save ArrayList of ATM Objects //call by: saveATMAccounts(accounts); 
  public static void saveATMAccounts(ArrayList<ClientAccount> saveAccounts) {
    FileOutputStream fout = null;
    ObjectOutputStream oos = null;
    try{  
      fout=new FileOutputStream("ATMAccounts.sav");
      oos = new ObjectOutputStream(fout);
      oos.writeObject(accounts);
      System.out.println("objects written "+(accounts.size()));
      } catch (Exception ex) {
      ex.printStackTrace();
      } finally {
        if (fout != null) {
          try {
            fout.close();
          } catch (IOException e) {
            e.printStackTrace();
            }
          }
        if (oos != null) {
          try {
            oos.close();
          } catch (IOException e) {
          e.printStackTrace();
            }
        }
     }
  }


  // INITIAL Load ArrayList of ATM Objects //call by: initialLoadATMAccounts(accounts); 
  public static void initialLoadATMAccounts(ArrayList<ClientAccount> loadAccounts){
    FileInputStream fIS = null;
    ObjectInputStream oIS = null;
    try{  
      fIS=new FileInputStream("ATMAccounts.sav");
      oIS = new ObjectInputStream(fIS);
      ArrayList<ClientAccount> loadAccounts = (ArrayList<ClientAccount>) oIS.readObject();
      oIS.close();
      fIS.close(); 
    }
    catch(Exception exc){
      exc.printStackTrace(); 
    }
  }
}


import java.io.Serializable;
public class ClientAccount implements Serializable {

    public String accountName;
    public double accountBalance;
    public int accountPIN;

    public ClientAccount(String accountName, double accountBalance, int accountPIN){
      this.accountName=accountName;
      this.accountBalance=accountBalance;
      this.accountPIN=accountPIN;
    }

// Account Name Methods    
    public String getAccountName() {
      return accountName;
    }
    public void setAccountName(String name) {
      accountName = name;
    }

// Account Balance Methods
    public double getAccountBalance() {
      return accountBalance;
    }
    public void setAccountBalance(double balance) {
      accountBalance = balance;
    }

// PIN Methods
    public int getAccountPIN() {
      return accountPIN;
    }
    public void setAccountPIN(int newPIN) {
      accountPIN = newPIN;
    }

}
  • I only se initial load. Where do you reload from the main? – MordechayS Dec 05 '16 at 16:47
  • What are you trying to do in `initialLoadATMAccounts(ArrayList loadAccounts)` and why are you not using `loadAccounts` parameter in your method anywhere? If you are not using it then why your method has this parameter? Something tells me that you may need to read [Is Java “pass-by-reference” or “pass-by-value”?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – Pshemo Dec 05 '16 at 16:49
  • You should use [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html). Also, you probably want your loading methods to return an `ArrayList`. – 4castle Dec 05 '16 at 16:54

1 Answers1

0

Instead of passing the desired array to initialLoadATMAccounts as param you should return the new, loaded array:

public static List<ClientAccount> initialLoadATMAccounts(){
    FileInputStream fIS = null;
    ObjectInputStream oIS = null;
    try{  
      fIS=new FileInputStream("ATMAccounts.sav");
      oIS = new ObjectInputStream(fIS);
      ArrayList<ClientAccount> loadAccounts = (ArrayList<ClientAccount>)     oIS.readObject();
      oIS.close();
      fIS.close(); 
      return loadAccounts;
    }
    catch(Exception exc){
      exc.printStackTrace(); 
    }
}

BTW: A IDE like eclipse would have issued a warning where you overwrite the param loadAccounts.

Heri
  • 4,368
  • 1
  • 31
  • 51