0

I am making cinema seat booking system as semester project. I saved person name, movie name and seat numbers attributes in a .txt file with javafx, but the problem is that it overwrites previous record when I make bookings for another person. No matter how many bookings I make it only saves last one because this line FileWriter data=new FileWriter("reservation.txt"); is also called every time. So is there a way to make .txt file in another class and write data in another class, or any other solution?

try{
    FileWriter data=new FileWriter("reservation.txt");

    data.write("Customer Name: "+Cinemafx.name+"    ");
    data.write("Movie Name: "+movieselection.movie_name+"    ");
    data.write("Seat Numbers: "+listString+"    ");

    data.close();
}
    catch(Exception ex){
}
veben
  • 19,637
  • 14
  • 60
  • 80
irfan
  • 15
  • 4

2 Answers2

1

You could save all the data in a collection and loop through the collections once and write everthing in the Flatfile without overwriting it. Or you have also the option to append data to a File with Filewriter, take a look at this question: Java FileWriter with append mode.

Alex Gruebel
  • 86
  • 1
  • 9
0

You can make a new file for each registration and save it in a registrations folder; that way not only will you be able to avoid overwriting but it'll be much easier to retrieve data for each customer as well. So for example you can give your file the name of your customer like:

FileWriter data = new FileWriter(Cinemafx.name + ".txt");

Though when multiple customers' name is same then it could cause a problem so you have to work around that. Suggestion: include an ID field maybe?