-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
247 lines (219 loc) · 8.58 KB
/
Main.java
File metadata and controls
247 lines (219 loc) · 8.58 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*ESCOBAR, PRINCESS - SANTIAGO, MONICA*/
import java.io.Console;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
private Scanner scanner;
public Main() {
scanner = new Scanner(System.in);
}
public int mainMenu() {
System.out.println("==============================================");
System.out.println(" █░░░█ █▀▀ █░░ █▀▀ █▀▀█ █▀▄▀█ █▀▀");
System.out.println(" █▄█▄█ █▀▀ █░░ █░░ █░░█ █░▀░█ █▀▀");
System.out.println(" ░▀░▀░ ▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀▀ ▀░░░▀ ▀▀▀");
System.out.println("==============================================\n\n\n");
System.out.println("What brings you here today?\n");
System.out.println("[1] Create a Vending Machine");
System.out.println("[2] Test a Vending Machine");
System.out.println("[3] Exit");
System.out.print("Enter selection: ");
return readInteger(1, 3);
}
public int displayTestMenu() {
System.out.println("Choose a feature to test: \n");
System.out.println("[1] Test Vending Machine Features");
System.out.println("[2] Perform Vending Machine Maintenance");
System.out.println("[3] Exit");
System.out.print("Enter your choice: ");
return readInteger(1, 3);
}
public int displayMaintMenu() {
System.out.println("[1] Restock Items");
System.out.println("[2] Add Items");
System.out.println("[3] Change Item Price");
System.out.println("[4] Collect Money");
System.out.println("[5] Replenish Denomination");
System.out.println("[6] Print Transaction History");
System.out.println("[7] Exit");
System.out.println("Enter your selection: ");
return readInteger(1, 7);
}
public static void main(String[] args) {
Main m = new Main();
m.startProgram();
}
public void startProgram() {
RegularVendingMachine vendingMachine = null;
while (true) {
int choice = mainMenu();
switch (choice) {
case 1:
vendingMachine = createVM();
break;
case 2:
if (vendingMachine != null) {
testVM(vendingMachine);
} else {
System.out.println("Error: Vending machine not found.");
}
break;
case 3:
System.exit(0);
break;
default:
System.out.println("Invalid selection! Please try again.");
}
}
}
private RegularVendingMachine currentVendingMachine;
public RegularVendingMachine createVM() {
System.out.println("Choose your Vending Machine option:\n");
System.out.println("[1] Regular Vending Machine");
System.out.println("[2] Special Vending Machine");
System.out.println("[0] Back");
int choice = readInteger(0, 2);
RegularVendingMachine vendingMachine = null;
switch (choice) {
case 0:
System.out.println("Returning to main menu.");
break;
case 1:
vendingMachine = createRegVM();
break;
case 2:
System.out.println("Service will open soon. Please check back later.");
break;
}
if (vendingMachine != null) {
currentVendingMachine = vendingMachine;
}
return currentVendingMachine;
}
public RegularVendingMachine createRegVM() {
RegularVendingMachine vendingMachine = new RegularVendingMachine();
int numSlots = readValidInteger(1, 8, "Enter no. of slot items (MAX: 8): ");
int itemCount = readValidInteger(1, 10, "Enter no. of items (MAX: 10): ");
vendingMachine.setItemCount(itemCount);
for (int i = 0; i < numSlots; i++) {
System.out.println("===================================");
System.out.println("Enter details for Item " + (i + 1) + ":");
System.out.println("===================================");
String itemName = readString("Name of item: ");
double price = readValidDouble(0, Double.MAX_VALUE, "Enter a price for the item: ");
int calories = readValidInteger(0, Integer.MAX_VALUE, "Enter no. of calories of the item: ");
Item item = new Item();
item.setItemName(itemName);
item.setItemPrice(price);
item.setItemCalories(calories);
vendingMachine.addItem(String.valueOf(i + 1), item);
System.out.println("Item " + (i + 1) + ":");
System.out.println("Name: " + item.getItemName());
System.out.println("Price: " + item.getItemPrice());
System.out.println("Calories: " + item.getItemCalories());
}
System.out.println("Your Regular Vending Machine is created successfully!");
System.out.println("Enjoy your goodies...");
return vendingMachine;
}
public int readInteger(int min, int max) {
int value;
while (true) {
try {
value = Integer.parseInt(scanner.nextLine());
if (value >= min && value <= max) {
break;
} else {
System.out.println("Invalid input. Please enter a number between " + min + " and " + max + ".");
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid integer.");
}
}
return value;
}
public int readValidInteger(int min, int max, String message) {
int value;
while (true) {
System.out.print(message);
try {
value = Integer.parseInt(scanner.nextLine());
if (value >= min && value <= max) {
break;
} else {
System.out.println("Invalid input. Please enter a number between " + min + " and " + max + ".");
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid integer.");
}
}
return value;
}
public double readValidDouble(double min, double max, String message) {
double value;
while (true) {
System.out.print(message);
try {
value = Double.parseDouble(scanner.nextLine());
if (value >= min && value <= max) {
break;
} else {
System.out.println("Invalid input. Please enter a number between " + min + " and " + max + ".");
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid number.");
}
}
return value;
}
public String readString(String message) {
System.out.print(message);
return scanner.nextLine();
}
public void testVM(RegularVendingMachine vm) {
while (true) {
int choice = displayTestMenu();
switch (choice) {
case 1:
vm.venMachineFeatures();
break;
case 2:
checkMaintenance(vm);
break;
case 3:
return;
default:
System.out.println("Invalid selection! Please try again.");
}
}
}
public void checkMaintenance(RegularVendingMachine vm) {
while (true) {
int choice = displayMaintMenu();
switch (choice) {
case 1:
vm.restockItem();
break;
case 2:
vm.addVenItem();
break;
case 3:
vm.changeVenItem();
break;
case 4:
vm.collectVenMoney();
break;
case 5:
vm.addVenMoney();
break;
case 6:
vm.printVenTransaction();
break;
case 7:
return;
default:
System.out.println("Invalid Selection! Please try again.");
}
}
}
}