Advertisement
Guest User

Test

a guest
Jan 28th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.45 KB | None | 0 0
  1. import java.util.Scanner;   //Used in Driver file
  2. //import java.io.InputStream;
  3. //Movie lass file
  4. public class Movie{
  5.     private int id;
  6.     private String name;
  7.     private String genre;
  8.     private int duration;
  9.     private String ageRating;
  10.     private String description;
  11.  
  12.     public Movie(){
  13.         id = 0;
  14.         name = "NULL";
  15.         genre = "NULL";
  16.         duration = 0;
  17.         ageRating = "NULL";
  18.         description = "NULL";
  19.     }
  20.  
  21.     public Movie(int idNo, String na, String gen, int dur, String ageR, String desc){
  22.         id = idNo;
  23.         name = na;
  24.         genre = gen;
  25.         duration = dur;
  26.         ageRating = ageR;
  27.         description = desc;
  28.     }
  29.  
  30.     public void setId(int idNo){
  31.         id = idNo;
  32.     }
  33.  
  34.     public void setName(String na){
  35.         name = na;
  36.     }
  37.  
  38.     public String getName(){
  39.         return name;
  40.     }
  41.  
  42.     public void setGenre(String gen){
  43.         genre = gen;
  44.     }
  45.  
  46.     public String getGenre(){
  47.         return genre;
  48.     }
  49.  
  50.     public void setDuration(int dur){
  51.         duration = dur;
  52.     }
  53.  
  54.     public void setAgeRating(String ageR){
  55.         ageRating = ageR;
  56.     }
  57.  
  58.     public String getAgeRating(){
  59.         return ageRating;
  60.     }
  61.  
  62.     public void setDescription(String desc){
  63.         description = desc;
  64.     }
  65.  
  66.     public void displayRow(){
  67.         System.out.printf("%-1s %-3s %-50s %-23s %-8s %-1s %n", "|", id, name, genre, ageRating, "|");
  68.     }
  69.  
  70.     public void displayDetails(){
  71.         System.out.println("Movie Id : " + id);
  72.         System.out.println("Movie Name : " + name);
  73.         System.out.println("Genre : " + genre);
  74.         System.out.println("Duration : " + duration);
  75.         System.out.println("Age Rating : " + ageRating);
  76.         System.out.println("Description : \n" + description);
  77.     }
  78.  
  79. //////////////////////////////////////////////////////////////////////////////////////////////
  80. //////////////////////////////////////////////////////////////////////////////////////////////
  81.  
  82.     //In Functions File
  83.     //Global Variable
  84.     public static int size = 10;    //Size of array/maximum number of movies that can be recorded
  85.  
  86.     public static int total = 5;
  87.  
  88.     public static Movie movies[] = new Movie[size]; //array of Movie class object
  89.  
  90.     public static int index = 0;    //index of movie
  91.  
  92.     public static Scanner input = new Scanner(System.in);
  93.  
  94.     public static String pause; //system("Pause") / getch() dummy (OR variable used to pause wait and for user to enter)
  95.  
  96.     //Initialise array of objects
  97.     public static void initialiseArray(){
  98.         for(int i = 0; i<size; i++){
  99.             movies[i] = new Movie();
  100.         }
  101.     }
  102.  
  103.     //fill 5 objects with data/details aka HARDCODED OBJECTS
  104.     public static void fillObjects(){
  105.         movies[0] = new Movie(1, "Frozen 2", "Adventure", 103, "PG", "Anna, Elsa, Kristoff, Olaf and Sven leave Arendelle to travel to an ancient, autumn-bound forest of an enchanted land. They set out to find the origin of Elsa's powers in order to save their kingdom.");
  106.         movies[1] = new Movie(2, "The Avengers", "Action", 143, "PG-13", "Earth's mightiest heroes must come together and learn to fight as a team if they are going to stop the mischievous Loki and his alien army from enslaving humanity.");
  107.         movies[2] = new Movie(3, "Annabelle", "Horror", 99, "R", "A couple begins to experience terrifying supernatural occurrences involving a vintage doll shortly after their home is invaded by satanic cultists.");
  108.         movies[3] = new Movie(4, "BoiBoiBoy: The Movie", "Action", 100, "G", "The movie follows BoBoiBoy and his friends on an adventure on a mysterious island to find Ochobot. The Ochobot has been kidnapped by a group of alien treasure hunters so that they could locate an ancient Power Sphere older than Ochobot. The quest leads BoBoiBoy to meet his toughest foe yet, an alien treasure hunter who is looking to harness the power from this sphere for his greedy needs.");
  109.         movies[4] = new Movie(5, "Titanic", "Romance", 194, "PG-13", "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.");
  110.     }
  111.  
  112.     //Call main method
  113.     static void mainCaller(){
  114.         main(null);
  115.     }
  116.  
  117.     //Clear Command Line Console
  118.     public static void clearScreen(){  
  119.         System.out.print("\033[H\033[2J");  
  120.         System.out.flush();  
  121.     }
  122.  
  123.     public static void tableTop(){
  124.         //Table Border
  125.         for(int i = 0; i<91; i++){
  126.             System.out.print("=");
  127.         }
  128.  
  129.         System.out.println("");
  130.  
  131.         //Table Header
  132.         System.out.printf("%-1s %-3s %-50s %-20s %-11s %-1s %n", "|", "ID", "Name", "Genre","Age Rating", "|");
  133.        
  134.         //Table Divider
  135.         System.out.print("|");
  136.         for(int i = 0; i<89; i++){
  137.             System.out.print("-");
  138.         }
  139.         System.out.println("|");
  140.     }
  141.  
  142.     public static void tableBottom(){
  143.         //Table Bottom Border
  144.         for(int i = 0; i<91; i++){
  145.             System.out.print("=");
  146.         }
  147.  
  148.         System.out.println("");
  149.     }
  150.  
  151.     //Main Menu Option 4 : View
  152.     public static void View(){
  153.         char choice;
  154.         System.out.println("*--------------------<View>--------------------*");
  155.         System.out.println(" 1. View all.....................(Table Format)");
  156.         System.out.println(" 2. List all, given Genre........(Table Format)");
  157.         System.out.println(" 3. List all, given Age Rating...(Table Format)");
  158.         System.out.println(" 4. Search by Name............(Detailed Format)");
  159.         System.out.println(" 0. Go Back......................(To Main Menu)");
  160.         System.out.println("*--------------------<****>--------------------*\n");
  161.  
  162.         System.out.print("Please enter your choice : ");
  163.         choice = input.next().charAt(0);
  164.  
  165.         clearScreen();
  166.  
  167.          switch(choice){
  168.             //View All
  169.             case '1':
  170.                 //Table Name
  171.                 System.out.printf("%49s %n","View All");
  172.  
  173.                 viewAll();
  174.  
  175.                 break;
  176.  
  177.             //List all by Genre
  178.             case '2':
  179.                 String genre;
  180.  
  181.                 do{
  182.                     //ERROR : genreSelection() wont return a genre value after a invalid choice is entered
  183.                     genre = genreSelection();
  184.                     System.out.println(genre + " haha");    //Temporary test
  185.  
  186.                     if(searchGenre(genre)){
  187.                         //Table Name
  188.                         System.out.printf("%49s %n",genre + " Movie(s)");
  189.  
  190.                         tableTop();
  191.  
  192.                         //Search and display movies with the chosen genre
  193.                         givenGenre(genre, total-1);
  194.  
  195.                         tableBottom();
  196.                     }else{
  197.                         System.out.println("There are no " + genre + " movies.\n");
  198.  
  199.                         //Pause screen before clearing
  200.                         System.out.print("\nPress enter to continue...");
  201.                         pause = input.nextLine();
  202.                         pause = input.nextLine();
  203.  
  204.                         clearScreen();
  205.                     }
  206.                 }while(!searchGenre(genre));
  207.  
  208.                 break;
  209.  
  210.             //List all by Age Rating
  211.             case '3':
  212.                 String ageRating;
  213.                 do{
  214.                     ageRating = ageRatingSelection();
  215.  
  216.                     if(searchAgeRating(ageRating)){
  217.                         //Table Name
  218.                         System.out.printf("%49s %n",ageRating + " Movie(s)");
  219.  
  220.                         tableTop();
  221.  
  222.                         //Search and display movies with the chosen Age Rating
  223.                         givenAgeRating(ageRating, total-1);
  224.  
  225.                         tableBottom();
  226.                     }else{
  227.                         System.out.println("There are no " + ageRating + " movies.\n");
  228.  
  229.                         //Pause screen before clearing
  230.                         System.out.print("\nPress enter to continue...");
  231.                         pause = input.nextLine();
  232.                         pause = input.nextLine();
  233.  
  234.                         clearScreen();
  235.                     }
  236.                 }while(!searchAgeRating(ageRating));
  237.  
  238.                 break;
  239.  
  240.             //Search by Name
  241.             //CANNOT FIND
  242.             case '4':
  243.                 String name;
  244.                 input.skip("\n");
  245.                 System.out.println("Please enter the name of the movie : ");
  246.                 name = input.nextLine();
  247.                 if(searchName(name) == -1){
  248.                     System.out.println("There are no movies named " + name + ".\n");
  249.  
  250.                     //Pause screen before clearing
  251.                     System.out.print("\nPress enter to continue...");
  252.                     pause = input.nextLine();
  253.  
  254.                     clearScreen();
  255.                     View();
  256.  
  257.                 }else{
  258.                     movies[searchName(name)].displayDetails();
  259.  
  260.                     //Pause screen before clearing
  261.                     System.out.print("\nPress enter to continue...");
  262.                     pause = input.nextLine();
  263.                     pause = input.nextLine();
  264.  
  265.                     clearScreen();
  266.                     mainCaller();
  267.                 }
  268.  
  269.                 break;
  270.  
  271.             //Go Back to Main Menu
  272.             case '0':
  273.                 mainCaller();
  274.  
  275.             default:
  276.                 System.out.println("'" + choice + "' is an invalid choice.\n");
  277.                 //input.skip("\n");
  278.                 View();
  279.          }
  280.  
  281.         //Pause screen before clearing
  282.         System.out.print("\nPress enter to continue...");
  283.         pause = input.nextLine();
  284.         pause = input.nextLine();
  285.  
  286.         clearScreen();
  287.  
  288.         mainCaller();
  289.  
  290.     }
  291.  
  292.     //View Menu Option 1 : View All
  293.     public static void viewAll(){
  294.         tableTop();
  295.  
  296.         //Display each row of recorded movies
  297.         for(int i = 0; i<size; i++){
  298.             movies[i].displayRow();
  299.         }
  300.  
  301.         tableBottom();
  302.     }
  303.  
  304.     //View Menu Option 2 : List all, given Genre
  305.     //Selecting genre
  306.     static String genreSelection(){
  307.         char choice;
  308.         String genre = "";
  309.         System.out.println("*-------------------<Genres>-------------------*");
  310.         System.out.println(" 1. Action          2. Adventure");
  311.         System.out.println(" 3. Comedy          4. Drama");
  312.         System.out.println(" 5. Horror          6. Sci-Fi");
  313.         System.out.println(" 7. Romance         8. Others");
  314.         System.out.println(" 0. Go Back......................(To View Menu)");
  315.         System.out.println("*-------------------<******>-------------------*\n");
  316.  
  317.         System.out.print("Please enter your choice : ");
  318.  
  319.         choice = input.next().charAt(0);
  320.  
  321.         switch(choice){
  322.             case '1':
  323.                 genre = "Action";
  324.                 break;
  325.  
  326.             case '2':
  327.                 genre = "Adventure";
  328.                 break;
  329.  
  330.             case '3':
  331.                 genre = "Comedy";
  332.                 break;
  333.  
  334.             case '4':
  335.                 genre = "Drama";
  336.                 break;
  337.  
  338.             case '5':
  339.                 genre = "Horror";
  340.                 break;
  341.  
  342.             case '6':
  343.                 genre = "Sci-Fi";
  344.                 break;
  345.  
  346.             case '7':
  347.                 genre = "Romance";
  348.                 break;
  349.  
  350.             case '8':
  351.                 genre = "Others";
  352.                 break;
  353.  
  354.             case '0':
  355.                 clearScreen();
  356.                 View();
  357.                 break;
  358.  
  359.             default:
  360.                 clearScreen();
  361.                 System.out.print("'" + choice + "' is an invalid choice.\n\n");
  362.                 genreSelection();
  363.                 break;
  364.         }
  365.  
  366.         return genre;
  367.     }
  368.  
  369.     //Check if there is any movie with the same Genre
  370.     static boolean searchGenre(String genre){
  371.         for(int i = 0; i < size; i++){
  372.             //found
  373.             if(genre == movies[i].getGenre()){
  374.                 return true;
  375.             }
  376.         }
  377.         //not found
  378.         return false;
  379.     }
  380.  
  381.     //Display movies that has the same Genre
  382.     static void givenGenre(String genre, int high){
  383.         if (high < 0){
  384.           return;
  385.         }
  386.         else if (genre == movies[high].getGenre() ){
  387.           movies[high].displayRow();
  388.           givenGenre(genre, high-1);
  389.         }
  390.         else {
  391.           givenGenre(genre, high-1);
  392.         }
  393.     }
  394.  
  395.     //View Menu Option 3 : List all, given Age Rating
  396.     //Selecting age rating
  397.     static String ageRatingSelection(){
  398.         char choice;
  399.         String ageRating = "";
  400.         System.out.println("*-----------------<Age Rating>-----------------*");
  401.         System.out.println(" 1. G               2. PG");
  402.         System.out.println(" 3. PG-13           4. R");
  403.         System.out.println(" 0. Go Back......................(To View Menu)");
  404.         System.out.println("*-----------------<**********>-----------------*\n");
  405.  
  406.         System.out.print("Please enter your choice : ");
  407.  
  408.         choice = input.next().charAt(0);
  409.  
  410.         switch(choice){
  411.             case '1':
  412.                 ageRating = "G";
  413.                 break;
  414.  
  415.             case '2':
  416.                 ageRating = "PG";
  417.                 break;
  418.  
  419.             case '3':
  420.                 ageRating = "PG-13";
  421.                 break;
  422.  
  423.             case '4':
  424.                 ageRating = "R";
  425.                 break;
  426.  
  427.             case '0':
  428.                 clearScreen();
  429.                 View();
  430.                 break;
  431.  
  432.             default:
  433.                 clearScreen();
  434.                 System.out.print("'" + choice + "' is an invalid choice.\n\n");
  435.                 ageRatingSelection();
  436.                 break;
  437.         }
  438.  
  439.         return ageRating;
  440.     }
  441.  
  442.     //Check if there is any movie with the same Age Rating
  443.     static boolean searchAgeRating(String ageRating){
  444.         for(int i = 0; i < size; i++){
  445.             //found
  446.             if(ageRating == movies[i].getAgeRating()){
  447.                 return true;
  448.             }
  449.         }
  450.         //not found
  451.         return false;
  452.     }
  453.  
  454.     //Display movies that has the same AgeRating
  455.     static void givenAgeRating(String ageRating, int high){
  456.         if (high < 0){
  457.           return;
  458.         }
  459.         else if (ageRating == movies[high].getAgeRating() ){
  460.           movies[high].displayRow();
  461.           givenAgeRating(ageRating, high-1);
  462.         }
  463.         else {
  464.           givenAgeRating(ageRating, high-1);
  465.         }
  466.     }
  467.  
  468.     //Check if there is any movie with the same Name
  469.     static int searchName(String name){
  470.         for(int i = 0; i < size; i++){
  471.             //found
  472.             if(name == movies[i].getName()){
  473.                 return i;
  474.             }
  475.         }
  476.         //not found
  477.         return -1;
  478.     }
  479.  
  480. //////////////////////////////////////////////////////////////////////////////////////////////
  481. //////////////////////////////////////////////////////////////////////////////////////////////
  482.  
  483.     public static void main(String[] args){
  484.         //Create input object of Scanner class
  485.        
  486.         String dummyS;
  487.         int dummyI;
  488.  
  489.         //Initialise array of objects of Movie class
  490.         if(index == 0){
  491.             initialiseArray();
  492.         }
  493.        
  494.         fillObjects();
  495.  
  496.         View();
  497.     }
  498. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement