Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Jacob Gallucci
- E-mail: jag6248@psu.edu
- Course: CMPSC 221
- Assignment: Programming Assignment 2 - Movie Database - Main
- Due date: 2/27/2018
- File: jMDB.java
- Purpose: Driver for jMDB movie database: Gives the user the ability to view the different movies in the Database
- Also contains ... Sorting Methods, Searching method to search the database, Display methods to display the movie list
- setup database method and getInput methods
- Compiler/IDE: OpenJDK 1.8.0_151 (compiled from CLI) - Atom / Vim text editors
- Operating system: Debian Stretch 9
- Reference(s):
- I had a bubble sort program from HACC I referenced to get the alphabettical sort
- https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html - Reference for exceptions
- */
- import java.io.*;
- import java.util.*;
- public class jMDB
- {
- public static void main(String[] args)
- {
- awardMovie[] movies = new awardMovie[10]; // Array of awardMovies
- Scanner keyboard = new Scanner(System.in); // Scanner for user input
- String sortTypeInput;
- int sortType; // Input (1 or 2) for how the user wishes to sort the movie titles
- int choice = -1; // Loop controller
- char search; // Yes or no if the user wants to search the database
- char goAgain; // Yes or no if the user wants to do another search / view an element of the DB
- for (int i = 0; i < 10; i++) // Initializing the array
- movies[i] = new awardMovie();
- setupMovies(movies); // Calls function that reads data from a text file to input into the array's objects
- System.out.println("Welcome to jMDB! Jacob Gallucci's knock off Movie Database!\n");
- do
- {
- System.out.println("How would you like to sort the Award Winning Movies?\n 1: By Title (alphabetically)\n 2: By Genre (alphabetically)\n");
- System.out.print("Input an option from above: ");
- try
- {
- sortTypeInput = keyboard.nextLine();
- sortType = Integer.parseInt(sortTypeInput);
- }
- catch(NumberFormatException ex)
- {
- System.out.println("Error Please enter an Integer!");
- sortType = -1;
- }
- }while((sortType != 1) & (sortType != 2));
- if (sortType == 0)
- sortAlphaTitle(movies); // Sorts list alphabetically
- else
- sortAlphaGenre(movies);
- do // Go Again Loop - Loops while the user wants to remain in the program
- {
- displayTitles(movies, sortType); // Displays menu of movies
- do // Search Loop - Loops while the user wants to search the database
- {
- System.out.print("Would you like to search the database?(Y/N): ");
- search = getSearchInput(keyboard); // Gets search Yes or No
- if (Character.toUpperCase(search) == 'Y') // if the user wants to search the database
- {
- searchDB(movies, keyboard); // Searches the database
- displayTitles(movies, sortType); // Displays list of movies again
- }
- }while(Character.toUpperCase(search) == 'Y');
- choice = getInput(keyboard); // User enters their movie of choice to learn more about
- System.out.println(movies[(choice - 1)]); // Prints information about the movie
- System.out.print("Would you like to go again?(Y/N): ");
- goAgain = getSearchInput(keyboard); // Gets user input whether they want to go again
- }while(Character.toUpperCase(goAgain) == 'Y');
- System.out.println("Thank you for using jMDB!");
- System.exit(0); // end program
- }
- //////////////////////////////////////////////////////////////////////////METHODS////////////////////////////////////////////////
- private static awardMovie[] setupMovies(awardMovie[] list) // Sets up movie array
- {
- String dummyString; // String used for input
- int i = 0; // Loop controller
- int dummyInt; // String used to convert input to an integer
- try
- {
- FileReader reader = new FileReader("movies.txt"); // Sets up file reader
- BufferedReader buffReader = new BufferedReader(reader); // Sets up buffer reader
- // this loop inputs data from file into awardString object while there is data in the file or integer i is less than 10
- /*
- * 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)
- */
- while (((dummyString = buffReader.readLine()) != null) || (i < 10))
- {
- list[i].setTitle(dummyString);
- dummyString = buffReader.readLine();
- list[i].setRating(dummyString);
- dummyString = buffReader.readLine();
- list[i].setGenre(dummyString);
- dummyString = buffReader.readLine();
- list[i].setDirector(dummyString);
- dummyString = buffReader.readLine();
- list[i].setStar(dummyString);
- dummyString = buffReader.readLine();
- list[i].setAwardTitle(dummyString);
- dummyInt = Integer.parseInt(buffReader.readLine());
- list[i].setAwardYear(dummyInt);
- i++;
- }
- }
- // If the file is not found
- catch(FileNotFoundException ex)
- {
- System.out.println("File not found!");
- }
- // If the file is broken
- catch(IOException ex)
- {
- System.out.println("File Broken! Pls Fix!");
- }
- return list; // Returns setup array to the main
- }
- // Uses a bubble sort to sort titles alphabettically from #->Z
- public static awardMovie[] sortAlphaTitle(awardMovie[] list)
- {
- awardMovie temp = new awardMovie(); // Temporary awardMovie object used to swap
- for (int i = 0; i < 9; i++)
- {
- for (int j = 0; j < (8 - i); j++)
- {
- if (((list[j].getTitle()).compareToIgnoreCase((list[j+1].getTitle()))) > 0)
- {
- temp = list[j];
- list[j] = list[j+1];
- list[j+1] = temp;
- }
- }
- }
- return list; // Returns sorted list to the main
- }
- // Uses bubble sort to sort genres alphabetically if genres are the same, it sorts it by title alphabetically
- public static awardMovie[] sortAlphaGenre(awardMovie[] list)
- {
- awardMovie temp = new awardMovie(); // Temporary awardMovie object used to swap
- for (int i = 0; i < 9; i++)
- {
- for (int j = 0; j < (8 - i); j++)
- {
- if (((list[j].getGenre()).compareToIgnoreCase((list[j+1].getGenre()))) > 0)
- {
- temp = list[j];
- list[j] = list[j+1];
- list[j+1] = temp;
- }
- else if (((list[j].getGenre()).compareToIgnoreCase((list[j+1].getGenre()))) == 0)
- {
- if (((list[j].getTitle()).compareToIgnoreCase((list[j+1].getTitle()))) > 0)
- {
- temp = list[j];
- list[j] = list[j+1];
- list[j+1] = temp;
- }
- }
- }
- }
- return list; // Returns sorted list to the main
- }
- // Displays titles of the movies depending on how the user chooses they wish to see the movies
- public static void displayTitles(awardMovie[] list, int type)
- {
- if (type == 1) // if user wants the movies alphabetically by title
- {
- for (int i = 0; i < 10; i++)
- System.out.println((i + 1) + ": " + list[i].getTitle());
- }
- else // if the user wants the movies alphabetically by genre
- {
- for (int i = 0; i < 10; i++)
- System.out.println((i + 1) + ": [" + list[i].getGenre() + "] " + list[i].getTitle());
- }
- return;
- }
- // Gets user input as it relates to the movie the user wants to view
- public static int getInput(Scanner key)
- {
- String input; // user input
- int selection = -1; // user input converted to integer
- System.out.print("\nEnter the number that corrisponds to the movie you wish to view in detail: ");
- do // Loops while the user's input is not between 1 and 10
- {
- input = key.nextLine();
- try
- {
- selection = Integer.parseInt(input);
- }
- catch (NumberFormatException e) // if the user does not input an integer
- {
- System.out.println("Please enter an integer!");
- selection = -1;
- }
- if ((selection < 1) | (selection > 10)) // if the user input is outside the range prsent them with an error message
- System.out.print("Please input a number from 1 to 10: ");
- }while(!(selection > 0) | !(selection < 11));
- return selection; // returns the user input
- }
- // Search again method
- public static char getSearchInput(Scanner keyboard)
- {
- char search; // Character for user input
- do // Loops while user input != Y or N
- {
- search = (keyboard.nextLine()).charAt(0);
- if ((Character.toUpperCase(search) != 'Y') && (Character.toUpperCase(search) != 'N'))
- System.out.println("Please enter Y or N!");
- }while((Character.toUpperCase(search) != 'Y') & (Character.toUpperCase(search) != 'N'));
- return search; // returns user input to main
- }
- // Search Function - Searches database based on user's desired search element
- public static void searchDB(awardMovie[] list, Scanner key)
- {
- String input; // user input
- int selection = -1; // Users choice what characteristic of the movie they would like to search by
- int yearOfAward; // used to convert user input to an integer
- System.out.println("\n\nWhat characteristic would you like to search the database by?:");
- System.out.println("1) Title");
- System.out.println("2) Director");
- System.out.println("3) Star of the Movie");
- System.out.println("4) Rating");
- System.out.println("5) Genre");
- System.out.println("6) Year award was given");
- System.out.println("7) Name of the Award");
- System.out.print("\nInput Choice: ");
- do // Loops while user's input is not in the range of 1-7
- {
- input = key.nextLine();
- try
- {
- selection = Integer.parseInt(input);
- }
- catch (NumberFormatException e) // if what is inputted is not an integer
- {
- System.out.print("Please enter an integer!: ");
- selection = -1;
- }
- if ((selection < 1) | (selection > 7))
- System.out.println("Please input a number from 1 to 10");
- }while(!(selection > 0) | !(selection < 8));
- // If the user wants to search by title
- if (selection == 1)
- {
- System.out.print("Enter the Title you wish to search for: ");
- input = key.nextLine();
- System.out.println("The following movies satisfy your search: \n");
- for (int i = 0; i < 10; i++)
- {
- if ((list[i].getTitle().equalsIgnoreCase(input)))
- System.out.println(list[i].getTitle());
- }
- }
- // If the user wants to search by Director
- else if (selection == 2)
- {
- System.out.print("Enter the Director you wish to search for: ");
- input = key.nextLine();
- System.out.println("The following movies satisfy your search: \n");
- for (int i = 0; i < 10; i++)
- {
- if ((list[i].getDirector().equalsIgnoreCase(input)))
- System.out.println(list[i].getTitle());
- }
- }
- // If the user wants to search by Star
- else if (selection == 3)
- {
- System.out.print("Enter the Star you wish to search for: ");
- input = key.nextLine();
- System.out.println("The following movies satisfy your search: \n");
- for (int i = 0; i < 10; i++)
- {
- if ((list[i].getStar().equalsIgnoreCase(input)))
- System.out.println(list[i].getTitle());
- }
- }
- // If the user wants to search by rating
- else if (selection == 4)
- {
- System.out.print("Enter the Rating you wish to search for: ");
- input = key.nextLine();
- System.out.println("The following movies satisfy your search: \n");
- for (int i = 0; i < 10; i++)
- {
- if ((list[i].getRating().equalsIgnoreCase(input)))
- System.out.println(list[i].getTitle());
- }
- }
- // if the user wants to search by genre
- else if (selection == 5)
- {
- System.out.print("Enter the Genre you wish to search for: ");
- input = key.nextLine();
- System.out.println("The following movies satisfy your search: \n");
- for (int i = 0; i < 10; i++)
- {
- if ((list[i].getGenre().equalsIgnoreCase(input)))
- System.out.println(list[i].getTitle());
- }
- }
- // if the user wants to search by year an award was given
- else if (selection == 6)
- {
- System.out.print("Enter the Year of Award you wish to search for: ");
- do
- {
- input = key.nextLine();
- try
- {
- yearOfAward = Integer.parseInt(input);
- }
- catch (NumberFormatException e) // if the user does not input a year in the form of an integer
- {
- System.out.println("Please enter a year in number format!");
- yearOfAward = 0;
- }
- }while(!(yearOfAward != 0));
- System.out.println("The following movies satisfy your search: \n");
- for (int i = 0; i < 10; i++)
- {
- if ((list[i].getAwardYear()) == yearOfAward)
- System.out.println(list[i].getTitle());
- }
- }
- // if the user wants to search Award Title
- else if (selection == 7)
- {
- System.out.print("Enter the Award Title you wish to search for: ");
- input = key.nextLine();
- System.out.println("The following movies satisfy your search: \n");
- for (int i = 0; i < 10; i++)
- {
- if ((list[i].getAwardTitle().equalsIgnoreCase(input)))
- System.out.println(list[i].getTitle());
- }
- }
- System.out.print("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement