I have two classes that interact:
Shelf, that stores CDs, DVDs, PaperMedias:
public class Shelf {
private ArrayList<CD> cds;
private ArrayList<DVD> dvds;
private ArrayList<PaperMedia> paperMedias;
...etc
And Customer :
public class Customer implements Serializable {
private Map<PhysicalMedia, Calendar> mediaOwned;
private Map<PhysicalMedia,Calendar> mediaReturned;
private Map<PhysicalMedia,CalendarPeriod> mediaOnHold;
...etc
Physical media is a parent of CD,DVD,PaperMedia.
First I will initialize shelf with some items, and customer to have a few of these items borrowed. Then I save these objects to ShelfObjects.txt and CustomerObjects.txt.
When I read these objects once again from these files, it seems like link between these two is lost , specifically between PhysicalMedia of customer and PhysicalMedia of shelf. These should definitely have the same reference for example Metallica CD should have same reference in Shelf as in Customer's account.
So when i change say a status of Metallica CD, it wont update it in the other source!
Is there a was to preserve this reference ?
I load and save media in the following way in CustomerDatabase class:
public class CustomersDatabase implements Serializable {
private ArrayList<Customer> customers = new ArrayList<Customer>();
//etc..
public void load() {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("CustomersObject.txt"));
try {
customers.clear();
while(true) {
customers.add((Customer)in.readObject());
}
} catch (ClassNotFoundException e) {
System.out.println("Customer class in file wasn't found");
}
in.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
e.printStackTrace();
} catch (IOException e) {
System.out.println("\n^ Customer load successful\n");
}
public void save() {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("CustomersObject.txt",false));
for (int i=0;i<customers.size();i++) {
out.writeObject(customers.get(i));
}
out.close();
System.out.println("\nCustomer save successful\n");
} catch (FileNotFoundException e) {
System.out.println("File not found");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IO exception ");
e.printStackTrace();
}
}
I do similar load and store in Shelf class.