Advertisement
veronikaaa86

11. Fruit Shop

Mar 12th, 2022
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. package advancedConditionalStatements;
  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. double price = 0;
  14. boolean isValid = true;
  15.  
  16. switch (dayOfWeek) {
  17. case "Monday":
  18. case "Tuesday":
  19. case "Wednesday":
  20. case "Thursday":
  21. case "Friday":
  22. if (fruit.equals("banana")) {
  23. price = 2.5;
  24. } else if (fruit.equals("apple")) {
  25. price = 1.2;
  26. } else if (fruit.equals("orange")) {
  27. price = 0.85;
  28. } else if (fruit.equals("grapefruit")) {
  29. price = 1.45;
  30. } else if (fruit.equals("kiwi")) {
  31. price = 2.70;
  32. } else if (fruit.equals("pineapple")) {
  33. price = 5.50;
  34. } else if (fruit.equals("grapes")) {
  35. price = 3.85;
  36. } else {
  37. isValid = false;
  38. }
  39. break;
  40. case "Saturday":
  41. case "Sunday":
  42. if (fruit.equals("banana")) {
  43. price = 2.70;
  44. } else if (fruit.equals("apple")) {
  45. price = 1.25;
  46. } else if (fruit.equals("orange")) {
  47. price = 0.90;
  48. } else if (fruit.equals("grapefruit")) {
  49. price = 1.60;
  50. } else if (fruit.equals("kiwi")) {
  51. price = 3.00;
  52. } else if (fruit.equals("pineapple")) {
  53. price = 5.60;
  54. } else if (fruit.equals("grapes")) {
  55. price = 4.20;
  56. } else {
  57. isValid = false;
  58. }
  59. break;
  60.  
  61. default:
  62. isValid = false;
  63. }
  64. double total = price * quantity;
  65. if (!isValid) {
  66. System.out.println("error");
  67. } else {
  68. System.out.printf("%.2f", total);
  69. }
  70. }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement