所以我有3个不同的对象:牙医,患者和预约。
牙医和患者每人都有一个ArrayList,以便他们共享相同的约会。不过这是问题所在。当我将牙医和患者序列化为文本文件然后再次对其进行去官方化时,约会中所做的更改只会在患者预约中更新?
我有以下代码创建约会:
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");
同样令人讨厌的约会对象 - 为什么更改都没有更新?
编辑:我在下面添加了3个类(以及患者和牙医继承自的用户类:
用户:
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;
}
病人:
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;
}
预约:
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;
}
序列化代码:
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");
}
}
答案 0 :(得分:1)
您必须将实施更改为一次的所有对象的(de)序列化。例如,通过提供/实施
public class PersistenceService {
public void serialize(String fname, List<Serializable> outgoing) {...
public List<Serializable> deserialize(String fname) {
您已经拥有了实现此类的大部分代码;最重要的是,你只需要:
instanceof
确定每个条目,如果它是患者,牙医,无论如何......)< / LI>
答案 1 :(得分:0)
您的代码将patients
及其所有引用的对象序列化为一个文件,然后单独将dentists
和所有引用的对象序列化为另一个文件。
在反序列化第二个文件时,它无法知道表示这些约会的对象已经存在于堆上(从之前的反序列化或任何其他方式)。它只是从文件中反序列化对象及其所有依赖项,并由此创建约会的副本。
appointments
引用的destists
和patients
引用的Data myData = new Data(patients, dentists) // object which just holds reference to both lists, nothing more
ReadAndWrite.writeData(myData, "allData.txt");
是堆上的两个不同实例。
解决此问题的方法是序列化一个对象,该对象将两个列表引用到一个文件中,例如
{{1}}