Advertisement
Spocoman

11. Fruit Shop

Aug 25th, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class FruitShop {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String fruit = scanner.nextLine();
  7.         String day = scanner.nextLine();
  8.         double quantity = Double.parseDouble(scanner.nextLine());
  9.         double price = 0;
  10.  
  11.         if (day.equals("Monday")
  12.                 || day.equals("Tuesday")
  13.                 || day.equals("Wednesday")
  14.                 || day.equals("Thursday")
  15.                 || day.equals("Friday")) {
  16.             price = switch (fruit) {
  17.                 case "banana" -> 2.50;
  18.                 case "apple" -> 1.20;
  19.                 case "orange" -> 0.85;
  20.                 case "grapefruit" -> 1.45;
  21.                 case "kiwi" -> 2.70;
  22.                 case "pineapple" -> 5.50;
  23.                 case "grapes" -> 3.85;
  24.                 default -> price;
  25.             };
  26.         } else if (day.equals("Saturday")
  27.                 || day.equals("Sunday")) {
  28.             price = switch (fruit) {
  29.                 case "banana" -> 2.70;
  30.                 case "apple" -> 1.25;
  31.                 case "orange" -> 0.90;
  32.                 case "grapefruit" -> 1.60;
  33.                 case "kiwi" -> 3.00;
  34.                 case "pineapple" -> 5.60;
  35.                 case "grapes" -> 4.20;
  36.                 default -> price;
  37.             };
  38.         }
  39.  
  40.         if (price == 0) {
  41.             System.out.println("error");
  42.         } else {
  43.             System.out.printf("%.2f\n", price * quantity);
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement