Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: Write a program that manages a Movie database. Requirements: Each mov

ID: 3761922 • Letter: O

Question

Objective:

Write a program that manages a Movie database.

Requirements:

Each movie needs to have the follow attributes:

Name

Year

Rating (1 to 5 stars)

Director

Box Office Gross

The Movie database must be able to:

Add a movie

Remove a movie by title

Sort movie by Title

Sort movie by Rating

Sort movie by Box Office Gross

Sort movies by a director (LastName)

Print to a database file (you may define your own protocol)

Read from a database file

***Write a front end that will allow a user to use any of the features noted in the database description

Something similar to the following three pieces from a Taco program that pretty much has identical requirements to the program I need written (please separate each new piece of code with dashes as I did below):

Explanation / Answer

Complete Program:

File: Movie.java

// Movie class implementation
public class Movie
{
   private String name;  
   private int year;
   private int rating;  
   private String director;
   private double gross;
  
   public Movie()
   {
       name = "no name yet";      
       year = 0;
       rating = 0;
       director = "no name yet";
       gross = 0.0;
   }

   public Movie(String aName, int aYear, int aRating, String aDirector, double aGross)
   {
       name = aName;      
       year = aYear;
       rating = aRating;
       director = aDirector;
       gross = aGross;
   }

   public String getName()
   {
       return name;
   }

   public void setName(String aName)
   {
       name = aName;
   }

   public int getYear()
   {
       return year;
   }

   public void setYear(int aYear)
   {
       year = aYear;
   }

   public int getRating()
   {
       return rating;
   }

   public void setRating(int aRating)
   {
       rating = aRating;
   }
  
   public String getDirector()
   {
       return director;
   }

   public void setDirector(String aDirector)
   {
       director = aDirector;
   }

   public double getGross()
   {
       return gross;
   }

   public void setGross(double aGross)
   {
       gross = aGross;
   }      
} // end of Movie class

File: MovieDatabase.java

// MovieDatabase class implementation
import java.io.*;
import java.util.Scanner;
public class MovieDatabase
{
   private Movie[] movies;
   public static final int MOVIESDB_SIZE = 10;
   public static final String delim = " ";
   public static final int FIELD_AMT = 5;

   public MovieDatabase()
   {
       movies = new Movie[MOVIESDB_SIZE];
   }

   public Movie[] getMovies()
   {
       return movies;
   }

   public void addMovie(Movie aMovie)
   {
       if(movies[movies.length - 1] != null)
       {
           System.out.println("The movie database is full");
           return;
       }
      
       for(int i = 0; i < movies.length - 1; i++)
       {
           if(movies[i] == null)
           {
               movies[i] = aMovie;
               break;
           }
       }
   }

   public void removeMovie(String aName)
   {
       int removeIndex = -1;
      
       for(int i = 0; i < movies.length; i++)
       {
           if(movies[i].getName().equalsIgnoreCase(aName))
           {
               removeIndex = i;
               break;
           }
       }
      
       if(removeIndex == -1)
       {
           System.out.println("The movie was not found");
       }
       else
       {
           for(int i = removeIndex; i < movies.length - 1; i++)
           {
               movies[i] = movies[i + 1];
           }
           movies[movies.length - 1] = null;
       }
   }
  
   public void sortMoviesByTitle()
   {
       boolean swapped = true;
      
       while(swapped == true)
       {
           swapped = false;
           for(int i = 0; i < movies.length - 1; i++)
           {
               if(movies[i + 1] == null)
               {
                   break;
               }
              
               if(movies[i].getName().compareToIgnoreCase(movies[i + 1].getName()) < 0)
               {
                   Movie temp = movies[i];
                   movies[i] = movies[i + 1];
                   movies[i + 1] = temp;
                   swapped = true;
               }
           }
       }
   }
  
   public void sortMoviesByRating()
   {
       boolean swapped = true;
      
       while(swapped == true)
       {
           swapped = false;
           for(int i = 0; i < movies.length - 1; i++)
           {
               if(movies[i + 1] == null)
               {
                   break;
               }
              
               if(movies[i].getRating() < movies[i + 1].getRating())
               {
                   Movie temp = movies[i];
                   movies[i] = movies[i + 1];
                   movies[i + 1] = temp;
                   swapped = true;
               }
           }
       }
   }
  
   public void sortMoviesByGross()
   {
       boolean swapped = true;
      
       while(swapped == true)
       {
           swapped = false;
           for(int i = 0; i < movies.length - 1; i++)
           {
               if(movies[i + 1] == null)
               {
                   break;
               }
              
               if(movies[i].getGross() < movies[i + 1].getGross())
               {
                   Movie temp = movies[i];
                   movies[i] = movies[i + 1];
                   movies[i + 1] = temp;
                   swapped = true;
               }
           }
       }
   }
  
