Advertisement
Spocoman

Film Premiere

Sep 6th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String movie = scanner.nextLine(),
  7.                 pack = scanner.nextLine();
  8.         int tickets = Integer.parseInt(scanner.nextLine());
  9.         double ticketPrice = 0;
  10.  
  11.         if (movie.equals("John Wick")) {
  12.             if (pack.equals("Drink")) {
  13.                 ticketPrice = 12;
  14.             } else if (pack.equals("Popcorn")) {
  15.                 ticketPrice = 15;
  16.             } else {
  17.                 ticketPrice = 19;
  18.             }
  19.         } else if (movie.equals("Star Wars")) {
  20.             if (pack.equals("Drink")) {
  21.                 ticketPrice = 18;
  22.             } else if (pack.equals("Popcorn")) {
  23.                 ticketPrice = 25;
  24.             } else {
  25.                 ticketPrice = 30;
  26.             }
  27.         } else if (movie.equals("Jumanji")) {
  28.             if (pack.equals("Drink")) {
  29.                 ticketPrice = 9;
  30.             } else if (pack.equals("Popcorn")) {
  31.                 ticketPrice = 11;
  32.             } else {
  33.                 ticketPrice = 14;
  34.             }
  35.         }
  36.  
  37.         if (movie.equals("Star Wars") && tickets >= 4) {
  38.             ticketPrice *= 0.70;
  39.         } else if (movie.equals("Jumanji") && tickets == 2) {
  40.             ticketPrice *= 0.85;
  41.         }
  42.  
  43.         System.out.printf("Your bill is %.2f leva.\n", ticketPrice * tickets);
  44.     }
  45. }
  46.  
  47. Решение със switch:
  48.  
  49. import java.util.Scanner;
  50.  
  51. public class Main {
  52.     public static void main(String[] args) {
  53.         Scanner scanner = new Scanner(System.in);
  54.         String movie = scanner.nextLine(),
  55.                 pack = scanner.nextLine();
  56.         int tickets = Integer.parseInt(scanner.nextLine());
  57.         double ticketPrice = 0;
  58.  
  59.         switch (movie) {
  60.             case "John Wick" -> ticketPrice = switch (pack) {
  61.                 case "Drink" -> 12;
  62.                 case "Popcorn" -> 15;
  63.                 default -> 19;
  64.             };
  65.             case "Star Wars" -> ticketPrice = switch (pack) {
  66.                 case "Drink" -> 18;
  67.                 case "Popcorn" -> 25;
  68.                 default -> 30;
  69.             };
  70.             case "Jumanji" -> ticketPrice = switch (pack) {
  71.                 case "Drink" -> 9;
  72.                 case "Popcorn" -> 11;
  73.                 default -> 14;
  74.             };
  75.         }
  76.  
  77.         if (movie.equals("Star Wars") && tickets >= 4) {
  78.             ticketPrice *= 0.70;
  79.         } else if (movie.equals("Jumanji") && tickets == 2) {
  80.             ticketPrice *= 0.85;
  81.         }
  82.  
  83.         System.out.printf("Your bill is %.2f leva.\n", ticketPrice * tickets);
  84.     }
  85. }
  86.  
  87. Решение с тернарен опеаратор:
  88.  
  89. import java.util.Scanner;
  90.  
  91. public class Main {
  92.     public static void main(String[] args) {
  93.         Scanner scanner = new Scanner(System.in);
  94.         String movie = scanner.nextLine(),
  95.                 pack = scanner.nextLine();
  96.         int tickets = Integer.parseInt(scanner.nextLine());
  97.  
  98.         double ticketPrice =
  99.                 movie.equals("John Wick")
  100.                 ? (pack.equals("Drink") ? 12 : pack.equals("Popcorn") ? 15 : 19)
  101.                 : movie.equals("Star Wars")
  102.                 ? (pack.equals("Drink") ? 18 : pack.equals("Popcorn") ? 25 : 30)
  103.                 : movie.equals("Jumanji")
  104.                 ? (pack.equals("Drink") ? 9 : pack.equals("Popcorn") ? 11 : 14) : 0;
  105.  
  106.         ticketPrice *=
  107.                 movie.equals("Star Wars") && tickets >= 4 ? 0.70
  108.                         : movie.equals("Jumanji") && tickets == 2 ? 0.85 : 1;
  109.  
  110.         System.out.printf("Your bill is %.2f leva.\n", ticketPrice * tickets);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement