/* DVDArray.java        Authors: Koffman & Wolz
 * Class that stores DVD information in an array of DVDs.
 */
import java.io.*;
import javax.swing.*;
public class DVDArray {

   // Data field
   private DVD[] DVDs;  // array of DVDs

   // Methods
   // Constructors
   public DVDArray() {}  

   public DVDArray(int mySize) {
      DVDs = new DVD[mySize];
   }

   // postcondition: Reads a DVD collection from text file ins
   //   into array DVDS.
   public void readDVDs(BufferedReader ins) {
      try {
         // Read and store the data for DVD collection in array DVDs.
         String title = ins.readLine();
         int count = 0;
         while (title != null && count < DVDs.length) {
            String category = ins.readLine();
            int year = Integer.parseInt(ins.readLine());
            double time = Double.parseDouble(ins.readLine());
            DVDs[count] = new DVD(title, category, year, time);
            count++;
            title = ins.readLine();
         }

         ins.close();
      }  // catch exceptions
      catch (NumberFormatException ex) {
         System.out.println("bad numeric string");
         ex.printStackTrace();
      }
      catch (IOException ex) {
         System.out.println("i/o error: " + ex.getMessage());
         ex.printStackTrace();
      }
   }

   // postcondition: Writes the array DVDs as
   //   a single object to binary file outs. 
   public void writeDVDArray (ObjectOutputStream outs) {
      try {
         // Write array DVDs as a record to binary output file
         outs.writeObject(DVDs);

         outs.close();
      } catch (IOException ex) {
         System.out.println("i/o error: " + ex.getMessage());
         ex.printStackTrace();
      }
   }

 
   // postcondition: Reads a single object (type DVD[]) from file
   //   ins into array DVDS. 
   public void readDVDArray(ObjectInputStream ins) {
      try {
         // Read DVD array as a record from binary stream ins.
         try {
            DVDs = new DVD[DVDs.length];
            DVDs = (DVD[]) ins.readObject();
         } catch (EOFException eof) {
            System.out.println("end of input file reached");
            ins.close();
         } // catch errors
      } catch (IOException ex) {
         System.out.println("i/o error: " + ex.getMessage());
         ex.printStackTrace();
      } catch (ClassNotFoundException ex) {
         System.out.println("object in binary file is not DVD[]");
         ex.printStackTrace();
      }

   }

   public String toString() {
     String result = "";
     for (int i = 0; i < DVDs.length; i++)
       result += DVDs[i];
     return result;
   }

   // Driver program to test readDVDArray and writeDVDArray.
   // postcondition: reads DVD data from a text file,
   //   writes it as an array to an output file and reads
   //   the array from the output file.
   public static void main(String[] args) {
      try {
         DVDArray myDVDs = new DVDArray(3);

        // Create object ins and connect it to the input file
        String inFileName =
             JOptionPane.showInputDialog("Enter input file name");
        FileReader inStream = new FileReader(inFileName);
        BufferedReader ins = new BufferedReader(inStream);

        // Create object outs and associate it with the output file.
        String outFileName =
             JOptionPane.showInputDialog("Enter output file name");
        FileOutputStream outStream = new FileOutputStream(outFileName);
        ObjectOutputStream outs = new ObjectOutputStream(outStream);

        // Read the DVD information into the array.
        myDVDs.readDVDs(ins);
        System.out.println(myDVDs.toString());
        myDVDs.writeDVDArray(outs);

        // Create object bins and connect it to the output file
        FileInputStream binStream = new FileInputStream(outFileName);
        ObjectInputStream bins = new ObjectInputStream(binStream);

        // Read array data from the "output file".
        myDVDs.readDVDArray(bins);
        System.out.println(myDVDs.toString());

    }  catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
  }

}