   public void sortMoviesByDirector()
   {
       boolean swapped = true;
      
       while(swapped == true)
       {
           swapped = false;
           for(int i = 0; i < movies.length - 1; i++)
           {
               if(movies[i + 1] == null)
               {
                   break;
               }
              
               if(movies[i].getDirector().compareToIgnoreCase(movies[i + 1].getDirector()) < 0)
               {
                   Movie temp = movies[i];
                   movies[i] = movies[i + 1];
                   movies[i + 1] = temp;
                   swapped = true;
               }
           }
       }
   }
  
   public void writeMovieDBFile(String aFileName)
   {
       try
       {
           PrintWriter fileWriter = new PrintWriter(new FileOutputStream(aFileName));
           for(Movie movie : movies)
           {
               if(movie == null)
               {
                   break;
               }
               fileWriter.println(movie.getName() + delim + movie.getYear() + delim + movie.getRating() + delim + movie.getDirector() + delim + movie.getGross());
           }
           fileWriter.close();
       }
       catch(Exception e)
       {
           System.out.println("Exception" + e.getMessage());
       }
   }
  
   public void readMovieDBFile(String aFileName)
   {
       try
       {
           movies = new Movie[MOVIESDB_SIZE];
           Scanner fileScanner = new Scanner(new File(aFileName));
           int movieCount = 0;
          
           while(fileScanner.hasNextLine())
           {
               String fileLine = fileScanner.nextLine();
               String[] splitLines = fileLine.split(delim);
               if(splitLines.length == FIELD_AMT)
               {
                   String name = splitLines[0];
                   int year = Integer.parseInt(splitLines[1]);
                   int rating = Integer.parseInt(splitLines[2]);
                   String diector = splitLines[3];
                   double gross = Double.parseDouble(splitLines[4]);
                  
                   Movie aMovie= new Movie(name, year, rating, diector, gross);
                  
                   addMovie(aMovie);
                  
                   movieCount++;
               }
               else
               {
                   System.out.println("Not well formed so ignored");
               }
           }
           fileScanner.close();
       }
       catch(Exception e)
       {}
   }  
} // end of MovieDatabase class

File: MovieDBFrontEnd.java

// MovieDBFrontEnd class implementation
import java.util.Scanner;
public class MovieDBFrontEnd
{
   public static void main(String[] args)
   {
       MovieDatabase movieDB = new MovieDatabase();
      
       Scanner keyboard = new Scanner(System.in);
      
       System.out.println("Welcome to the movie database");
      
       boolean quit = false;
      
       while(quit == false)
       {
           System.out.println(" Enter 1 to add a movie "
                   + "Enter 2 to remove a movie "
                   + "Enter 3 to sort movies by title "
                   + "Enter 4 to sort movies by rating "
                   + "Enter 5 to sort movies by box office gross "
                   + "Enter 6 to sort movies by director "
                   + "Enter 7 to read a movie database file "
                   + "Enter 8 to write to a movie database file "
                   + "Enter 9 to quit");
          
           System.out.print("Enter your choice: ");
           int choice = keyboard.nextInt();
          
           switch(choice)
           {
               case 1:
                   System.out.print(" Enter the name of the movie as a single word: ");
                   String name = keyboard.next();
                  
                   System.out.print("Enter the year of the movie: ");
                   int year = keyboard.nextInt();
                  
                   System.out.print("Enter the rating of the movie: ");
                   int rating = keyboard.nextInt();
                  
                   System.out.print("Enter the diector name of the movie as a single word: ");
                   String diector = keyboard.next();
                  
                   System.out.print("Enter the gross of the movie: ");
                   double gross = keyboard.nextDouble();
                  
                   movieDB.addMovie(new Movie(name, year, rating, diector, gross));
                   break;
                  
               case 2:
                   System.out.print(" Enter the name of the movie as a single word: ");
                   name = keyboard.next();
                  
                   movieDB.removeMovie(name);
                   break;
                  
               case 3:
                   movieDB.sortMoviesByTitle();
                   break;
                  
               case 4:
                   movieDB.sortMoviesByRating();
                   break;
                  
               case 5:
                   movieDB.sortMoviesByGross();
                   break;
                  
               case 6:
                   movieDB.sortMoviesByDirector();
                   break;
                  
               case 7:
                   System.out.print(" Enter the file name to read a MovieDB");
                   String fileName = keyboard.next();
                  
                   movieDB.readMovieDBFile(fileName);
                   break;
                  
               case 8:
                   System.out.print(" Enter the file name to write a MovieDB File");
                   fileName = keyboard.next();
                  
                   movieDB.writeMovieDBFile(fileName);
                   break;
                  
               case 9:
                   quit = true;
                   break;
                  
               default:
                   System.out.println("Invalid input");
           }
          
           for(Movie movie : movieDB.getMovies())
           {
               if(movie == null)
               {
                   break;
               }
               System.out.println(movie.getName() + " " + movie.getYear() + " " + movie.getRating() + " " + movie.getDirector() + " $" + movie.getGross());
           }
       }
       System.out.println("Good bye");
   }
} // end of MovieDBFrontEnd class