Reputation: 13
So I have 3 different objects: Dentist, Patient and Appointment.
The Dentist and Patient each have an ArrayList so that they share the same appointments. Here comes the problem though. When I serialize the Dentist and Patient into a textfile and then dezerialize them again, changes that are made in the appointment is only updated in the Patient appointment?
I have the following code which creates an appointment:
Appointment a = new Appointment (p, d, 20.0, startDate, endDate, s);
p.addAppointment(a);
d.addAppointment(a);
patients.add(p);
dentists.add(d);
ReadAndWrite.writePatients(patients, "patients.txt");
ReadAndWrite.writeDentists(dentists, "dentists.txt");
It's the same freaking Appointment object - why are the changes made not updated in both???
EDIT: I have below added the 3 classes (And the user class which patient and dentist inherits from:
User:
public class User implements Serializable {
protected String firstName;
protected String lastName;
protected String address;
protected LocalDate dateOfBirth;
protected int phoneNumber;
protected long cpr;
protected int postCode;
protected String username;
protected String password;
protected boolean loggedIn;
protected ArrayList<Appointment> appointments = new ArrayList<Appointment>();
public User (String firstName, String lastName, String address, LocalDate dateOfBirth, int phoneNumber, int cpr, int postCode)
{
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.dateOfBirth = dateOfBirth;
this.phoneNumber = phoneNumber;
this.cpr = cpr;
this.postCode = postCode;
this.username = createUsername(firstName, lastName);
this.password = createPassword(lastName, cpr);
}
public User () { }
public static String createUsername(String firstName, String lastName)
{
if(!(firstName.length() == 0 || lastName.length() < 3))
{
String first = firstName.substring(0, 1);
String last = lastName.substring(0, 3);
return first + last;
}
return null;
}
public static String createPassword(String lastName, long l)
{
if(!(lastName.length() < 3))
{
String last = lastName.substring(0 , 3);
String pass = Long.toString(l);
pass = pass.substring(pass.length() - 4, pass.length());
return last + pass;
}
return null;
}
public static boolean isPostCodeValid(String s)
{
String n = ".*[0-9].*";
if(s.matches(n) && s.length() == 4)
{
int temp = Integer.parseInt(s);
if (temp <= 9999)
{
return true;
}
return false;
}
return false;
}
public static boolean isCPRValid(String s)
{
if (!s.contains("-"))
{
return false;
}
String[] parts = s.split("-");
String ddmmyy = parts[0];
String security = parts[1];
if (!(ddmmyy.length() != 6 || security.length() != 4))
{
String n = ".*[0-9].*";
if(ddmmyy.matches(n) && security.matches(n))
{
return true;
}
}
return false;
}
public static boolean isPhoneNumberValid(String s)
{
if (s.length() != 8)
{
return false;
}
String n = ".*[0-9].*";
return (s.matches(n));
}
public static boolean isNameValid(String s)
{
if(s.isEmpty())
{
return false;
}
String n = ".*[0-9].*";
return !(s.matches(n));
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public int getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(int phoneNumber) {
this.phoneNumber = phoneNumber;
}
public long getCpr() {
return cpr;
}
public void setCpr(Long cpr) {
this.cpr = cpr;
}
public int getPostCode() {
return postCode;
}
public void setPostCode(int postCode) {
this.postCode = postCode;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isLoggedIn() {
return loggedIn;
}
public void setLoggedIn(boolean loggedIn) {
this.loggedIn = loggedIn;
}
public ArrayList<Appointment> getAppointments() {
return appointments;
}
public void addAppointment (Appointment a)
{
appointments.add(a);
}`
DENTIST
public class Dentist extends User {
static AtomicInteger nextId = new AtomicInteger();
private int id;
protected ArrayList<LocalDateTime> availabilities = new ArrayList<LocalDateTime>();
protected ArrayList<Service> services = new ArrayList<Service>();
protected String clinicName;
public Dentist(String firstName, String lastName, String address, LocalDate dateOfBirth, int phoneNumber, int cpr, int postCode, String clinicName) {
super(firstName, lastName, address, dateOfBirth, phoneNumber, cpr, postCode);
this.clinicName = clinicName;
id = nextId.incrementAndGet();
}
public Dentist()
{
id = nextId.incrementAndGet();
}
public int getId() {
return id;
}
public ArrayList<LocalDateTime> getAvailabilities() {
return availabilities;
}
public void setAvailabilities(ArrayList<LocalDateTime> availabilities) {
this.availabilities = availabilities;
}
public void addAvailabilities(LocalDateTime date)
{
this.availabilities.add(date);
}
public ArrayList<Service> getServices() {
return services;
}
public void setServices(ArrayList<Service> services) {
this.services = services;
}
public void addService (Service s)
{
services.add(s);
}
public String getClinicName() {
return clinicName;
}
public void setClinicName(String clinicName) {
this.clinicName = clinicName;
}
public static void setNextId(AtomicInteger nextId) {
Dentist.nextId = nextId;
}
Patient:
public class Patient extends User {
static AtomicInteger nextId = new AtomicInteger();
private int id;
private CreditCard creditCard;
public Patient(String firstName, String lastName, String address, LocalDate dateOfBirth, int phoneNumber, int cpr, int postCode) {
super(firstName, lastName, address, dateOfBirth, phoneNumber, cpr, postCode);
this.creditCard = null;
id = nextId.incrementAndGet();
}
public Patient ()
{
id = nextId.incrementAndGet();
}
public CreditCard getCreditCard() {
return creditCard;
}
public void setCreditCard(CreditCard creditCard) {
this.creditCard = creditCard;
}
public int getId() {
return id;
}
public static void setNextId(AtomicInteger nextId) {
Patient.nextId = nextId;
}
Appointment:
public class Appointment implements Serializable {
private double price;
private Patient patient;
private Dentist dentist;
private Service service;
private LocalDateTime startTime;
private LocalDateTime endTime;
static AtomicInteger nextId = new AtomicInteger();
private int id;
public Appointment(Patient patient, Dentist dentist, double price, LocalDateTime startTime, LocalDateTime endTime, Service service)
{
this.patient = patient;
this.dentist = dentist;
this.price = price;
this.startTime = startTime;
this.endTime = endTime;
this.service = service;
id = nextId.incrementAndGet();
}
public Appointment()
{
id = nextId.incrementAndGet();
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Patient getPatient() {
return patient;
}
public void setPatient(Patient patient) {
this.patient = patient;
}
public Dentist getDentist() {
return dentist;
}
public void setDentist(Dentist dentist) {
this.dentist = dentist;
}
public LocalDateTime getStartTime() {
return startTime;
}
public void setStartTime(LocalDateTime startTime) {
this.startTime = startTime;
}
public LocalDateTime getEndTime() {
return endTime;
}
public void setEndTime(LocalDateTime endTime) {
this.endTime = endTime;
}
public int getId() {
return id;
}
public String toString()
{
return getService().getName() + " -------" + "aID: " + getId() + " " + getStartTime() + " with PATIENT: " + patient.getFirstName() +" " + patient.getLastName() + " and DENTIST: " + dentist.getFirstName() + " " + dentist.getLastName() + " at " + dentist.getAddress();
}
public Service getService() {
return service;
}
public void setService(Service service) {
this.service = service;
}
public static void setNextId(AtomicInteger nextId) {
Appointment.nextId = nextId;
}
Serialization code:
public static void writePatients(ArrayList<Patient> list, String file)
{
try {
FileOutputStream f = new FileOutputStream(new File(file));
ObjectOutputStream o = new ObjectOutputStream(f);
o.writeObject(list);
o.close();
f.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream");
}
}
Upvotes: 1
Views: 79
Reputation: 140523
You have to change your implementation to do (de)serialization for all objects at once. For example by providing/implementing a
public class PersistenceService {
public void serialize(String fname, List<Serializable> outgoing) {...
public List<Serializable> deserialize(String fname) {
You already have most of the code to implement this class; and on top of that, you simply have to:
instanceof
to determine for each entry if it is a Patient, Dentist, whatsoever ...)Upvotes: 1
Reputation: 3985
Your code serializes patients
, and all its referenced objects into a file, and then, separately, serializes dentists
and all referenced objects into another file.
When deserializing the second file, it has no way of knowing that the objects which represent these appointments already exist on the heap (from previous deserialization or any other way). It just deserializes the object and all its dependencies from the file, and by this it creates a copy of the appointments.
So appointments
referenced by destists
and those referenced by patients
are two different instances on the heap.
The way to solve this is to serialize one object which references both lists into one file, something like
Data myData = new Data(patients, dentists) // object which just holds reference to both lists, nothing more
ReadAndWrite.writeData(myData, "allData.txt");
Upvotes: 0