Advertisement
SageTheWizard

jMDB.java

Feb 24th, 2018
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.52 KB | None | 0 0
  1. /*
  2.   Author: Jacob Gallucci
  3.   E-mail: jag6248@psu.edu
  4.   Course: CMPSC 221
  5.   Assignment: Programming Assignment 2 - Movie Database - Main
  6.   Due date: 2/27/2018
  7.   File: jMDB.java
  8.   Purpose: Driver for jMDB movie database:  Gives the user the ability to view the different movies in the Database
  9.            Also contains ... Sorting Methods, Searching method to search the database, Display methods to display the movie list
  10.                              setup database method and getInput methods
  11.   Compiler/IDE: OpenJDK 1.8.0_151 (compiled from CLI) - Atom / Vim text editors
  12.   Operating system: Debian Stretch 9
  13.   Reference(s):
  14.                 I had a bubble sort program from HACC I referenced to get the alphabettical sort
  15.                 https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html - Reference for exceptions
  16. */
  17. import java.io.*;
  18. import java.util.*;
  19.  
  20. public class jMDB
  21. {
  22.   public static void main(String[] args)
  23.   {
  24.     awardMovie[] movies = new awardMovie[10];   // Array of awardMovies
  25.     Scanner keyboard = new Scanner(System.in); // Scanner for user input
  26.     String sortTypeInput;
  27.     int sortType;     // Input (1 or 2) for how the user wishes to sort the movie titles
  28.     int choice = -1; // Loop controller
  29.     char search;    // Yes or no if the user wants to search the database
  30.     char goAgain;  // Yes or no if the user wants to do another search / view an element of the DB
  31.  
  32.     for (int i = 0; i < 10; i++) // Initializing the array
  33.       movies[i] = new awardMovie();
  34.  
  35.     setupMovies(movies);  // Calls function that reads data from a text file to input into the array's objects
  36.     System.out.println("Welcome to jMDB! Jacob Gallucci's knock off Movie Database!\n");
  37.     do
  38.     {
  39.       System.out.println("How would you like to sort the Award Winning Movies?\n 1: By Title (alphabetically)\n 2: By Genre (alphabetically)\n");
  40.       System.out.print("Input an option from above: ");
  41.       try
  42.       {
  43.         sortTypeInput = keyboard.nextLine();
  44.         sortType = Integer.parseInt(sortTypeInput);
  45.       }
  46.       catch(NumberFormatException ex)
  47.       {
  48.         System.out.println("Error Please enter an Integer!");
  49.         sortType = -1;
  50.       }
  51.     }while((sortType != 1) & (sortType != 2));
  52.  
  53.     if (sortType == 0)
  54.       sortAlphaTitle(movies);   // Sorts list alphabetically
  55.     else
  56.       sortAlphaGenre(movies);
  57.  
  58.     do // Go Again Loop - Loops while the user wants to remain in the program
  59.     {
  60.       displayTitles(movies, sortType); // Displays menu of movies
  61.  
  62.       do // Search Loop - Loops while the user wants to search the database
  63.       {
  64.         System.out.print("Would you like to search the database?(Y/N): ");
  65.         search = getSearchInput(keyboard); // Gets search Yes or No
  66.  
  67.         if (Character.toUpperCase(search) == 'Y') // if the user wants to search the database
  68.         {
  69.           searchDB(movies, keyboard); // Searches the database
  70.           displayTitles(movies, sortType);  // Displays list of movies again
  71.         }
  72.       }while(Character.toUpperCase(search) == 'Y');
  73.  
  74.       choice = getInput(keyboard); // User enters their movie of choice to learn more about
  75.  
  76.       System.out.println(movies[(choice - 1)]); // Prints information about the movie
  77.  
  78.       System.out.print("Would you like to go again?(Y/N): ");
  79.       goAgain = getSearchInput(keyboard); // Gets user input whether they want to go again
  80.  
  81.     }while(Character.toUpperCase(goAgain) == 'Y');
  82.     System.out.println("Thank you for using jMDB!");
  83.  
  84.     System.exit(0); // end program
  85.   }
  86.  
  87. //////////////////////////////////////////////////////////////////////////METHODS////////////////////////////////////////////////
  88.   private static awardMovie[] setupMovies(awardMovie[] list) // Sets up movie array
  89.   {
  90.     String dummyString; // String used for input
  91.     int i = 0; // Loop controller
  92.     int dummyInt; // String used to convert input to an integer
  93.     try
  94.     {
  95.       FileReader reader = new FileReader("movies.txt");     // Sets up file reader
  96.       BufferedReader buffReader = new BufferedReader(reader); // Sets up buffer reader
  97.  
  98.       // this loop inputs data from file into awardString object while there is data in the file or integer i is less than 10
  99.       /*
  100.        * NOTE: If the text file is in the incorrect format this will result in an incorrect format (ie. Director could be the title of the movie)
  101.       */
  102.       while (((dummyString = buffReader.readLine()) != null) || (i < 10))
  103.       {
  104.         list[i].setTitle(dummyString);
  105.         dummyString = buffReader.readLine();
  106.         list[i].setRating(dummyString);
  107.         dummyString = buffReader.readLine();
  108.         list[i].setGenre(dummyString);
  109.         dummyString = buffReader.readLine();
  110.         list[i].setDirector(dummyString);
  111.         dummyString = buffReader.readLine();
  112.         list[i].setStar(dummyString);
  113.         dummyString = buffReader.readLine();
  114.         list[i].setAwardTitle(dummyString);
  115.         dummyInt = Integer.parseInt(buffReader.readLine());
  116.         list[i].setAwardYear(dummyInt);
  117.         i++;
  118.       }
  119.     }
  120.     // If the file is not found
  121.     catch(FileNotFoundException ex)
  122.     {
  123.       System.out.println("File not found!");
  124.     }
  125.     // If the file is broken
  126.     catch(IOException ex)
  127.     {
  128.       System.out.println("File Broken! Pls Fix!");
  129.     }
  130.     return list; // Returns setup array to the main
  131.   }
  132.   // Uses a bubble sort to sort titles alphabettically from #->Z
  133.   public static awardMovie[] sortAlphaTitle(awardMovie[] list)
  134.   {
  135.     awardMovie temp = new awardMovie(); // Temporary awardMovie object used to swap
  136.     for (int i = 0; i < 9; i++)
  137.     {
  138.       for (int j = 0; j < (8 - i); j++)
  139.       {
  140.         if (((list[j].getTitle()).compareToIgnoreCase((list[j+1].getTitle()))) > 0)
  141.         {
  142.           temp = list[j];
  143.           list[j] = list[j+1];
  144.           list[j+1] = temp;
  145.         }
  146.       }
  147.     }
  148.     return list; // Returns sorted list to the main
  149.   }
  150.   // Uses bubble sort to sort genres alphabetically if genres are the same, it sorts it by title alphabetically
  151.   public static awardMovie[] sortAlphaGenre(awardMovie[] list)
  152.   {
  153.     awardMovie temp = new awardMovie(); // Temporary awardMovie object used to swap
  154.     for (int i = 0; i < 9; i++)
  155.     {
  156.       for (int j = 0; j < (8 - i); j++)
  157.       {
  158.         if (((list[j].getGenre()).compareToIgnoreCase((list[j+1].getGenre()))) > 0)
  159.         {
  160.           temp = list[j];
  161.           list[j] = list[j+1];
  162.           list[j+1] = temp;
  163.         }
  164.         else if (((list[j].getGenre()).compareToIgnoreCase((list[j+1].getGenre()))) == 0)
  165.         {
  166.           if (((list[j].getTitle()).compareToIgnoreCase((list[j+1].getTitle()))) > 0)
  167.           {
  168.             temp = list[j];
  169.             list[j] = list[j+1];
  170.             list[j+1] = temp;
  171.           }
  172.         }
  173.       }
  174.     }
  175.     return list; // Returns sorted list to the main
  176.   }
  177.  
  178.   // Displays titles of the movies depending on how the user chooses they wish to see the movies
  179.   public static void displayTitles(awardMovie[] list, int type)
  180.   {
  181.     if (type == 1) // if user wants the movies alphabetically by title
  182.     {
  183.       for (int i = 0; i < 10; i++)
  184.         System.out.println((i + 1) + ": " + list[i].getTitle());
  185.     }
  186.     else // if the user wants the movies alphabetically by genre
  187.     {
  188.       for (int i = 0; i < 10; i++)
  189.         System.out.println((i + 1) + ": [" + list[i].getGenre() + "] " + list[i].getTitle());
  190.     }
  191.  
  192.     return;
  193.   }
  194.  
  195.   // Gets user input as it relates to the movie the user wants to view
  196.   public static int getInput(Scanner key)
  197.   {
  198.     String input; // user input
  199.     int selection = -1; // user input converted to integer
  200.     System.out.print("\nEnter the number that corrisponds to the movie you wish to view in detail: ");
  201.     do // Loops while the user's input is not between 1 and 10
  202.     {
  203.       input = key.nextLine();
  204.       try
  205.       {
  206.         selection = Integer.parseInt(input);
  207.       }
  208.       catch (NumberFormatException e) // if the user does not input an integer
  209.       {
  210.         System.out.println("Please enter an integer!");
  211.         selection = -1;
  212.       }
  213.  
  214.       if ((selection < 1) | (selection > 10)) // if the user input is outside the range prsent them with an error message
  215.         System.out.print("Please input a number from 1 to 10: ");
  216.  
  217.     }while(!(selection > 0) | !(selection < 11));
  218.  
  219.     return selection; // returns the user input
  220.   }
  221.  
  222.   // Search again method
  223.   public static char getSearchInput(Scanner keyboard)
  224.   {
  225.     char search; // Character for user input
  226.     do // Loops while user input != Y or N
  227.     {
  228.       search = (keyboard.nextLine()).charAt(0);
  229.  
  230.       if ((Character.toUpperCase(search) != 'Y') && (Character.toUpperCase(search) != 'N'))
  231.         System.out.println("Please enter Y or N!");
  232.  
  233.     }while((Character.toUpperCase(search) != 'Y') & (Character.toUpperCase(search) != 'N'));
  234.  
  235.     return search; // returns user input to main
  236.   }
  237.  
  238.   // Search Function - Searches database based on user's desired search element
  239.   public static void searchDB(awardMovie[] list, Scanner key)
  240.   {
  241.     String input; // user input
  242.     int selection = -1; // Users choice what characteristic of the movie they would like to search by
  243.     int yearOfAward; // used to convert user input to an integer
  244.  
  245.     System.out.println("\n\nWhat characteristic would you like to search the database by?:");
  246.     System.out.println("1) Title");
  247.     System.out.println("2) Director");
  248.     System.out.println("3) Star of the Movie");
  249.     System.out.println("4) Rating");
  250.     System.out.println("5) Genre");
  251.     System.out.println("6) Year award was given");
  252.     System.out.println("7) Name of the Award");
  253.     System.out.print("\nInput Choice: ");
  254.  
  255.     do // Loops while user's input is not in the range of 1-7
  256.     {
  257.       input = key.nextLine();
  258.       try
  259.       {
  260.         selection = Integer.parseInt(input);
  261.       }
  262.       catch (NumberFormatException e) // if what is inputted is not an integer
  263.       {
  264.         System.out.print("Please enter an integer!: ");
  265.         selection = -1;
  266.       }
  267.  
  268.       if ((selection < 1) | (selection > 7))
  269.         System.out.println("Please input a number from 1 to 10");
  270.  
  271.     }while(!(selection > 0) | !(selection < 8));
  272.  
  273.     // If the user wants to search by title
  274.     if (selection == 1)
  275.     {
  276.       System.out.print("Enter the Title you wish to search for: ");
  277.       input = key.nextLine();
  278.       System.out.println("The following movies satisfy your search: \n");
  279.  
  280.       for (int i = 0; i < 10; i++)
  281.       {
  282.         if ((list[i].getTitle().equalsIgnoreCase(input)))
  283.           System.out.println(list[i].getTitle());
  284.       }
  285.     }
  286.     // If the user wants to search by Director
  287.     else if (selection == 2)
  288.     {
  289.       System.out.print("Enter the Director you wish to search for: ");
  290.       input = key.nextLine();
  291.       System.out.println("The following movies satisfy your search: \n");
  292.  
  293.       for (int i = 0; i < 10; i++)
  294.       {
  295.         if ((list[i].getDirector().equalsIgnoreCase(input)))
  296.           System.out.println(list[i].getTitle());
  297.       }
  298.     }
  299.     // If the user wants to search by Star
  300.     else if (selection == 3)
  301.     {
  302.       System.out.print("Enter the Star you wish to search for: ");
  303.       input = key.nextLine();
  304.       System.out.println("The following movies satisfy your search: \n");
  305.  
  306.       for (int i = 0; i < 10; i++)
  307.       {
  308.         if ((list[i].getStar().equalsIgnoreCase(input)))
  309.           System.out.println(list[i].getTitle());
  310.       }
  311.     }
  312.     // If the user wants to search by rating
  313.     else if (selection == 4)
  314.     {
  315.       System.out.print("Enter the Rating you wish to search for: ");
  316.       input = key.nextLine();
  317.       System.out.println("The following movies satisfy your search: \n");
  318.  
  319.       for (int i = 0; i < 10; i++)
  320.       {
  321.         if ((list[i].getRating().equalsIgnoreCase(input)))
  322.           System.out.println(list[i].getTitle());
  323.       }
  324.     }
  325.     // if the user wants to search by genre
  326.     else if (selection == 5)
  327.     {
  328.       System.out.print("Enter the Genre you wish to search for: ");
  329.       input = key.nextLine();
  330.       System.out.println("The following movies satisfy your search: \n");
  331.  
  332.       for (int i = 0; i < 10; i++)
  333.       {
  334.         if ((list[i].getGenre().equalsIgnoreCase(input)))
  335.           System.out.println(list[i].getTitle());
  336.       }
  337.     }
  338.     // if the user wants to search by year an award was given
  339.     else if (selection == 6)
  340.     {
  341.       System.out.print("Enter the Year of Award you wish to search for: ");
  342.       do
  343.       {
  344.         input = key.nextLine();
  345.         try
  346.         {
  347.           yearOfAward = Integer.parseInt(input);
  348.         }
  349.         catch (NumberFormatException e) // if the user does not input a year in the form of an integer
  350.         {
  351.           System.out.println("Please enter a year in number format!");
  352.           yearOfAward = 0;
  353.         }
  354.       }while(!(yearOfAward != 0));
  355.       System.out.println("The following movies satisfy your search: \n");
  356.  
  357.       for (int i = 0; i < 10; i++)
  358.       {
  359.         if ((list[i].getAwardYear()) == yearOfAward)
  360.           System.out.println(list[i].getTitle());
  361.       }
  362.     }
  363.     // if the user wants to search Award Title
  364.     else if (selection == 7)
  365.     {
  366.       System.out.print("Enter the Award Title you wish to search for: ");
  367.       input = key.nextLine();
  368.       System.out.println("The following movies satisfy your search: \n");
  369.  
  370.       for (int i = 0; i < 10; i++)
  371.       {
  372.         if ((list[i].getAwardTitle().equalsIgnoreCase(input)))
  373.           System.out.println(list[i].getTitle());
  374.       }
  375.     }
  376.     System.out.print("\n");
  377.  
  378.   }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement