Advertisement
GabrielHr00

06. Cinema Tickets

Feb 9th, 2025
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. package nestedLoops_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CinemaTickets {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int allTicketsSoldCount = 0;
  9.         int studentTicketsCount = 0;
  10.         int standardTicketsCount = 0;
  11.         int kidsTicketsCount = 0;
  12.  
  13.         String movieName = scanner.nextLine();
  14.         while (!movieName.equals("Finish")) {
  15.             int freeSeats = Integer.parseInt(scanner.nextLine());
  16.             int soldTicketsCount = 0;
  17.  
  18.             String ticketType = scanner.nextLine();
  19.             while (!ticketType.equals("End")) {
  20.                 soldTicketsCount++;
  21.                 allTicketsSoldCount++;
  22.  
  23.                 switch (ticketType) {
  24.                     case "student" -> studentTicketsCount++;
  25.                     case "standard" -> standardTicketsCount++;
  26.                     case "kid" -> kidsTicketsCount++;
  27.                 }
  28.  
  29.                 if (soldTicketsCount >= freeSeats) {
  30.                     break;
  31.                 }
  32.  
  33.                 ticketType = scanner.nextLine();
  34.             }
  35.  
  36.             System.out.printf("%s - %.2f%% full.%n", movieName, 1.0 * soldTicketsCount / freeSeats * 100);
  37.  
  38.             movieName = scanner.nextLine();
  39.         }
  40.  
  41.         System.out.printf("Total tickets: %d%n", allTicketsSoldCount);
  42.         System.out.printf("%.2f%% student tickets.%n", 1.0 * studentTicketsCount / allTicketsSoldCount * 100);
  43.         System.out.printf("%.2f%% standard tickets.%n", 1.0 * standardTicketsCount / allTicketsSoldCount * 100);
  44.         System.out.printf("%.2f%% kids tickets.%n", 1.0 * kidsTicketsCount / allTicketsSoldCount * 100);
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement