Hello,
I have written a program which includes this code
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.HashSet; /** * The purpose of this class is to be an * ArrayList manager. It helps manage the * ArrayLists and the HashSet and have * access to them and their data. */ public class ArrayListManager { private ArrayList<Admin> admin; private ArrayList<Manager> manager; private ArrayList<Employee> employee; private ArrayList<Keyboard> keyboard; private ArrayList<Mouse> mouse; private ArrayList<Printer> printer; private ArrayList<StorageUnit> storageUnit; private ArrayList<Order> order; private HashSet<String> mySet; /** * This is the constructor of the class ArrayListManager. * It creates eight different arraylists and one HashSet. */ public ArrayListManager() { admin = new ArrayList<Admin>(); manager = new ArrayList<Manager>(); employee = new ArrayList<Employee>(); keyboard = new ArrayList<Keyboard>(); mouse = new ArrayList<Mouse>(); printer = new ArrayList<Printer>(); storageUnit = new ArrayList<StorageUnit>(); order = new ArrayList<Order>(); mySet = new HashSet<String>(); } /** * This function returns an admin ArrayList. * @return The list with the administrators. */ public ArrayList getArrayAdmin() { return admin; } /** * This function returns a manager ArrayList. * @return The list with the managers. */ public ArrayList getArrayManager() { return manager; } /** * This function returns an employee ArrayList. * @return The list with the employees. */ public ArrayList getArrayEmployee() { return employee; } /** * This function returns the ArrayList keyboard. * @return The list with the keyboards. */ public ArrayList getArrayKeyboard() { return keyboard; } /** * This function returns the ArrayList mouse. * @return The list with the mice. */ public ArrayList getArrayMouse() { return mouse; } /** * This function returns the ArrayList printer. * @return The list with the printers. */ public ArrayList getArrayPrinter() { return printer; } /** * This function returns the ArrayList storageUnit. * @return The list with the storage units. */ public ArrayList getArrayStorageUnit() { return storageUnit; } /** * This function returns the ArrayList order. * @return The list with the orders. */ public ArrayList getArrayOrder() { return order; } /** * This functions gets the data from the files Admin.dat, Manager.dat, Employee.dat, * Keyboard.dat, Mouse.dat, Printer.dat, StorageUnit.dat, Order.dat and MySet.dat. * In this way, any changes that had been saved during a previous execution of the * programme are loaded again. */ public void getData() { FileInputStream fis = null; ObjectInputStream in = null; try { fis = new FileInputStream("Admin.dat"); in = new ObjectInputStream(fis); admin = (ArrayList) in.readObject(); in.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } try { fis = new FileInputStream("Manager.dat"); in = new ObjectInputStream(fis); manager = (ArrayList) in.readObject(); in.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } try { fis = new FileInputStream("Employee.dat"); in = new ObjectInputStream(fis); employee = (ArrayList) in.readObject(); in.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } try { fis = new FileInputStream("Keyboard.dat"); in = new ObjectInputStream(fis); keyboard = (ArrayList) in.readObject(); in.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } try { fis = new FileInputStream("Mouse.dat"); in = new ObjectInputStream(fis); mouse = (ArrayList) in.readObject(); in.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } try { fis = new FileInputStream("Printer.dat"); in = new ObjectInputStream(fis); printer = (ArrayList) in.readObject(); in.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } try { fis = new FileInputStream("StorageUnit.dat"); in = new ObjectInputStream(fis); storageUnit = (ArrayList) in.readObject(); in.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } try { fis = new FileInputStream("Order.dat"); in = new ObjectInputStream(fis); order = (ArrayList) in.readObject(); in.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } } }
Instead of having this code reiteration at getData function, I would like to write a function getData(ArrayList array, String filename).
public void getData(ArrayList array, String filename) { FileInputStream fis = null; ObjectInputStream in = null; try { fis = new FileInputStream(filename); in = new ObjectInputStream(fis); array = (ArrayList) in.readObject(); in.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); }
The problem with this function is when I try to load the data form the files to the arraylists.
At my main class I call this function by writting
ArrayListManager list = new ArrayListManager(); list.getData(list.getArrayAdmin(), "Admin.dat"); list.getData(list.getArrayManager(), "Manager.dat"); .... list.getData(list.getArrayStorageUnit(), "StorageUnit.dat");
The problem is that when I print the size of the arraylists using the function list.getArrayAdmin().size(), it is 0 which shows that the data is not loaded.
I would appreciate your help.