Advertisement
veronikaaa86

11. Fruit Shop

May 27th, 2023
1,385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. package conditionalStatementsAdvanced;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P11FruitShop {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String fruit = scanner.nextLine();
  10.         String dayOfWeek = scanner.nextLine();
  11.         double quantity = Double.parseDouble(scanner.nextLine());
  12.  
  13.         boolean isWorkingDay = dayOfWeek.equals("Monday")
  14.                 || dayOfWeek.equals("Tuesday")
  15.                 || dayOfWeek.equals("Wednesday")
  16.                 || dayOfWeek.equals("Thursday")
  17.                 || dayOfWeek.equals("Friday");
  18.  
  19.         boolean isWeekend = dayOfWeek.equals("Saturday")
  20.                 || dayOfWeek.equals("Sunday");
  21.  
  22.         boolean isValid = true;
  23.  
  24.         double price = 0;
  25.         if (isWorkingDay) {
  26.             if (fruit.equals("banana")) {
  27.                 price = 2.5;
  28.             } else if (fruit.equals("apple")) {
  29.                 price = 1.2;
  30.             }  else if (fruit.equals("orange")) {
  31.                 price = 0.85;
  32.             } else if (fruit.equals("grapefruit")) {
  33.                 price = 1.45;
  34.             } else if (fruit.equals("kiwi")) {
  35.                 price = 2.7;
  36.             } else if (fruit.equals("pineapple")) {
  37.                 price = 5.5;
  38.             } else if (fruit.equals("grapes")) {
  39.                 price = 3.85;
  40.             } else {
  41.                 isValid = false;
  42.             }
  43.         } else if (isWeekend) {
  44.             if (fruit.equals("banana")) {
  45.                 price = 2.7;
  46.             } else if (fruit.equals("apple")) {
  47.                 price = 1.25;
  48.             }  else if (fruit.equals("orange")) {
  49.                 price = 0.90;
  50.             } else if (fruit.equals("grapefruit")) {
  51.                 price = 1.6;
  52.             } else if (fruit.equals("kiwi")) {
  53.                 price = 3;
  54.             } else if (fruit.equals("pineapple")) {
  55.                 price = 5.6;
  56.             } else if (fruit.equals("grapes")) {
  57.                 price = 4.2;
  58.             } else {
  59.                 isValid = false;
  60.             }
  61.         } else {
  62.             isValid = false;
  63.         }
  64.  
  65.         if (isValid) {
  66.             double result = quantity * price;
  67.             System.out.printf("%.2f", result);
  68.         } else {
  69.             System.out.println("error");
  70.         }
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement