Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.19 KB | None | 0 0
  1. package main;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.Scanner;
  6.  
  7. import javax.xml.ws.spi.Invoker;
  8.  
  9. import model.Game;
  10. import model.Inventory;
  11. import model.Item;
  12. import model.Movie;
  13.  
  14. public class Store {
  15.    
  16.     public static Scanner in;
  17.     public static Inventory inventory;
  18.     public static int day = 1;
  19.    
  20.     public static void main(String[] args) {
  21.         String command;
  22.         in = new Scanner(System.in);
  23.         inventory = new Inventory();
  24.        
  25.         while (true) {
  26.             showMenu();
  27.             command = in.nextLine();
  28.  
  29.             System.out.println();
  30.            
  31.             switch (command) {
  32.                 case "L":
  33.                     listItemMenu();
  34.                     break;
  35.                 case "AG":
  36.                     addGameMenu();
  37.                     break;
  38.                 case "AM":
  39.                     addMovieMenu();
  40.                     break;
  41.                 case "RG":
  42.                     rentGameMenu();
  43.                     break;
  44.                 case "RM" :
  45.                     rentMovieMenu();
  46.                     break;
  47.                 case "GG" :
  48.                     returnGameMenu();
  49.                     break;
  50.                 case "GM" :
  51.                     returnMovieMenu();
  52.                     break;
  53.                 case "BG" :
  54.                     buyGameMenu();
  55.                     break;
  56.                 case "E":
  57.                     System.out.println("Program terminated");
  58.                     System.exit(0);
  59.                     break;
  60.                 case "T":
  61.                     timePassingMenu();
  62.                     break;
  63.                 default:
  64.                     System.out.println("Invalid command.");
  65.                     break;
  66.             }
  67.            
  68.             System.out.println();
  69.         }
  70.     }
  71.    
  72.     private static void showMenu(){
  73.         System.out.println("########################################");
  74.         System.out.println("Video Store Menu (Day " + day + ")");
  75.         System.out.println("########################################");
  76.         System.out.println("AG)\tAdd game\tAM)\tAdd movie");
  77.         //System.out.println("AM)\tAdd movie");
  78.         System.out.println("RG)\tRent game\tRM)\tRent movie");
  79.         //System.out.println("RM)\tRent movie");
  80.         System.out.println("GG)\tReturn game\tGM)\tReturn movie");
  81.         //System.out.println("GM)\tReturn movie");
  82.         System.out.println("BG)\tBuy game\tL)\tList all items");
  83.         System.out.println("T)\tTime passing\tE)\tExit");
  84.         System.out.println("----------------------------------------");
  85.         System.out.print("Enter input command : ");
  86.     }
  87.    
  88.     private static void listItemMenu() {
  89.         // TODO Auto-generated method stub
  90.         System.out.println("----------------------------------------");
  91.         System.out.println("List all items in inventory");
  92.         System.out.println("----------------------------------------");
  93.         System.out.println("Game (Total " + inventory.getTotalGames() + "):");
  94.         System.out.println("----------------------------------------");
  95.        
  96.         inventory.listGames();
  97.        
  98.         System.out.println("----------------------------------------");
  99.         System.out.println("Movie (Total " + inventory.getTotalMovies() + "):");
  100.         System.out.println("----------------------------------------");
  101.        
  102.         inventory.listMovies();
  103.     }
  104.    
  105.     private static void addGameMenu() {
  106.         // TODO Auto-generated method stub
  107.         System.out.println("----------------------------------------");
  108.         System.out.println("Add game to inventory");
  109.         System.out.println("----------------------------------------");
  110.         int rand = (int)(Math.random() * 9);
  111.         System.out.println("Name  : " + Inventory.gameNames[rand]);
  112.         System.out.println("Rate  : " + Inventory.gameRatings[rand]);
  113.         System.out.println("Price : " + Inventory.gamePrices[rand]);
  114.         inventory.addItem(new Game(Inventory.gameNames[rand],Inventory.gameRatings[rand],Inventory.gamePrices[rand]));
  115.         inventory.sortItems();
  116.  
  117.     }
  118.    
  119.     private static void addMovieMenu() {
  120.         // TODO Auto-generated method stub
  121.         System.out.println("----------------------------------------");
  122.         System.out.println("Add movie to inventory");
  123.         System.out.println("----------------------------------------");
  124.         int rand = (int)(Math.random() * 9);
  125.         System.out.println("Name  : " + Inventory.movieNames[rand]);
  126.         System.out.println("Rate  : " + Inventory.movieRatings[rand]);
  127.         System.out.println("Price : " + Inventory.moviePrices[rand]);
  128.         inventory.addItem(new Movie(Inventory.movieNames[rand],Inventory.movieRatings[rand],Inventory.moviePrices[rand]));
  129.         inventory.sortItems();
  130.     }
  131.    
  132.     private static void rentGameMenu(){
  133.         System.out.println("----------------------------------------");
  134.         System.out.println("Rent game");
  135.         System.out.println("----------------------------------------");
  136.        
  137.         String gameName;
  138.         String renterName;
  139.         int rentOption;
  140.        
  141.         // TODO Fill Code
  142.         System.out.println("Enter game name : ");
  143.         gameName = in.nextLine();
  144.         int index = inventory.searchForRentableGame(gameName);
  145.         Game t = null;
  146.         if(index != -1) {
  147.             t = (Game) Inventory.items.get(index);
  148.             System.out.println("Choose the rental options from 1-4 : ");
  149.             System.out.println("1) $3.00 for 1 week");
  150.             System.out.println("2) $5.40 for 2 weeks");
  151.             System.out.println("3) $7.00 for 3 weeks");
  152.             System.out.println("4) $9.00 for 4 weeks");
  153.             System.out.println("Enter your option : ");
  154.             rentOption = in.nextInt();
  155.             in.next();
  156.             System.out.println("Enter your name : ");
  157.             renterName = in.next();
  158.             System.out.println("You rented the game successfully. The game ID is "+t.getId()+".");
  159.             System.out.println("Please inform the game ID when you return it.");
  160.             System.out.println("Please return the game within Day "+(rentOption*7+1)+"."+ " Late fee is "+t.getLateFeePerDay()+ "/days.");
  161.             t.rent(renterName, rentOption);
  162.         }
  163.         else {
  164.             System.out.println("The game is not available to rent. Back to menu.");
  165.         }
  166.     }
  167.    
  168.     private static void rentMovieMenu(){
  169.         System.out.println("----------------------------------------");
  170.         System.out.println("Rent movie");
  171.         System.out.println("----------------------------------------");
  172.        
  173.         String movieName;
  174.         String renterName;
  175.        
  176.         // TODO Fill Code
  177.         System.out.println("Enter movie name : ");
  178.         movieName = in.next();
  179.         int index = inventory.searchForRentableGame(movieName);
  180.         Movie t = (Movie) Inventory.items.get(index);
  181.         if(index != -1) {
  182.             System.out.println("Enter your name : ");
  183.             renterName = in.next();
  184.             System.out.println("You rented the movie successfully. The movie ID is "+t.getId()+".");
  185.             System.out.println("Please inform the movie ID when you return it.");
  186.             t.rent(renterName, 0);
  187.         }
  188.         else {
  189.             System.out.println("The movie is not available to rent. Back to menu.");
  190.         }
  191.     }
  192.    
  193.     private static void returnGameMenu(){
  194.         System.out.println("----------------------------------------");
  195.         System.out.println("Return Game");
  196.         System.out.println("----------------------------------------");
  197.        
  198.         int gameID;
  199.        
  200.         // TODO Fill Code
  201.         System.out.println("Enter game ID : ");
  202.         gameID = in.nextInt();
  203.         in.next();
  204.         Game t = null;
  205.         for(int i=0;i<Inventory.items.size();++i) {
  206.             if(Inventory.items.get(i) instanceof Game && Inventory.items.get(i).getId()==gameID) {
  207.                 t = (Game) Inventory.items.get(i);
  208.                 break;
  209.             }
  210.         }
  211.         if(t!=null) {
  212.             if(day-t.getGiveBackDate()<=0) {
  213.                 System.out.println("The rental fee is $"+t.giveBack()+".");
  214.             }
  215.             else {
  216.                 System.out.println("The rental fee is $"+t.giveBack()+". (Late "+(day-t.getGiveBackDate())+" days)");
  217.             }
  218.         }
  219.         else {
  220.             System.out.println("the game is not rented. Back to menu.");
  221.         }
  222.     }
  223.    
  224.     private static void returnMovieMenu(){
  225.         System.out.println("----------------------------------------");
  226.         System.out.println("Return movie");
  227.         System.out.println("----------------------------------------");
  228.        
  229.         int movieID;
  230.        
  231.         // TODO Fill Code
  232.         System.out.println("Enter movie ID : ");
  233.         movieID = in.nextInt();
  234.         in.next();
  235.         Movie t = null;
  236.         for(int i=0;i<Inventory.items.size();++i) {
  237.             if(Inventory.items.get(i) instanceof Game && Inventory.items.get(i).getId()==movieID) {
  238.                 t = (Movie) Inventory.items.get(i);
  239.                 break;
  240.             }
  241.         }
  242.         if(t!=null) {
  243.             System.out.println("The rental fee is $"+t.giveBack()+".");
  244.  
  245.         }
  246.         else {
  247.             System.out.println("the movie is not rented. Back to menu.");
  248.         }
  249.     }
  250.    
  251.     private static void buyGameMenu(){
  252.         System.out.println("----------------------------------------");
  253.         System.out.println("Buy Game");
  254.         System.out.println("----------------------------------------");
  255.        
  256.         String gameName;
  257.         String buyerName;
  258.        
  259.         // TODO Fill Code
  260.         System.out.println("Enter game name : ");
  261.         gameName = in.next();
  262.         int index = inventory.searchForRentableGame(gameName);
  263.         Game t = (Game) Inventory.items.get(index);
  264.         if(index != -1) {
  265.             System.out.println("Enter your name : ");
  266.             buyerName = in.next();
  267.             System.out.println("The price is $"+t.getPrice()+".");
  268.             System.out.println("Thank you for buying");
  269.         }
  270.         else {
  271.             System.out.println("The game is not available to buy. Back to menu.");
  272.         }
  273.     }
  274.    
  275.     private static void timePassingMenu() {
  276.         // TODO Auto-generated method stub
  277.             System.out.println("----------------------------------------");
  278.             System.out.println("Simulate Time Passing");
  279.             System.out.println("----------------------------------------");
  280.            
  281.             System.out.print("Enter the number of days passing : ");
  282.            
  283.             try{
  284.                 int days = in.nextInt();
  285.                
  286.                 if (days <= 0)
  287.                 {
  288.                     System.out.println("The number of days should be more than 0.");
  289.                 }
  290.                 else
  291.                 {
  292.                     Store.day += days;
  293.                     System.out.println("Now is Day " + Store.day + ".");
  294.                 }
  295.             }
  296.             catch(Exception e)
  297.             {
  298.                 System.out.println("The input should be an integer.");
  299.             }
  300.            
  301.             in.nextLine(); // For '\n' from pressing enter;
  302.     }
  303.  
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement