myrdok123

P06CinemaTickets

Feb 12th, 2023
1,203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. package NestedLoops.Exercises;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P06CinemaTickets {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String input = scanner.nextLine();
  11.  
  12.         int totalTickets = 0;
  13.         int countStudentTickets = 0;
  14.         int countKidTickets = 0;
  15.         int countStandardTickets = 0;
  16.  
  17.         while (!input.equals("Finish")){
  18.  
  19.             int availableSeats = Integer.parseInt(scanner.nextLine());
  20.             String command = scanner.nextLine();
  21.             String currentMovie = input;
  22.  
  23.             int countTicketsPerMovie = 0;
  24.  
  25.             while (!command.equals("End")){
  26.  
  27.                 String ticketType = command;
  28.                 countTicketsPerMovie++;
  29.  
  30.                 switch (ticketType){
  31.                     case "student":
  32.                         countStudentTickets ++;
  33.                         break;
  34.                     case "standard":
  35.                         countStandardTickets ++;
  36.                         break;
  37.                     case "kid":
  38.                         countKidTickets ++;
  39.                         break;
  40.  
  41.                 }
  42.  
  43.                 if (countTicketsPerMovie >= availableSeats ){
  44.                     break;
  45.                 }
  46.  
  47.                 command = scanner.nextLine();
  48.             }
  49.  
  50.             totalTickets += countTicketsPerMovie;
  51.  
  52.             double percentFilled = countTicketsPerMovie * 1.0 / availableSeats * 100;
  53.             System.out.printf("%s - %.2f%% full.%n", currentMovie, percentFilled);
  54.  
  55.             input = scanner.nextLine();
  56.         }
  57.  
  58.  
  59.         double percentKidTickets = countKidTickets * 1.0 / totalTickets * 100;
  60.         double percentStudentTickets = countStudentTickets * 1.0 / totalTickets * 100;
  61.         double percentStandardTickets = countStandardTickets * 1.0 / totalTickets * 100;
  62.  
  63.         System.out.printf("Total tickets: %d%n", totalTickets);
  64.         System.out.printf("%.2f%% student tickets.%n", percentStudentTickets);
  65.         System.out.printf("%.2f%% standard tickets.%n", percentStandardTickets);
  66.         System.out.printf("%.2f%% kids tickets.", percentKidTickets);
  67.     }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment