Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cinema;
- import java.util.Scanner;
- class Cinema {
- static int seats;
- static int rows;
- static String[][] cinema = new String[rows][seats];
- static Scanner s = new Scanner(System.in);
- static int menuNumber;
- static int currentIncome;
- public static void main (String[] args) {
- createCinema();
- showMenu();
- }
- public static void createCinema () {
- System.out.println("Enter the number of rows:");
- rows = s.nextInt();
- System.out.println("Enter the number of seats in each row:");
- seats = s.nextInt();
- cinema = new String[rows][seats];
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < seats; j++) {
- cinema[i][j] = "S";
- }
- }
- }
- public static void showSeats () {
- System.out.print("Cinema:\n" + " ");
- for (int i = 0; i < seats; i++) {
- System.out.print(" " + (i + 1));
- }
- System.out.println();
- for (int i = 0; i < rows; i++) {
- System.out.print((i + 1) + " ");
- for (int j = 0; j < seats; j++) {
- System.out.print(cinema[i][j] + " ");
- }
- System.out.println();
- }
- System.out.println();
- }
- public static void buyTicket () {
- System.out.println("Enter a row number:");
- int rowNumber = s.nextInt();
- System.out.println("Enter a seat number in that row:");
- int seatNumber = s.nextInt();
- System.out.println();
- try {
- if (cinema[rowNumber - 1][seatNumber - 1].equals("B")) {
- System.out.println("That ticket has already been purchased!\n");
- buyTicket();
- } else {
- if (rows * seats > 60) {
- if (rowNumber > rows / 2) {
- currentIncome += 8;
- System.out.println("Ticket price: $" + 8);
- System.out.println();
- } else {
- currentIncome += 10;
- System.out.println("Ticket price: $" + 10);
- System.out.println();
- }
- } else {
- currentIncome += 10;
- System.out.println("Ticket price: $" + 10);
- System.out.println();
- }
- cinema[rowNumber - 1][seatNumber - 1] = "B";
- }
- } catch (ArrayIndexOutOfBoundsException e) {
- System.out.println("Wrong input!\n");
- buyTicket();
- }
- }
- public static void showMenu () {
- System.out.println("1. Show the seats\n" +
- "2. Buy a ticket\n" +
- "3. Statistics\n" +
- "0. Exit");
- menuNumber = s.nextInt();
- while (menuNumber != 0) {
- if (menuNumber == 1) {
- showSeats();
- showMenu();
- } else if (menuNumber == 2) {
- buyTicket();
- showMenu();
- } else if (menuNumber == 3) {
- showStatistics();
- showMenu();
- }
- return;
- }
- }
- public static void showStatistics () {
- int bookedSeats = 0;
- int availableSeats = 0;
- int totalIncome = 0;
- double percentageTickets = 0;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < seats; j++) {
- if (cinema[i][j] == "B") {
- bookedSeats++;
- } else if (cinema[i][j] == "S") {
- availableSeats++;
- }
- }
- }
- if (rows * seats > 60) {
- int firstHalfRoomPrice = ((rows / 2) * seats) * 10;
- int secondHalfRoomPrice = ((rows - (rows / 2)) * seats) * 8;
- totalIncome = firstHalfRoomPrice + secondHalfRoomPrice;
- } else {
- totalIncome = rows * seats * 10;
- }
- percentageTickets = (bookedSeats / ((double)(rows * seats)) * 100);
- String percentage = String.format("%.2f", percentageTickets);
- System.out.println("Number of purchased tickets: " + bookedSeats);
- System.out.println("Percentage: " + percentage + "%");
- System.out.println("Current income: $" + currentIncome);
- System.out.println("Total income: $" + totalIncome);
- System.out.println();
- showMenu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement