I got a project, it want us create a application that can keep track of your collection of DVD and allows a user add a new DVD to the collection. The DVD information contain Title, Category, Year and Rating. In this application must use list (ArrayList or Vector or LinkedList) to store the list of DVD objects in your application and store all of the DVD data into text file permanently.
Example of a text file containing three DVDs is:
Avatar:Sci-fi:2009:PG-13
Toy Story:Animation:1995:G
The Proposal:Comedy:2009:PG-13
In this file, each DVD is written in one line and each data field for each DVD is separated by a semicolon (:)
public class DVDList extends javax.swing.JFrame {
String line, DVDlist = "D:/My Documents/NetBeansProjects/DVD-List/DVD_Collection.txt";
DVD dvd, dvd2;
LinkedList<DVD> list = new LinkedList<DVD>();
private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {
AddWin.getAccessibleContext();
AddWin.setVisible(true);
AddWin.setSize(300, 280);
}
private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {
try {
File file = new File(DVDlist);
Scanner scan = new Scanner(file);
scan.useDelimiter(":");
while (scan.hasNextLine()) {
String title = scan.next();
String category = scan.next();
String year = scan.next();
String rate = scan.next();
dvd2 = new DVD(title, category, year, rate);
list.add(dvd2);
}
String title = txtTitle.getText();
String category = txtCategory.getText();
String year = txtYear.getText();
String rate = txtRate.getText();
dvd = new DVD(title, category, year, rate);
dvd.setInfo(title, category, year, rate);
list.add(new DVD(title, category, year, rate));
AddWin.dispose();
} catch (IOException iox) {
}
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
try {
File file = new File(DVDlist);
FileWriter fw = new FileWriter(file);
PrintWriter pw = new PrintWriter(fw);
pw.println(dvd.getTitle() + ":" + dvd.getCategory() + ":" + dvd.getYear() + ":" + dvd.getRate());
pw.flush();
fw.close();
} catch (IOException iox) {
}
}
public class DVD {
public String title, category, year, rate;
public DVD(String t, String c, String y, String r) {
title = t;
category = c;
year = y;
rate = r;
}
public String getTitle() {
return title;
}
public String getCategory() {
return category;
}
public String getYear() {
return year;
}
public String getRate() {
return rate;
}
public void setInfo(String title, String category, String year, String rate) {
this.title = title;
this.category = category;
this.year = year;
this.rate = rate;
}
The Problem I facing here is when I run the program and add new DVD info, it will overwrite my previous DVD info.
How should I do to make the new info save in new line.