-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoffeeMachineMenu.java
More file actions
117 lines (103 loc) · 3.59 KB
/
Copy pathCoffeeMachineMenu.java
File metadata and controls
117 lines (103 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.util.Scanner;
public class CoffeeMachineMenu {
private static int water = 400;
private static int milk = 540;
private static int beans = 120;
private static int cups = 9;
private static double money = 50.0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("\n---- Coffee Machine Menu ----");
System.out.println("1: Buy a Coffee");
System.out.println("2: Take the Money");
System.out.println("3: Check Machine Status");
System.out.println("4: Turn Off Machine");
System.out.print("Choose Option: ");
if (!sc.hasNextInt()) {
System.out.println("Invalid input! Enter a number.");
sc.next();
continue;
}
int choice = sc.nextInt();
switch (choice) {
case 1:
buyCoffee(sc);
break;
case 2:
takeMoney();
break;
case 3:
printStatus();
break;
case 4:
System.out.println("Machine turned off.");
sc.close();
return;
default:
System.out.println("Invalid choice!");
}
}
}
private static void buyCoffee(Scanner sc) {
System.out.println("\n--- Choose Coffee ---");
System.out.println("1: Espresso (250ml water, 16g beans, 1 cup) - $4.00");
System.out.println("2: Latte (350ml water, 75ml milk, 20g beans, 1 cup) - $7.00");
System.out.println("3: Cappuccino (200ml water, 100ml milk, 12g beans, 1 cup) - $6.00");
System.out.print("Enter Choice: ");
if (!sc.hasNextInt()) {
System.out.println("Invalid coffee choice.");
sc.next();
return;
}
int type = sc.nextInt();
int requiredWater = 0;
int requiredMilk = 0;
int requiredBeans = 0;
double cost = 0.0;
switch (type) {
case 1:
requiredWater = 250;
requiredBeans = 16;
cost = 4.00;
break;
case 2:
requiredWater = 350;
requiredMilk = 75;
requiredBeans = 20;
cost = 7.00;
break;
case 3:
requiredWater = 200;
requiredMilk = 100;
requiredBeans = 12;
cost = 6.00;
break;
default:
System.out.println("Invalid Coffee Choice");
return;
}
if (water >= requiredWater && milk >= requiredMilk
&& beans >= requiredBeans && cups >= 1) {
water -= requiredWater;
milk -= requiredMilk;
beans -= requiredBeans;
cups--;
money += cost;
System.out.println("Making your coffee... Enjoy!");
} else {
System.out.println("Insufficient ingredients.");
}
}
private static void takeMoney() {
System.out.printf("You took $%.2f%n", money);
money = 0.0;
}
private static void printStatus() {
System.out.println("Water: " + water + " ml");
System.out.println("Milk: " + milk + " ml");
System.out.println("Beans: " + beans + " g");
System.out.println("Cups: " + cups);
System.out.printf("Money: $%.2f%n", money);
}
}