-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVendingMachine_07.java
More file actions
98 lines (94 loc) · 3.47 KB
/
Copy pathVendingMachine_07.java
File metadata and controls
98 lines (94 loc) · 3.47 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
import java.util.Scanner;
public class VendingMachine_07 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String moneyInserted = null;
double sumMoney = 0;
boolean breakMe = false;
loop:
while (!breakMe) {
moneyInserted = scanner.nextLine();
switch (moneyInserted) {
case "0.1":
sumMoney += Double.parseDouble(moneyInserted);
break;
case "0.2":
sumMoney += Double.parseDouble(moneyInserted);
break;
case "0.5":
sumMoney += Double.parseDouble(moneyInserted);
break;
case "1":
sumMoney += Double.parseDouble(moneyInserted);
break;
case "2":
sumMoney += Double.parseDouble(moneyInserted);
break;
case "Start":
breakMe = true;
break loop;
default:
double wrongMoney = Double.parseDouble(moneyInserted);
System.out.printf("Cannot accept %.2f", wrongMoney);
System.out.println();
}
}
double price = 0;
loop2:
while (sumMoney > -0.01) {
String product = scanner.nextLine();
switch (product) {
case "Nuts":
price = 2.0;
if (price > sumMoney) {
System.out.println("Sorry, not enough money");
} else {
sumMoney -= price;
System.out.printf("Purchased %s%n", product);
}
break;
case "Water":
price = .7;
if (price > sumMoney) {
System.out.println("Sorry, not enough money");
} else {
sumMoney -= price;
System.out.printf("Purchased %s%n", product);
}
break;
case "Crisps":
price = 1.5;
if (price > sumMoney) {
System.out.println("Sorry, not enough money");
} else {
sumMoney -= price;
System.out.printf("Purchased %s%n", product);
}
break;
case "Soda":
price = .8;
if (price > sumMoney) {
System.out.println("Sorry, not enough money");
} else {
sumMoney -= price;
System.out.printf("Purchased %s%n", product);
}
break;
case "Coke":
price = 1;
if (price > sumMoney) {
System.out.println("Sorry, not enough money");
} else {
sumMoney -= price;
System.out.printf("Purchased %s%n", product);
}
break;
case "End":
break loop2;
default:
System.out.println("Invalid product");
}
}
System.out.printf("Change: %.2f", sumMoney);
}
}