Ivan_Bochev

Hall

Jun 17th, 2021 (edited)
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.21 KB | None | 0 0
  1. package c;
  2.  
  3. import java.util.*;
  4.  
  5. public class Hall {
  6.     private static ArrayList<Concert> Aconcerts;
  7.     private static ArrayList<String> Agenres;
  8.     private static String address;
  9.     private static boolean flag;
  10.     private static boolean last;
  11.     private static Scanner sc = new Scanner(System.in);
  12.  
  13.     public Hall(String address, ArrayList<Concert> aconcerts, ArrayList<String> agenres) {
  14.         Aconcerts = aconcerts;
  15.         Agenres = agenres;
  16.         Hall.address = address;
  17.     }
  18.  
  19.     public static void sellTickets() {
  20.         try {
  21.             ArrayList<Concert> concerts = new ArrayList<>();
  22.             System.out.println("\nWe offer tickets for 2 types of concerts:\n1. Rock concerts\n2. Metal concerts");
  23.             System.out.print("\nWhich concert type do you choose: ");
  24.             int choose = Integer.parseInt(sc.nextLine());
  25.             switch (choose) {
  26.             case 1:
  27.                 System.out.println("\nThere are 2 characteristics for grouping the concerts:\n1. Genre\n2. Price");
  28.                 System.out.print("\nWhat's the characteristic that you want to group the concerts by: ");
  29.                 int characteristic = Integer.parseInt(sc.nextLine());
  30.                 switch (characteristic) {
  31.                 case 1:
  32.                     chooseByGenre(choose, concerts);
  33.                     break;
  34.                 case 2:
  35.                     chooseByPrice(choose, concerts);
  36.                     break;
  37.                 default:
  38.                     System.out.println("\nInvalid characteristic!\nPlease try again!");
  39.                     sellTickets();
  40.                 }
  41.                 break;
  42.             case 2:
  43.                 System.out.println("\nThere are 2 characteristics for grouping the concerts:\n1. Genre\n2. Price");
  44.                 System.out.print("\nWhat's the characteristic that you want to group the concerts by: ");
  45.                 int aspect = Integer.parseInt(sc.nextLine());
  46.                 switch (aspect) {
  47.                 case 1:
  48.                     chooseByGenre(choose, concerts);
  49.                     break;
  50.                 case 2:
  51.                     chooseByPrice(choose, concerts);
  52.                     break;
  53.                 default:
  54.                     System.out.println("\nInvalid characteristic!\nPlease try again!");
  55.                     sellTickets();
  56.                 }
  57.                 break;
  58.             default:
  59.                 System.out.println("\nInvalid choice!\nPlease try again!");
  60.                 sellTickets();
  61.             }
  62.         } catch (NumberFormatException nfe) {
  63.             System.out.println("\nInvalid input!");
  64.             sellTickets();
  65.         }
  66.     }
  67.  
  68.     private static void chooseByGenre(int choose, ArrayList<Concert> concerts) {
  69.         int choice = 1;
  70.         switch (choose) {
  71.         case 1:
  72.             System.out.println("\nThis are the genres that we offer:");
  73.             for (int i = 0; i < Agenres.size(); i++) {
  74.                 if (Agenres.get(i).contains("punk") || Agenres.get(i).contains("rock")) {
  75.                     System.out.printf("%d. %s\n", choice, Agenres.get(i));
  76.                     choice++;
  77.                 }
  78.             }
  79.             System.out.print("\nEnter the genre that you want: ");
  80.             String genre = sc.nextLine();
  81.             for (Concert c : Aconcerts) {
  82.                 if (Agenres.contains(genre)) {
  83.                     if (c.getGenre().equals(genre) || c.getGenre().contains(genre)) {
  84.                         concerts.add(c);
  85.                     }
  86.                 } else {
  87.                     System.out.println("\nSorry,but we don't have any concert in that genre category!");
  88.                     break;
  89.                 }
  90.             }
  91.             break;
  92.         default:
  93.             System.out.println("\nThis are the genres that we offer:");
  94.             for (int i = 0; i < Agenres.size(); i++) {
  95.                 if (Agenres.get(i).contains("metal")) {
  96.                     System.out.printf("%d. %s\n", choice, Agenres.get(i));
  97.                     choice++;
  98.                 }
  99.             }
  100.             System.out.print("\nEnter the genre that you want: ");
  101.             String genres = sc.nextLine();
  102.             for (Concert c : Aconcerts) {
  103.                 if (Agenres.contains(genres)) {
  104.                     if (c.getGenre().equals(genres) || c.getGenre().contains(genres)) {
  105.                         concerts.add(c);
  106.                     }
  107.                 } else {
  108.                     System.out.println("\nSorry,but we don't have any concert in that genre category!");
  109.                     break;
  110.                 }
  111.             }
  112.         }
  113.         choosingTickets(concerts);
  114.     }
  115.  
  116.     private static boolean validGenre(int choose, Concert c) {
  117.         switch (choose) {
  118.         case 1:
  119.             if ((c instanceof MetalConcerts) && (c.getGenre().contains("punk rock") || c.getGenre().contains("pop punk")
  120.                     || c.getGenre().contains("alternative rock") || c.getGenre().contains("hard rock")
  121.                     || c.getGenre().contains("grunge rock"))) {
  122.                 return true;
  123.             }
  124.         default:
  125.             if ((c instanceof RockConcerts) && (c.getGenre().contains("thrash metal")
  126.                     || c.getGenre().contains("heavy metal") || c.getGenre().contains("groove metal")
  127.                     || c.getGenre().contains("glam metal") || c.getGenre().contains("black metal")
  128.                     || c.getGenre().contains("melodic death metal") || c.getGenre().contains("death metal")
  129.                     || c.getGenre().contains("nu metal") || c.getGenre().contains("alternative metal")
  130.                     || c.getGenre().contains("progressive metal"))) {
  131.                 return true;
  132.             }
  133.         }
  134.         return false;
  135.     }
  136.  
  137.     private static void chooseByPrice(int choose, ArrayList<Concert> concerts) {
  138.         try {
  139.             System.out.println("There are 3 price categories:\n1. 30 $ - 100 $\n2. 100 $ - 200 $\n3. 200 $ - 300 $\n");
  140.             System.out.print("Enter the price that you want: ");
  141.             double price = Double.parseDouble(sc.nextLine());
  142.             for (Concert c : Aconcerts) {
  143.                 if (choose == 1) {
  144.                     if (price >= 30 && price <= 100) {
  145.                         if (((c instanceof RockConcerts) || validGenre(choose, c))
  146.                                 && (c.getTicketPrice() >= 30 && c.getTicketPrice() <= 100)) {
  147.                             concerts.add(c);
  148.                         }
  149.                     } else if (price > 100 && price <= 200) {
  150.                         if (((c instanceof RockConcerts) || validGenre(choose, c))
  151.                                 && (c.getTicketPrice() > 100 && c.getTicketPrice() <= 200)) {
  152.                             concerts.add(c);
  153.                         }
  154.                     } else if (price > 200 && price <= 300) {
  155.                         if (((c instanceof RockConcerts) || validGenre(choose, c))
  156.                                 && (c.getTicketPrice() > 200 && c.getTicketPrice() <= 300)) {
  157.                             concerts.add(c);
  158.                         }
  159.                     } else {
  160.                         System.out.println("\nSorry,but we don't have any concert in that price category!");
  161.                         break;
  162.                     }
  163.                 } else if (choose == 2) {
  164.                     if (price >= 30 && price <= 100) {
  165.                         if ((price >= 30 && price <= 100) && ((c instanceof MetalConcerts) || validGenre(choose, c))
  166.                                 && (c.getTicketPrice() >= 30 && c.getTicketPrice() <= 100)) {
  167.                             concerts.add(c);
  168.                         }
  169.                     } else if (price > 100 && price <= 200) {
  170.                         if (((c instanceof MetalConcerts) || validGenre(choose, c))
  171.                                 && (c.getTicketPrice() > 100 && c.getTicketPrice() <= 200)) {
  172.                             concerts.add(c);
  173.                         }
  174.                     } else if (price > 200 && price <= 300) {
  175.                         if (((c instanceof MetalConcerts) || validGenre(choose, c))
  176.                                 && (c.getTicketPrice() > 200 && c.getTicketPrice() <= 300)) {
  177.                             concerts.add(c);
  178.                         }
  179.                     } else {
  180.                         System.out.println("\nSorry,but we don't have any concert in that price category!");
  181.                         break;
  182.                     }
  183.                 }
  184.             }
  185.         } catch (NumberFormatException nfe) {
  186.             System.out.println("\nInvalid input!\n");
  187.             chooseByPrice(choose, concerts);
  188.         }
  189.         choosingTickets(concerts);
  190.     }
  191.  
  192.     private static void sortAndPrint(ArrayList<Concert> concerts) {
  193.         Collections.sort(concerts, Concert.comparator);
  194.         for (Concert c : concerts) {
  195.             System.out.println("\nBand's name: " + c.getBand());
  196.             System.out.println("Ticket price: " + c.getTicketPrice());
  197.         }
  198.     }
  199.  
  200.     private static String generateForm(String symbol, int number) {
  201.         StringBuffer buffer = new StringBuffer();
  202.         for (int i = 0; i < number; i++) {
  203.             buffer.append(symbol);
  204.         }
  205.         return buffer.toString();
  206.     }
  207.  
  208.     private static void showTicket(int count, Concert concert) {
  209.         int rows = 24;
  210.         String price = "Price: " + concert.getTicketPrice();
  211.         String tcount = "Count: " + count;
  212.         String tprice = "Total: " + count * concert.getTicketPrice();
  213.         System.out.println(" " + generateForm("_ ", rows) + "_");
  214.         System.out.println("|" + generateForm(" ", rows - 2) + "TICKET" + generateForm(" ", rows - 3) + "|");
  215.         System.out.println("|" + concert.getBand()
  216.                 + generateForm(" ", 2 * rows + 1 - (concert.getBand().length() + tcount.length())) + tcount + "|");
  217.         String type = (concert instanceof RockConcerts) ? "Rock concert" : "Metal Concert";
  218.         System.out
  219.                 .println("|" + type + generateForm(" ", 2 * rows + 1 - (type.length() + price.length())) + price + "|");
  220.         System.out.println("|" + generateForm(" ", 2 * rows + 1) + "|");
  221.         System.out.println(
  222.                 "|" + address + generateForm(" ", 2 * rows + 1 - (address.length() + tprice.length())) + tprice + "|");
  223.         System.out.println(" " + generateForm("¯ ", rows) + "¯");
  224.     }
  225.  
  226.     private static void decision(String choice, int count, Concert concert) {
  227.         switch (choice) {
  228.         case "yes":
  229.             showTicket(count, concert);
  230.             if (last) {
  231.                 concert.setTicketCount(10);
  232.             } else {
  233.                 concert.setTicketCount(concert.getTicketCount() - count);
  234.             }
  235.             Application.setBudget(Application.getBudget() - count * concert.getTicketPrice());
  236.             flag = true;
  237.             break;
  238.         case "no":
  239.             System.out.println("\nWe are sorry to hear that!");
  240.             flag = true;
  241.             break;
  242.         default:
  243.             System.out.println("\nSomething went wrong!\nPlease try again!");
  244.         }
  245.     }
  246.  
  247.     private static void calculateAffCount(Concert concert) {
  248.         int affCount = (int) (Application.getBudget() / concert.getTicketPrice());
  249.         if (affCount >= 1) {
  250.             switch (affCount) {
  251.             case 1:
  252.                 System.out.printf("\nYou can buy only 1 ticket for the %s's concert!\n", concert.getBand());
  253.                 System.out.print("Would you buy it ('yes' or 'no'): ");
  254.                 String choice = sc.nextLine();
  255.                 decision(choice, affCount, concert);
  256.                 break;
  257.             default:
  258.                 System.out.printf("\nYou can buy only %d tickets for the %s's concert!\n", affCount, concert.getBand());
  259.                 System.out.print("Would you buy them ('yes' or 'no'): ");
  260.                 choice = sc.nextLine();
  261.                 decision(choice, affCount, concert);
  262.             }
  263.         } else {
  264.             System.out.printf("\nYou cannot buy a single ticket for the %s's concert!\n", concert.getBand());
  265.             flag = true;
  266.         }
  267.     }
  268.  
  269.     private static void choose(int count, Concert concert) {
  270.         while (true) {
  271.             try {
  272.                 if (flag) {
  273.                     break;
  274.                 }
  275.                 if (Application.getBudget() < count * concert.getTicketPrice()) {
  276.                     switch (count) {
  277.                     case 1:
  278.                         System.out.printf(
  279.                                 "\nSorry, but you have only %.2f $, which is not enough to buy tickets for the %s's concert!\n",
  280.                                 Application.getBudget(), concert.getBand());
  281.                         System.out.print("Would you add some money to your budget ('yes' or 'no'): ");
  282.                         String choice = sc.nextLine();
  283.                         switch (choice) {
  284.                         case "yes":
  285.                             System.out.print("\nEnter the amount you want to add: ");
  286.                             double add = Double.parseDouble(sc.nextLine());
  287.                             if (add > 0) {
  288.                                 if (Application.validBudget(Application.getBudget() + add)) {
  289.                                     Application.setBudget(Application.getBudget() + add);
  290.                                 } else {
  291.                                     System.out.printf("Your current budget is: %.2f $\n", Application.getBudget());
  292.                                 }
  293.                             } else {
  294.                                 System.out.println("\nInvalid input!");
  295.                             }
  296.                             flag = true;
  297.                             break;
  298.                         case "no":
  299.                             System.out.println("\nOK!");
  300.                             flag = true;
  301.                             break;
  302.                         default:
  303.                             System.out.println("\nSomething went wrong!\nPlease try again!");
  304.                         }
  305.                         break;
  306.                     default:
  307.                         calculateAffCount(concert);
  308.                     }
  309.                 } else {
  310.                     if (concert.getTicketCount() < count) {
  311.                         switch (concert.getTicketCount()) {
  312.                         case 1:
  313.                             System.out.printf("\nWe have only 1 ticket for the %s's concert!\n", concert.getBand());
  314.                             System.out.print("Would you buy it ('yes' or 'no'): ");
  315.                             String choice = sc.nextLine();
  316.                             last = true;
  317.                             decision(choice, count, concert);
  318.                             break;
  319.                         default:
  320.                             System.out.printf("\nWe have only %d tickets for the %s's concert!\n",
  321.                                     concert.getTicketCount(), concert.getBand());
  322.                             System.out.print("Would you buy them ('yes' or 'no'): ");
  323.                             choice = sc.nextLine();
  324.                             last = true;
  325.                             decision(choice, count, concert);
  326.                         }
  327.                     } else if (concert.getTicketCount() == count) {
  328.                         concert.setTicketCount(10);
  329.                         Application.setBudget(Application.getBudget() - count * concert.getTicketPrice());
  330.                         showTicket(count, concert);
  331.                         break;
  332.                     } else {
  333.                         concert.setTicketCount(concert.getTicketCount() - count);
  334.                         Application.setBudget(Application.getBudget() - count * concert.getTicketPrice());
  335.                         showTicket(count, concert);
  336.                         break;
  337.                     }
  338.                 }
  339.             } catch (NumberFormatException nfe) {
  340.                 System.out.println("\nInvalid input!");
  341.                 sellTickets();
  342.             }
  343.         }
  344.     }
  345.  
  346.     private static void choosingTickets(ArrayList<Concert> concerts) {
  347.         while (concerts.size() != 0) {
  348.             try {
  349.                 if (flag) {
  350.                     break;
  351.                 }
  352.                 sortAndPrint(concerts);
  353.                 System.out.print("\nWould you like to buy tickets ('yes' or 'no'): ");
  354.                 String decision = sc.nextLine();
  355.                 switch (decision) {
  356.                 case "yes":
  357.                     switch (concerts.size()) {
  358.                     case 1:
  359.                         System.out.print("How many tickets would you buy: ");
  360.                         int count = Integer.parseInt(sc.nextLine());
  361.                         if (count <= 0) {
  362.                             System.out.println("\nInvalid ticket count!\nPlease try again!");
  363.                         } else {
  364.                             choose(count, concerts.get(0));
  365.                         }
  366.                         break;
  367.                     default:
  368.                         System.out.printf(
  369.                                 "\nYou got %d choises\nWhich band do you choose (enter number from 1 to %d): ",
  370.                                 concerts.size(), concerts.size());
  371.                         int choice = Integer.parseInt(sc.nextLine());
  372.                         if (choice <= 0 || choice > concerts.size()) {
  373.                             System.out.println("\nInvalid choice!\nPlease try again!");
  374.                         } else {
  375.                             System.out.print("How many tickets would you buy: ");
  376.                             int counts = Integer.parseInt(sc.nextLine());
  377.                             if (counts <= 0) {
  378.                                 System.out.println("\nInvalid ticket count!\nPlease try again!");
  379.                             } else {
  380.                                 for (int i = 0; i <= concerts.size(); i++) {
  381.                                     if (i + 1 == choice) {
  382.                                         choose(counts, concerts.get(i));
  383.                                     }
  384.                                 }
  385.                             }
  386.                         }
  387.                     }
  388.                     break;
  389.                 case "no":
  390.                     System.out.println("\nWe are sorry to hear that!");
  391.                     flag = true;
  392.                     break;
  393.                 default:
  394.                     System.out.println("\nSomething went wrong!\nPlease try again!");
  395.                 }
  396.             } catch (NumberFormatException nfe) {
  397.                 System.out.println("\nInvalid input!");
  398.             }
  399.         }
  400.         flag = false;
  401.         last = false;
  402.         System.out.print("\nWould you like to see other concerts ('yes' or 'no'): ");
  403.         String choice = sc.nextLine();
  404.         switch (choice) {
  405.         case "yes":
  406.             sellTickets();
  407.             break;
  408.         case "no":
  409.             System.out.println("\nGoodbye!\nHave a nice day!");
  410.             break;
  411.         default:
  412.             System.out.println("\nSomething went wrong!\nPlease try again!");
  413.             sellTickets();
  414.         }
  415.     }
  416.  
  417. }
Add Comment
Please, Sign In to add comment