Advertisement
SageTheWizard

jMDB.java

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