Introduction and Background As we discussed in lecture files
Introduction and Background
As we discussed in lecture, files can be a very useful as a way to input data into a program and to store the output of program execution. Arrays can also be useful data structures in which to store and access information. In this lab you will develop a simple application that utilizes object-oriented programming, Java arrays and a Java text file. The idea is as follows: You will implement a very primitive database of movies. Initially, the database will be read into the program from a text file. Then, through the program you will be able to search for a movie, add a movie, show the complete list of movies, or quit the program. When you quit the program the list of movies will be saved back to the file.
We discussed in lecture how an ArrayList can be utilized to store / manipulate a collection of objects. In this lab you will see some of what an ArrayList does for you in the MovieDB class. It is a good way to see how to implement these operations and also why having an ArrayList already implemented for us is a very useful thing.
Lab Exercise
Most of this lab has been written for you, with comments. Read over the code and the comments carefully, so that you understand what is being done. Note especially how the Movie class and the MovieDB class have been set up. Note also the movieFile.txt data file, so you understand how the data is to be formatted. Your task is to complete the sections of code that have been omitted from the main program so that it works correctly. Ask your TA if you need help.
Here are the pertinent files:
Movie.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/Movie.java) -- Class to represent a Movie. This has already been completed for you, but read the comments carefully.
 MovieDB.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/MovieDB.java) -- Class to represent a simple database of Movies. This also has been completed for you, but again read it carefully to see how it implements the various operations
 Lab7.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/Lab7.java) -- Main program class. Much of this has also been implemented, but you must complete 3 sections to get it to work
 movieFile.txt (http://people.cs.pitt.edu/~ramirez/cs401/labs/movieFile.txt) -- Example file used for the program
 output.txt (http://people.cs.pitt.edu/~ramirez/cs401/labs/output.txt) -- Demo runs to show how program should work
Complete the 3 CODE SEGMENTS in Lab7.java to complete this lab.
Solution
Lab7.java
// CS 401 Fall 2016 Lab 7 Main Program
 // Your job is to complete this program so that it runs correctly.
 // The Movie class and MovieDB class have already been completed for you.
 // Utilizing data abstraction, you can access/use these classes without
 // having to know their implementation details. However, since you have
 // the code for them you certainly can look at the implementations.
 // You just need to write the correct code in the 3 passages below. Some
 // comments indicate what you need to do in each case.
 import java.util.*;
 import java.io.*;
 public class Lab7
 {
    public static void main(String [] args) throws IOException
    {
        MovieDB movies = new MovieDB(10); // Create MovieDB object. The
                    // size is set at 10, meaning it can hold up to 10
                    // movies. If we wanted (as discussed in lecture) we
                    // could allow for it to be resized so it could hold
                    // an arbitrary number of movies.
        loadData(movies);       // input movie data from file
        getCommands(movies);   // interact with user
        saveData(movies);       // save movie data back to file
    }
   public static void loadData(MovieDB movies) throws IOException
    {
        // Note the syntax below for creating a Scanner to a file
        Scanner S = new Scanner(new FileInputStream(\"movieFile.txt\"));
       // *** CODE SEGMENT 1 *** //
        // Complete this method in the following way:
        // Read in the number of movies from the file
        // For each movie read the data from the file and create a Movie
        // object
        // Add the Movie to the MoviesDB object (movies) using the appropriate
        // method (see MovieDB class)
        int n;
        int numMovies;
       //try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        String title = new String();
        String director = new String();
        String studio = new String();
        double gross;
        numMovies = Integer.parseInt(S.nextLine());
        //MovieDB movieDB = new MovieDB(numMovies);
           for(int i=0; i<numMovies; i++){
            title = S.nextLine();
            director = S.nextLine();
            studio = S.nextLine();
            gross = Double.parseDouble(S.nextLine());
            Movie movie = new Movie(title, director, studio, gross);
            movies.addMovie(movie);
        }
    }
   public static void getCommands(MovieDB movies)
    {
        Scanner inScan = new Scanner(System.in);
        System.out.println(\"Enter your choice:\");
        System.out.println(\"1. List movies\");
        System.out.println(\"2. Add new movie\");
        System.out.println(\"3. Find movie\");
        System.out.println(\"4. Quit\");
        String choice = inScan.nextLine();
        while (true)
        {
            Movie temp;
            if (choice.equals(\"1\"))
            {
                System.out.println(movies.toString());
            }
            else if (choice.equals(\"2\"))
            {
                // *** CODE SEGMENT 2 *** //
                // Complete this choice in the following way:
                // Prompt for and read in each of the values needed
                // for the new Movie object (3 strings, 1 double)
                // Create a new Movie object and then add it to the
                // MovieDB object (movies) using the correct method.
                System.out.println(\"Movie name?\");
                String title = inScan.nextLine();
                System.out.println(\"Director?\");
                String director = inScan.nextLine();
                System.out.println(\"Studio?\");
                String studio = inScan.nextLine();
                System.out.println(\"Gross?\");
                Double gross = Double.parseDouble(inScan.nextLine());
                temp = new Movie(title, director, studio, gross);
                movies.addMovie(temp);
            }
            else if (choice.equals(\"3\"))
            {
                // *** CODE SEGMENT 3 *** //
                // Complete this choice in the following way:
                // Ask the user for the movie name and read it in
                // Call the appropriate method in the MovieDB object
                // (movies) to find the Movie and return it
                // Show the movie\'s info (or indicate it is not found)
                System.out.println(\"Movie name?\");
                String title = inScan.nextLine();
                temp = movies.findMovie(title);
                if (temp==null){
                    System.out.println(title+\" was not found.\");
                }
                else{
                    System.out.println(temp.toString());
                }
            }
            else
            {
                System.out.println(\"Good-bye\");
                break; // any other value -- quit
            }
            System.out.println(\"Enter your choice:\");
            System.out.println(\"1. List movies\");
            System.out.println(\"2. Add new movie\");
            System.out.println(\"3. Find movie\");
            System.out.println(\"4. Quit\");
            choice = inScan.nextLine();
        }
    }
   public static void saveData(MovieDB movies) throws IOException
    {
        PrintWriter P = new PrintWriter(\"movieFile.txt\");
        // Note that we are printing the entire DB to the file with
        // one statement. This is because the toStringFile() method
        // of the MovieDB class calls the toStringFile() method of
        // each Movie within the DB.
        P.print(movies.toStringFile());
        P.close();
    }
 }



