Advertisement
veronikaaa86

11. Fruit Shop

May 22nd, 2021
1,068
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 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 workingDay = dayOfWeek.equals("Monday") || dayOfWeek.equals("Tuesday") || dayOfWeek.equals("Wednesday") || dayOfWeek.equals("Thursday") || dayOfWeek.equals("Friday");
  14.         boolean weekend = dayOfWeek.equals("Saturday") || dayOfWeek.equals("Sunday");
  15.  
  16.         boolean flag = true;
  17.  
  18.         double priceKG = 0;
  19.         if (workingDay) {
  20.             if ("banana".equals(fruit)) {
  21.                 priceKG = 2.50;
  22.             } else if ("apple".equals(fruit)) {
  23.                 priceKG = 1.20;
  24.             } else if ("orange".equals(fruit)) {
  25.                 priceKG = 0.85;
  26.             } else if ("grapefruit".equals(fruit)) {
  27.                 priceKG = 1.45;
  28.             } else if ("kiwi".equals(fruit)) {
  29.                 priceKG = 2.70;
  30.             } else if ("pineapple".equals(fruit)) {
  31.                 priceKG = 5.50;
  32.             } else if ("grapes".equals(fruit)) {
  33.                 priceKG = 3.85;
  34.             } else {
  35.                 flag = false;
  36.             }
  37.         } else if (weekend) {
  38.             if ("banana".equals(fruit)) {
  39.                 priceKG = 2.70;
  40.             } else if ("apple".equals(fruit)) {
  41.                 priceKG = 1.25;
  42.             } else if ("orange".equals(fruit)) {
  43.                 priceKG = 0.90;
  44.             } else if ("grapefruit".equals(fruit)) {
  45.                 priceKG = 1.60;
  46.             } else if ("kiwi".equals(fruit)) {
  47.                 priceKG = 3;
  48.             } else if ("pineapple".equals(fruit)) {
  49.                 priceKG = 5.60;
  50.             } else if ("grapes".equals(fruit)) {
  51.                 priceKG = 4.20;
  52.             } else {
  53.                 flag = false;
  54.             }
  55.         } else {
  56.             flag = false;
  57.         }
  58.         if (flag) {
  59.             double totalPrice = quantity * priceKG;
  60.             System.out.printf("%.2f", totalPrice);
  61.  
  62.         } else {
  63.             System.out.println("error");
  64.         }
  65.     }
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement