0

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.

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by javaAddict
0

At the btnOkActionPerformed whenver the button is clicked, you read the file and the you add the new DVD to the list. It is not necessary. You need to read only once the file and sotre those value at the list. Of course I don't know the purpose of that button.

Usually you read the file into a list at the begining of your program once, then add as many as you want new DVDs to the list and when you are done save the entire list back to the file.

So you don't need to append the new values.


If you want to keep your code, adn you do want to append the new DVD:

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();

Then look the API of those classes. One of the constructors has an extra argument that when set to true, whatever you write, is appended to at the end of the file.
Look the constructors at the API of the class FileWriter or PrintWriter.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.