Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.88 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Store {
  3.  
  4.     //array for Videos space allocated = 100
  5.     static Video[] videos;
  6.     //array for Customers space allocated = 3
  7.     static Customer[] customers;
  8.     static Scanner keyboard;
  9.  
  10.     public static void main(String[] args) {
  11.  
  12.         videos = new Video[100];
  13.  
  14.         //populate the array for Videos
  15.         videos[0] = new DVD ("Star Wars", 3.5, "Lucas");
  16.         videos[1] = new DVD ("Death Note", 2.5, "Tarantino");
  17.         videos[2] = new VCD ("Death Note", 1.5, "A", 2006);
  18.         videos[3] = new DVD ("Shrek", 2.0, "Agogo");
  19.         videos[4] = new VHS ("Transformers", 3.25);
  20.         videos[5] = new VHS ("The Eye", 3.0);
  21.         videos[6] = new VCD ("Fantastic 4", 2.0, "B", 2004);
  22.         videos[7] = new VCD ("Shampoo", 2.0, "B", 2007);
  23.         videos[8] = new DVD ("Bourne Ultimatum", 3.0,"Ang Lee");
  24.         videos[9] = new VCD ("Bourne Ultimatum", 3.0, "A", 2007);
  25.         videos[10] = new VHS ("Bourne Ultimatum", 3.0);
  26.  
  27.         customers = new Customer[100];
  28.  
  29.         //populate the array for Customers
  30.         customers[0] = new Customer("Lighto Yagami", "012-888-9808", 23, 0);
  31.         customers[1] = new Customer("L.Lawliet", "011-222-3333", 24, 0 );
  32.         customers[2] = new Customer("Misa Misa", "020-452-5656", 21, 0);
  33.  
  34.         //Declarations
  35.         keyboard = new Scanner(System.in);
  36.         boolean quit = false;                       //flag for do while loop
  37.         String option;
  38.         String movieTitle;
  39.         double movieLength;
  40.         String customerName;
  41.         String mediumType;
  42.         String directorName;
  43.         String movieRating;
  44.         int movieYear;
  45.         int indexDVD = -1;
  46.         int indexVCD = -1;
  47.         int indexVHS = -1;
  48.         int customerIndex = -1;
  49.  
  50.  
  51.         do{
  52.             //Menu for the user
  53.             System.out.println("a)  Renting a DVD, VCD, or VHS");
  54.             System.out.println("b)  Returning a DVD, VCD or VHS");
  55.             System.out.println("c)  Buy a video");
  56.             System.out.println("d)  Print out customer information");
  57.             System.out.println("e)  Add a video");
  58.             System.out.println("f)  Print out information for all the DVDs.");
  59.             System.out.println("g)  Exit Program");
  60.             option = keyboard.nextLine();
  61.  
  62.             //See the user key in which option
  63.             // Rent a video
  64.             if (option.equals("a") || option.equals("A")) {
  65.                 System.out.println("Please enter the title of Video");
  66.                 movieTitle = keyboard.nextLine();
  67.                 for (int i=0; i<videos.length; i++) {
  68.                     if (videos[i] == null) {
  69.                         System.out.println("The video title could not be found");
  70.                         break;
  71.                     }
  72.                     if (videos[i].getTitle().toLowerCase().equals(movieTitle.toLowerCase())) {          //compare the title in lower case
  73.                         if (videos[i].getStatus() == 'I') {
  74.                                 if (videos[i] instanceof DVD) {
  75.                                     System.out.println("DVD");
  76.                                     indexDVD = i;
  77.                                 }
  78.                                 else if (videos[i] instanceof VCD) {
  79.                                     System.out.println("VCD");
  80.                                     indexVCD = i;
  81.                                 }
  82.                                 else if (videos[i] instanceof VHS) {
  83.                                     System.out.println("VHS");
  84.                                     indexVHS = i;
  85.                                 }
  86.                             System.out.print("Which medium type do you want?");
  87.                             mediumType = keyboard.nextLine();
  88.                             if (mediumType.equals("DVD") || (mediumType.equals("dvd")) &&  (indexDVD != -1) ) {
  89.                                 if (videos[indexDVD].getStatus() == 'O') {
  90.                                     System.out.println("Sorry, the DVD version of this video has been rented out.");
  91.                                     return;
  92.                                 }
  93.                                 else {
  94.                                     videos[indexDVD].setStatus('O');
  95.                                     System.out.println("Thank you. You have successfully rent this DVD.");
  96.                                     break;
  97.                                 }
  98.                             }
  99.                             else if (mediumType.equals("VCD") || (mediumType.equals("vcd")) && (indexVCD != -1)) {
  100.                                 if (videos[indexVCD].getStatus() == 'O') {
  101.                                     System.out.println("Sorry, the VCD version of this video has been rented out.");
  102.                                     return;
  103.                                 }
  104.                                 else {
  105.                                     videos[indexVCD].setStatus('O');
  106.                                     System.out.println("Thank you. You have successfully rent this VCD.");
  107.                                     break;
  108.                                 }
  109.                             }
  110.                             else if (mediumType.equals("VHS") || (mediumType.equals("vhs")) && (indexVHS != -1)) {
  111.                                 if (videos[indexVHS].getStatus() == 'O') {
  112.                                     System.out.println("Sorry, the VHS version of this video has been rented out.");
  113.                                     return;
  114.                                 }
  115.                                 else {
  116.                                     videos[indexVHS].setStatus('O');
  117.                                     System.out.println("Thank you. You have successfully rent this VHS.");
  118.                                     break;
  119.                                 }
  120.                             }
  121.                             else {
  122.                                 System.out.println("Sorry, the medium type you requested was not found.");
  123.                                 break;
  124.                             }
  125.                         }
  126.                     }
  127.                     else if (videos[i].getStatus() == 'O') {
  128.                         System.out.println("Sorry, the all medium type of this video title you requested to be rent has been rented out");
  129.                         break;
  130.                     }
  131.                     else {
  132.                         System.out.println("Sorry, the video title you requested was not found.");
  133.                         break;
  134.                     }
  135.                     return;
  136.  
  137.                 }
  138.             }
  139.             //Returning a video
  140.             else if (option.equals("b") || option.equals("B")) {
  141.                 System.out.println("Please enter the title of video");
  142.                 movieTitle = keyboard.nextLine();
  143.                 for (int i=0; i<100; i++) {
  144.                     if (videos[i] == null) {
  145.                         System.out.println("Sorry, the video title could not be found");
  146.                         break;
  147.                     }
  148.                     if (videos[i].getTitle().toLowerCase().equals(movieTitle.toLowerCase())) {
  149.                         if (videos[i].getStatus() == 'O') {
  150.                             if (videos[i] instanceof DVD) {
  151.                                 System.out.println("DVD");
  152.                                 indexDVD = i;
  153.                             }
  154.                             else if (videos[i] instanceof VCD) {
  155.                                 System.out.println("VCD");
  156.                                 indexVCD = i;
  157.                             }
  158.                             else if (videos[i] instanceof VHS) {
  159.                                 System.out.println("VHS");
  160.                                 indexVHS = i;
  161.                             }
  162.                             System.out.print("Which medium type do you want to return?");
  163.                             mediumType = keyboard.nextLine();
  164.                             if (mediumType.equals("DVD") && indexDVD != -1 ) {
  165.                                 if (videos[indexDVD].getStatus() == 'I') {
  166.                                     System.out.println("Sorry, the DVD version of this video is in store.");
  167.                                     return;
  168.                                 }
  169.                                 else {
  170.                                     videos[indexDVD].setStatus('I');
  171.                                     System.out.println("Thank you. You have successfully returned this DVD.");
  172.                                     break;
  173.                                 }
  174.                                 }
  175.                                 else if (mediumType.equals("VCD") && indexVCD != -1) {
  176.                                     if (videos[indexVCD].getStatus() == 'I') {
  177.                                         System.out.println("Sorry, the VCD version of this video is in store.");
  178.                                         return;
  179.                                     }
  180.                                     else {
  181.                                         videos[indexVCD].setStatus('I');
  182.                                         System.out.println("Thank you. You have successfully returned this VCD.");
  183.                                         break;
  184.                                     }
  185.                                 }
  186.                                 else if (mediumType.equals("VHS") && indexVHS != -1) {
  187.                                     if (videos[indexVHS].getStatus() == 'I') {
  188.                                         System.out.println("Sorry, the VHS version of this video is in store.");
  189.                                         return;
  190.                                     }
  191.                                     else {
  192.                                         videos[i].setStatus('I');
  193.                                         System.out.println("Thank you for choosing us. Please come again");
  194.                                         break;
  195.                                     }
  196.                                 }
  197.                         else {
  198.                             System.out.println("The video is in store. Please try again.");
  199.                             break;
  200.                         }
  201.                         }
  202.                     }
  203.                 }
  204.             }
  205.  
  206.             //Buying a video
  207.             else if (option.equals("c") || option.equals("C")) {
  208.  
  209.                 buyingMovie();
  210.  
  211.             } // end else if line 109
  212.  
  213.             //Print out customer information
  214.             else if (option.equals("d") || option.equals("D")) {
  215.                 System.out.println("Please enter customer name");
  216.                 customerName = keyboard.nextLine();
  217.                 for (int i=0; i<customers.length; i++) {
  218.                     if (customers[i] == null) {
  219.                         customerIndex = -1;
  220.                         break;
  221.                     }
  222.                     if (customers[i].getName().toLowerCase().equals(customerName.toLowerCase())) {
  223.                         customerIndex = i;
  224.                         break;
  225.                     }
  226.                     if (i == (customers.length-1)) {
  227.                         customerIndex = -1;
  228.                         break;
  229.                     }
  230.                 }
  231.                 if (customerIndex != -1) {
  232.                         System.out.println("Customer :" + customers[customerIndex].getName());
  233.                         System.out.println("Phone :" + customers[customerIndex].getPhoneNumber());
  234.                         System.out.println("Age : " + customers[customerIndex].getAge());
  235.                 }
  236.             }
  237.  
  238.             //Add a new video object
  239.             else if (option.equals("e") || option.equals("E")) {
  240.                 for (int i=0; i<videos.length; i++) {
  241.                     if (videos[i] == null) {
  242.                         System.out.println("Please enter the title of movie:");
  243.                         movieTitle = keyboard.nextLine();
  244.                         System.out.println("Please enter the length of the movie:");
  245.                         movieLength = Double.parseDouble( keyboard.nextLine() );
  246.                         System.out.println("Please enter the medium type of this movie:");
  247.                         mediumType = keyboard.nextLine();
  248.                         if (mediumType.equals("DVD") || mediumType.equals("dvd")) {
  249.                             System.out.println("Please enter director name:");
  250.                             directorName = keyboard.nextLine();
  251.                             videos[i] = new DVD (movieTitle, movieLength,directorName);
  252.                             System.out.println("You've successfully added new DVD video.");
  253.                         }
  254.                         else if (mediumType.equals("VCD") || mediumType.equals("vcd")) {
  255.                             System.out.println("Please enter movie rating:");
  256.                             movieRating = keyboard.nextLine();
  257.                             System.out.println("Please enter movie year:");
  258.                             movieYear = Integer.parseInt(keyboard.nextLine() );
  259.                             videos[i] =  new VCD (movieTitle, movieLength, movieRating, movieYear);
  260.                             System.out.println("You've successfully added new VCD");
  261.                         }
  262.                         else if (mediumType.equals("VHS") || mediumType.equals("vhs")) {
  263.                             videos[i] =  new VHS (movieTitle, movieLength);
  264.                             System.out.println("You've successfully added new VHS.");
  265.                         }
  266.                         break;
  267.                     }
  268.                 }
  269.             }
  270.  
  271.             //Print all information about video
  272.             else if (option.equals("f") || option.equals("f")) {
  273.                 for (int i = 0; i <videos.length; i++) {
  274.                     if (videos[i] == null) {
  275.                         break;
  276.                     }
  277.                     videos[i].print();
  278.                 }
  279.             }
  280.  
  281.             //Exit the program
  282.             else if (option.equals("g") || option.equals("G")) {
  283.                 System.out.println("Thank you for using this system.");
  284.                 quit = true;                                                                //if quit = true then the loop will stop then program stops.
  285.             }
  286.  
  287.             else {
  288.                 System.out.println("You do not enter a valid option, please try again.");
  289.             }
  290.  
  291.         }while (quit == false);                                                             //if quit = false then the loop will continue.
  292.     }
  293.  
  294.  
  295.  
  296.     public static void buyingMovie() {
  297.  
  298.         String customerName;
  299.         boolean found = false;
  300.         String movieTitle;
  301.         String mediumType;
  302.         int indexDVD = -1;
  303.         int indexVCD = -1;
  304.         int indexVHS = -1;
  305.         int customerIndex = -1;
  306.         int movieIndex = -1;
  307.  
  308.         //get customer name
  309.         System.out.println("Please enter name");
  310.         customerName = keyboard.nextLine();
  311.         for (int i=0; i<customers.length; i++) {
  312.             if (customers[i] == null) {
  313.                 break;
  314.             }
  315.             if (customers[i].getName().toLowerCase().equals(customerName.toLowerCase())) {
  316.                 found = true;
  317.                 customerIndex = i;
  318.                 break;
  319.             }
  320.             if (i == (customers.length-1)) {
  321.                 found = false;
  322.             }
  323.         }
  324.         if (found == false) {
  325.             System.out.println("The customer name could not be found");
  326.             return;
  327.         }
  328.  
  329.  
  330.         System.out.println("What movie do you want to buy?");
  331.         movieTitle = keyboard.nextLine();
  332.         for (int i=0; i<videos.length; i++) { //problem
  333.             if (videos[i] == null) {
  334.                 break;
  335.             }
  336.             if (videos[i].getTitle().toLowerCase().equals(movieTitle.toLowerCase())) {
  337.                 found = true;
  338.                 if (videos[i] instanceof DVD) { // the 3 instanceof should be inside the first loop
  339.                     System.out.println("DVD");
  340.                     indexDVD = i; // remembering the index of the DVD
  341.                 }
  342.                 else if (videos[i] instanceof VCD) {
  343.                     System.out.println("VCD");
  344.                     indexVCD = i; // remembering the index of the VCD
  345.                 }
  346.                 else if (videos[i] instanceof VHS) {
  347.                     System.out.println("VHS");
  348.                     indexVHS = i; // remembering the index of the VHS
  349.                 }
  350.             }
  351.             if (i == (videos.length-1)) {
  352.             }
  353.         }
  354.         if (found == false) {
  355.             System.out.println("The movie name could not be found");
  356.             return;
  357.         }
  358.  
  359.         /*for (int i=0; i<videos.length; i++) { //amendment
  360.             // throw away this for loop - not needed
  361.         }*/
  362.         System.out.println("Which medium type do you want?");
  363.         mediumType = keyboard.nextLine();
  364.         if (mediumType.equals("DVD") && indexDVD != -1) {
  365.             if (videos[indexDVD].getStatus() == 'O') {
  366.                 System.out.println("Sorry, the DVD version of this video has been rented out and is not available to purchase at this time");
  367.                 return;
  368.             }
  369.             else
  370.                 System.out.println("Customer:" + customers[customerIndex].getName());
  371.                 System.out.println("Video Title:" + videos[indexDVD].getTitle());   //
  372.                 System.out.println("Video Type: DVD");
  373.                 System.out.println("Actual Price:" + videos[indexDVD].getPrice());
  374.                 //customers[customerIndex].setAmount();
  375.                 System.out.println("Thank you. You've successfully purchased this DVD.");
  376.                 videos[indexDVD] = null;
  377.         }
  378.         else if (mediumType.equals("VCD") && indexVCD != -1) {
  379.             if (videos[indexVCD].getStatus() == 'O') {
  380.                 System.out.print("Sorry, the VCD version of this video has been rented out and is not available to purchase at this time");
  381.                 return;
  382.             }
  383.             else
  384.                 System.out.println("Customer:" + customers[customerIndex].getName());
  385.                 System.out.println("Video Title:" + videos[indexVCD].getTitle());    //
  386.                 System.out.println("Video Type: VCD");
  387.                 System.out.println("Actual Price:" + videos[indexVCD].getPrice());
  388.                 //customers[customerIndex].setAmount();
  389.                 System.out.println("Thank you. You've successfully purchased this VCD.");
  390.                 videos[indexVCD] = null;
  391.         }
  392.         else if (mediumType.equals("VHS") && indexVHS != -1) {
  393.                 if (videos[indexVHS].getStatus() == 'O') {
  394.                 System.out.println("Sorry, the VHS version of this video has been rented out and is not available to purchase at this time");
  395.                 return;
  396.             }
  397.             else
  398.                 System.out.println("Customer:" + customers[customerIndex].getName());
  399.                 System.out.println("Video Title:" + videos[indexVHS].getTitle());   //
  400.                 System.out.println("Video Type: VHS");
  401.                 System.out.println("Actual Price:" + videos[indexVHS].getPrice());
  402.                 //customers[customerIndex].setAmount();
  403.                 System.out.println("Thank you. You've successfully purchased this VHS.");
  404.                 videos[indexVHS] = null;
  405.         }
  406.         else {
  407.             System.out.println("Sorry, the medium type you requested was not found.");
  408.         }
  409.         Video[] temporaries = new Video[100];
  410.         temporaries = videos;
  411.         int n = 0;
  412.         for (int i=0; i<videos.length; i++){
  413.             if (videos[i] != null) {
  414.                 temporaries[n++] = videos[i];
  415.             }
  416.         }
  417.         videos = temporaries;
  418.     }//end buyingMovie method
  419.  
  420. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement