Advertisement
Guest User

Untitled

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