Advertisement
keevosp

Cinema Room Manager by hyperskill.org

May 30th, 2023
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.59 KB | Source Code | 0 0
  1. package cinema;
  2.  
  3. import java.util.Scanner;
  4.  
  5. class Cinema {
  6.    
  7.     static int seats;
  8.     static int rows;
  9.     static String[][] cinema = new String[rows][seats];
  10.     static Scanner s = new Scanner(System.in);
  11.     static int menuNumber;
  12.     static int currentIncome;
  13.    
  14.     public static void main (String[] args) {
  15.    
  16.         createCinema();
  17.         showMenu();
  18.     }
  19.    
  20.     public static void createCinema () {
  21.         System.out.println("Enter the number of rows:");
  22.         rows = s.nextInt();
  23.         System.out.println("Enter the number of seats in each row:");
  24.         seats = s.nextInt();
  25.         cinema = new String[rows][seats];
  26.        
  27.         for (int i = 0; i < rows; i++) {
  28.             for (int j = 0; j < seats; j++) {
  29.                 cinema[i][j] = "S";
  30.             }
  31.         }
  32.     }
  33.    
  34.     public static void showSeats () {
  35.         System.out.print("Cinema:\n" + " ");
  36.         for (int i = 0; i < seats; i++) {
  37.             System.out.print(" " + (i + 1));
  38.         }
  39.         System.out.println();
  40.         for (int i = 0; i < rows; i++) {
  41.             System.out.print((i + 1) + " ");
  42.             for (int j = 0; j < seats; j++) {
  43.                 System.out.print(cinema[i][j] + " ");
  44.             }
  45.             System.out.println();
  46.         }
  47.         System.out.println();
  48.     }
  49.    
  50.     public static void buyTicket () {
  51.         System.out.println("Enter a row number:");
  52.         int rowNumber = s.nextInt();
  53.         System.out.println("Enter a seat number in that row:");
  54.         int seatNumber = s.nextInt();
  55.         System.out.println();
  56.        
  57.         try {
  58.             if (cinema[rowNumber - 1][seatNumber - 1].equals("B")) {
  59.                 System.out.println("That ticket has already been purchased!\n");
  60.                 buyTicket();
  61.             } else {
  62.                 if (rows * seats > 60) {
  63.                     if (rowNumber > rows / 2) {
  64.                         currentIncome += 8;
  65.                         System.out.println("Ticket price: $" + 8);
  66.                         System.out.println();
  67.                     } else {
  68.                         currentIncome += 10;
  69.                         System.out.println("Ticket price: $" + 10);
  70.                         System.out.println();
  71.                     }
  72.                 } else {
  73.                     currentIncome += 10;
  74.                     System.out.println("Ticket price: $" + 10);
  75.                     System.out.println();
  76.                 }
  77.                 cinema[rowNumber - 1][seatNumber - 1] = "B";
  78.             }
  79.         } catch (ArrayIndexOutOfBoundsException e) {
  80.             System.out.println("Wrong input!\n");
  81.             buyTicket();
  82.         }
  83.     }
  84.    
  85.     public static void showMenu () {
  86.         System.out.println("1. Show the seats\n" +
  87.                            "2. Buy a ticket\n" +
  88.                            "3. Statistics\n" +
  89.                            "0. Exit");
  90.         menuNumber = s.nextInt();
  91.         while (menuNumber != 0) {
  92.             if (menuNumber == 1) {
  93.                 showSeats();
  94.                 showMenu();
  95.             } else if (menuNumber == 2) {
  96.                 buyTicket();
  97.                 showMenu();
  98.             } else if (menuNumber == 3) {
  99.                 showStatistics();
  100.                 showMenu();
  101.             }
  102.             return;
  103.         }
  104.     }
  105.    
  106.     public static void showStatistics () {
  107.         int bookedSeats = 0;
  108.         int availableSeats = 0;
  109.         int totalIncome = 0;
  110.         double percentageTickets = 0;
  111.    
  112.         for (int i = 0; i < rows; i++) {
  113.             for (int j = 0; j < seats; j++) {
  114.                 if (cinema[i][j] == "B") {
  115.                     bookedSeats++;
  116.                 } else if (cinema[i][j] == "S") {
  117.                     availableSeats++;
  118.                 }
  119.             }
  120.         }
  121.         if (rows * seats > 60) {
  122.             int firstHalfRoomPrice = ((rows / 2) * seats) * 10;
  123.             int secondHalfRoomPrice = ((rows - (rows / 2)) * seats) * 8;
  124.             totalIncome = firstHalfRoomPrice + secondHalfRoomPrice;
  125.         } else {
  126.             totalIncome = rows * seats * 10;
  127.         }
  128.         percentageTickets = (bookedSeats / ((double)(rows * seats)) * 100);
  129.         String percentage = String.format("%.2f", percentageTickets);
  130.        
  131.         System.out.println("Number of purchased tickets: " + bookedSeats);
  132.         System.out.println("Percentage: " + percentage + "%");
  133.         System.out.println("Current income: $" + currentIncome);
  134.         System.out.println("Total income: $" + totalIncome);
  135.         System.out.println();
  136.        
  137.         showMenu();
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement