I need to make a seating reservation system for my assignment and I have to manage .dat files. I have to store all Student objects in a file. When a student is registered, a new object is created and I need to add it to the archive. But as far as I know, you can not edit a serializable file in a "direct" form, so what I am trying to do is to generate an array of objects (temporary) with all the objects from the file, enter the new object registered and copy each object from the array to the file again.
I have the methods to do that but I don't know where to call the methods to perform the operations. Also I don't know in which class do I have to create the ArrayList.
Here is my code, I just put the classes relevant to my question:
import java.io.*;
import java.util.*;
class Estudiante extends Persona implements Serializable {
private String idNo;
private byte gradEst;
private String codeSeat;
public Estudiante(String name, char sex, int age, String location, String idNo, byte gradEst, String codeSeat){
super(name, sex, age, location);
setIdNo(idNo);
setGrade(gradEst);
setCodeSeat(codeSeat);
}
public void setIdNo(String idNo){
this.idNo=idNo;
}
public void setGrade(byte gradEst){
this.gradEst=gradEst;
}
public void setCodeSeat(String codeSeat){
this.codeSeat=codeSeat;
}
public String getIdNo(){
return idNo;
}
public byte getGrado(){
return gradEst;
}
public byte getCodeSeat(){
return codeSeat;
}
public static ArrayList<Student> generateBackup(){
FileInputStream f3 = null;
ObjectInputStream f4 = null;
ArrayList<Student> students = new ArrayList<Student>();
try{
f3=new FileInputStream("C:\\Documents and Settings\\Daniel Garcia\\Escritorio\\Andres\\Final\\students.dat");
f4=new ObjectInputStream(f3);
while(true){
Student e=(Student)f4.readObject();
students.add(e);
}
}
catch(EOFException e){
System.out.println("End of file");
}
catch(IOException e){
System.out.println("Error reading file");
}
catch(ClassNotFoundException e){
System.out.println("Error in type of class");
}
finally{
try{
f4.close();
f3.close();
}
catch(IOException e){
System.out.println("Error closing file");
}
}
return students;
}
public static void writeBackup(ArrayList<Student> students){
FileOutputStream f1 = null;
ObjectOutputStream f2 = null;
try{
f1=new FileOutputStream("C:\\Documents and Settings\\Daniel Garcia\\Escritorio\\Andres\\Final\\students.dat");
f2=new ObjectOutputStream(f1);
for(Student e : students){
f2.writeObject(e);
}
}
catch(IOException e){
System.out.println ("Error saving file");
}
finally{
try{
f2.close();
f1.close();
}
catch(IOException e){System.out.println ("Error closing file");
}
}
}
}
import java.io.*;
import java.util.*;
class Main{
public static void main (String args[]){
boolean menu = false;
Student x = null;
do{
System.out.println("Welcome!!");
System.out.println();
System.out.println("What do you want to do?");
System.out.println("1) Separate a seat");
System.out.println("2) Cancel a seat");
System.out.println("3) Another here");
System.out.println("4) Another here");
System.out.println("5) Another here");
System.out.println();
char ans = Lectura.readChar();
System.out.println();
switch(ans){
case '1':
Student.generateBackup();
System.out.println("Name");
String name = Lectura.readString();
System.out.println("Age");
byte age = Lectura.readByte();
System.out.println("Sex M/F");
char sex = Lectura.readChar();
System.out.println("Id. No.");
String idNo = Lectura.readString();
System.out.println("Grade of Studies");
byte gradEst = Lectura.readByte();
System.out.println("Location");
String location = Lectura.readString();
System.out.println("Here is a Seating chart");
Seatings.displaySeatings();
System.out.println("Select your seat");
String codeSeat = Lectura.readString();
x = new Estudiante(name,sex,age,location,idNo,gradEst,codeSeat);
//I need to put x in the arraylist but I don't know how to do that, any ideas?? Estudiante.writeBackup();
menu=true;
break;
}
}
while(menu);
}
}
I translated from Spanish to English some parts of the code so you understand it with more precision.
Thanks for the help in advance.