Advertisement
Spocoman

Fitness Card

Sep 6th, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 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.         double cash = Double.parseDouble(scanner.nextLine()), sum;
  7.         String gender = scanner.nextLine();
  8.         int age = Integer.parseInt(scanner.nextLine());
  9.         String sport = scanner.nextLine();
  10.  
  11.         if (gender.equals("m")) {
  12.             sum = switch (sport) {
  13.                 case "Gym" -> 42;
  14.                 case "Boxing" -> 41;
  15.                 case "Yoga" -> 45;
  16.                 case "Zumba" -> 34;
  17.                 case "Dances" -> 51;
  18.                 case "Pilates" -> 39;
  19.                 default -> 0;
  20.             };
  21.         } else {
  22.             sum = switch (sport) {
  23.                 case "Gym" -> 35;
  24.                 case "Boxing" -> 37;
  25.                 case "Yoga" -> 42;
  26.                 case "Zumba" -> 31;
  27.                 case "Dances" -> 53;
  28.                 case "Pilates" -> 37;
  29.                 default -> 0;
  30.             };
  31.         }
  32.  
  33.         if (age <= 19) {
  34.             sum *= 0.8;
  35.         }
  36.  
  37.         if (sum <= cash) {
  38.             System.out.println("You purchased a 1 month pass for " + sport + ".");
  39.         } else {
  40.             System.out.printf("You don't have enough money! You need $%.2f more.\n", sum - cash);
  41.         }
  42.     }
  43. }
  44.  
  45. Решение със switch и тернарен оператор:
  46.  
  47. import java.io.PrintStream;
  48. import java.util.Scanner;
  49.  
  50. public class Main {
  51.     public static void main(String[] args) {
  52.         Scanner scanner = new Scanner(System.in);
  53.         double cash = Double.parseDouble(scanner.nextLine());
  54.         String gender = scanner.nextLine();
  55.         int age = Integer.parseInt(scanner.nextLine());
  56.         String sport = scanner.nextLine();
  57.  
  58.         cash -= switch (sport) {
  59.             case "Gym" -> gender.equals("m") ? 42 : 35;
  60.             case "Boxing" -> gender.equals("m") ? 41 : 37;
  61.             case "Yoga" -> gender.equals("m") ? 45 : 42;
  62.             case "Zumba" -> gender.equals("m") ? 34 : 31;
  63.             case "Dances" -> gender.equals("m") ? 51 : 53;
  64.             case "Pilates" -> gender.equals("m") ? 39 : 37;
  65.             default -> 0;
  66.         } * (age <= 19 ? 0.8 : 1);
  67.  
  68.         PrintStream p = cash >= 0
  69.                 ? System.out.printf("You purchased a 1 month pass for %s.\n", sport)
  70.                 : System.out.printf("You don't have enough money! You need $%.2f more.\n", Math.abs(cash));
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement