/* DVDCollection.java        Authors: Koffman & Wolz
 * Class that stores a collection of DVDs.
 * Uses FileReader, BufferedReader,
 * FileWriter, and PrintWriter.
 */
import javax.swing.*;
import java.io.*;
import java.util.*;

public class DVDCollection {
   // Data field
   Vector DVDs = new Vector();

   // Methods
   
   // postcondition: Reads all data from specified input file into
   //   vector DVDs. Throws a FileNotFoundException 
   //   if input file is missing.
   public void readDVDs(BufferedReader ins) {
      int year = 0;
      try {
        // Read and store data for DVD collection
        String title = ins.readLine();
        while (title != null) {
           String category = ins.readLine();
           year = Integer.parseInt(ins.readLine());
           double cost = Double.parseDouble(ins.readLine());
           DVDs.addElement(new DVD(title, category, year, cost));
           title = ins.readLine();
        }
        ins.close();
      } // catch or throw exceptions
      catch (NumberFormatException ex) {
        System.out.println("bad numeric string" + year);
        ex.printStackTrace();
      }
      catch (IOException ex) {
        System.out.println("i/o error " + ex.getMessage());
        ex.printStackTrace();
      }
   }

   // precondition: Vector DVDs stores a DVD collection
   // postconditon: Writes the collection to the specified file.
   public void writeDVDs(PrintWriter outs) {
      // Write the DVD collection to the output file
      for (int next = 0; next < DVDs.size(); next++) {
         DVD nextDVD = (DVD) DVDs.elementAt(next);
         outs.println(nextDVD.getTitle());
         outs.println(nextDVD.getCategory());
         outs.println(nextDVD.getYear());
         outs.println(nextDVD.getTime());
      }

      outs.close();
      System.out.println("Output file written");
   }

   // postcondition: Adds the specified DVD to the collection.
   public void addDVD(DVD aDVD) {
      DVDs.addElement(aDVD);
   }

   // postcondition; Returns a string of all DVD titles
   //   in a specified category.
   public String listInCategory(String targetCat) {
      String result = "";
      for (int next = 0; next < DVDs.size(); next ++) {
        DVD nextDVD = (DVD) DVDs.elementAt(next);
        if (nextDVD.getCategory().equals(targetCat))
           result += nextDVD.getTitle() + "\n";
      }
      return result;
   }

   // postcondition: Returns the DVD with a specified title.
   //   Returns a null reference if not found.
   public DVD searchDVD(String aTitle) {
      for (int next = 0; next < DVDs.size(); next ++) {
         DVD nextDVD = (DVD) DVDs.elementAt(next);
         if (nextDVD.getTitle().equals(aTitle))
           return nextDVD;
       }

       // Assertion: DVD with aTitle not found.
       return null;
   }

   // postcondition: Represents the collection as a string.
   public String toString() {
      return DVDs.toString();
   }

   // Driver program to test the DVDCollection class.
   public static void main(String[] args) {
     try {
        // Create DVD collection and read and store DVDs.
        DVDCollection myDVDs = new DVDCollection();

        // Create object ins and connect it to the input file
        String inFileName =
           JOptionPane.showInputDialog("Input file:");
        FileReader inStream = new FileReader(inFileName);
        BufferedReader ins = new BufferedReader(inStream);

        myDVDs.readDVDs(ins);
        System.out.println(myDVDs.toString());

        // Display DVDs in a specified category.
        String targetCat =
           JOptionPane.showInputDialog("Enter target category:");
        System.out.println("\nDVDs in category " + targetCat + "\n" +
                           myDVDs.listInCategory(targetCat));

        // Create object outs and connect it to the output file
        String outFileName =
           JOptionPane.showInputDialog("Output file name:");
        FileWriter outStream = new FileWriter(outFileName);
        PrintWriter outs = new PrintWriter(outStream);

        // Write DVDs to the output file.
        myDVDs.writeDVDs(outs);

     }  // catch exceptions
     catch (IOException ex) {
        System.out.println(ex.getMessage());
        ex.printStackTrace();
     }
   }

}

