-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularVendingMachine.java
More file actions
400 lines (345 loc) · 13.3 KB
/
RegularVendingMachine.java
File metadata and controls
400 lines (345 loc) · 13.3 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import java.util.ArrayList;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Scanner;
public class RegularVendingMachine {
private ArrayList<Slot> slots;
private Map<String, Item> inventory;
private Map<Item, Integer> startingInventory;
private Map<Item, Integer> endingInventory;
private double salesSum;
private Map<String, Integer> changeValue;
public RegularVendingMachine() {
this.slots = new ArrayList<>();
inventory = new HashMap<>();
startingInventory = new HashMap<>();
endingInventory = new HashMap<>();
salesSum = 0.0;
changeValue = new HashMap<>();
}
public void venMachineFeatures() {
System.out.println("Vending Machine Features:");
System.out.println("1. Display available items");
System.out.println("2. Purchase an item");
System.out.println("3. Return to previous menu");
System.out.print("Enter your choice: ");
int choice = readValidChoice(1, 3);
switch (choice) {
case 1:
displayAvailableItems();
break;
case 2:
purchaseItem();
break;
case 3:
return;
default:
System.out.println("Invalid selection! Please try again.");
venMachineFeatures();
}
}
private void displayAvailableItems() {
System.out.println("Available Items:");
Map<String, Item> inventory = getInventory();
for (Map.Entry<String, Item> entry : inventory.entrySet()) {
String slot = entry.getKey();
Item item = entry.getValue();
System.out.println(slot + " - " + item.getItemName() + " - $" + item.getItemPrice());
}
}
private void purchaseItem() {
String slot = readValidString("Enter the slot of the item you want to purchase: ");
double amount = readValidInteger(0, Integer.MAX_VALUE, "Enter the amount of money: ");
Item item = getItem(slot);
if (item != null) {
if (amount >= item.getItemPrice()) {
double change = amount - item.getItemPrice();
System.out.println("You purchased: " + item.getItemName());
System.out.println("Change: $" + change);
item.incrementUnitsSold(); // Increment units sold count
updateInventory(slot);
} else {
System.out.println("Insufficient funds. Please insert more money.");
}
} else {
System.out.println("Invalid slot. Please try again.");
}
}
public void restockItem() {
performMaintenance(); // Call the existing performMaintenance() method to restock items
System.out.println("Items restocked.");
}
public void addVenItem() {
System.out.println("Enter details for the new item:");
String itemName = readValidString("Name of item: ");
double price = readValidDouble(0, Integer.MAX_VALUE, "Enter a price for the item: ");
int calories = readValidInteger(0, Integer.MAX_VALUE, "Enter no. of calories of the item: ");
String slot = readValidString("Enter the slot for the item: ");
Item item = new Item();
item.setItemName(itemName);
item.setItemPrice(price);
item.setItemCalories(calories);
addItem(slot, item);
System.out.println("New item added:");
System.out.println("Name: " + item.getItemName());
System.out.println("Price: " + item.getItemPrice());
System.out.println("Calories: " + item.getItemCalories());
}
public void changeVenItem() {
String itemSlot = readValidString("Enter the item slot: ");
double newPrice = readValidDouble(0, Integer.MAX_VALUE, "Enter the new price for the item: ");
setPrice(itemSlot, newPrice);
System.out.println("Item price changed successfully.");
}
public void collectVenMoney() {
collectPayment(); // Call the existing collectPayment() method to collect money
System.out.println("Money collected.");
}
public void addVenMoney() {
String denomination = readValidString("Enter the denomination of the coin/bill to replenish: ");
insertCoin(denomination); // Assuming coins and bills have the same denomination values
System.out.println("Denomination replenished.");
}
public void printVenTransaction() {
System.out.println("========================================");
System.out.println("Regular Vending Machine Transaction Summary:");
System.out.println("========================================");
System.out.println("Items Sold:");
Map<Item, Integer> endingInventory = getEndingInventory();
for (Map.Entry<Item, Integer> entry : endingInventory.entrySet()) {
Item item = entry.getKey();
int quantity = entry.getValue();
int unitsSold = item.getUnitsSold(); // Get the number of units sold for this item
System.out.println(item.getItemName() + ": Quantity: " + itemCount + " units sold = (" + unitsSold + " total)");
}
}
public void addItem(String itemSlot, Item item) {
inventory.put(itemSlot, item);
startingInventory.put(item, 0);
endingInventory.put(item, 0);
}
public void deleteItem(String itemSlot) {
Item item = inventory.get(itemSlot);
if (item != null) {
inventory.remove(itemSlot);
startingInventory.remove(item);
endingInventory.remove(item);
}
}
public Item getItem(String itemSlot) {
return inventory.get(itemSlot);
}
public void setPrice(String itemSlot, double price) {
Item item = inventory.get(itemSlot);
if (item != null) {
item.setItemPrice((int) price);
}
}
public double getPrice(String itemSlot) {
Item item = inventory.get(itemSlot);
if (item != null) {
return item.getItemPrice();
}
return 0.0;
}
public int getCalories(String itemSlot) {
Item item = inventory.get(itemSlot);
if (item != null) {
return item.getItemCalories();
}
return 0;
}
public void insertCoin(String coinValue) {
Integer count = changeValue.getOrDefault(coinValue, 0);
changeValue.put(coinValue, count + 1);
}
public void insertBill(String billValue) {
Integer count = changeValue.getOrDefault(billValue, 0);
changeValue.put(billValue, count + 1);
}
public void selectItem(String itemSlot) {
Item item = inventory.get(itemSlot);
if (item != null) {
double price = item.getItemPrice();
if (salesSum >= price) {
int startingCount = startingInventory.getOrDefault(item, 0);
int endingCount = endingInventory.getOrDefault(item, 0);
if (startingCount > endingCount) {
// Item available
startingInventory.put(item, startingCount - 1);
endingInventory.put(item, endingCount + 1);
salesSum += price; // Update the sales count
dispenseItem(item);
giveChange(price);
} else {
// Item out of stock
System.out.println("Item is out of stock.");
}
} else {
// Insufficient funds
System.out.println("Insufficient funds. Please insert more coins or bills.");
}
}
}
public void cancelTransaction() {
returnChange();
}
private void dispenseItem(Item item) {
System.out.println("Dispensing " + item.getItemName() + ".");
}
private void giveChange(double price) {
double change = salesSum - price;
if (change > 0) {
System.out.println("Change: " + change);
salesSum = 0.0;
returnChange();
}
}
private void returnChange() {
System.out.println("Returning change:");
for (Map.Entry<String, Integer> entry : changeValue.entrySet()) {
String coinOrBill = entry.getKey();
int count = entry.getValue();
if (count > 0) {
System.out.println(coinOrBill + " x " + count);
changeValue.put(coinOrBill, 0);
}
}
}
public void performMaintenance() {
replenishItems();
collectPayment();
replenishChange();
}
private void replenishItems() {
for (Map.Entry<Item, Integer> entry : endingInventory.entrySet()) {
Item item = entry.getKey();
int count = entry.getValue();
startingInventory.put(item, count);
endingInventory.put(item, 0);
}
System.out.println("Items replenished.");
}
public void collectPayment() {
salesSum = 0.0;
System.out.println("Payment collected.");
}
public void replenishChange() {
for (Map.Entry<String, Integer> entry : changeValue.entrySet()) {
String coinOrBill = entry.getKey();
int count = entry.getValue();
if (count == 0) {
changeValue.put(coinOrBill, 10); // Assuming a capacity of 10 coins/bills for each denomination
}
}
System.out.println("Change replenished.");
}
public Map<Item, Integer> getStartingInventory() {
return startingInventory;
}
public Map<Item, Integer> getEndingInventory() {
return endingInventory;
}
private int readValidChoice(int min, int max) {
Scanner scanner = new Scanner(System.in);
int input;
while (true) {
try {
input = scanner.nextInt();
if (input >= min && input <= max) {
break;
} else {
System.out.println("Invalid input. Please try again.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please try again.");
scanner.nextLine(); // Clear the invalid input from the scanner
}
}
return input;
}
private int readValidInteger(int min, int max, String message) {
Scanner scanner = new Scanner(System.in);
int input;
while (true) {
System.out.print(message);
try {
input = scanner.nextInt();
if (input >= min && input <= max) {
break;
} else {
System.out.println("Invalid input. Please try again.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please try again.");
scanner.nextLine(); // Clear the invalid input from the scanner
}
}
return input;
}
private double readValidDouble(double min, double max, String message) {
Scanner scanner = new Scanner(System.in);
double input;
while (true) {
System.out.print(message);
try {
input = scanner.nextDouble();
if (input >= min && input <= max) {
break;
} else {
System.out.println("Invalid input. Please try again.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please try again.");
scanner.nextLine(); // Clear the invalid input from the scanner
}
}
return input;
}
private String readValidString(String message) {
Scanner scanner = new Scanner(System.in);
String input;
while (true) {
System.out.print(message);
input = scanner.nextLine();
if (!input.isEmpty()) {
break;
} else {
System.out.println("Invalid input. Please try again.");
}
}
return input;
}
public Map<String, Item> getInventory() {
return inventory;
}
public void updateInventory(String itemName) {
for (Slot slot : slots) {
ArrayList<Item> itemSlot = slot.getItemSlot();
for (Item item : itemSlot) {
if (item.getItemName().equals(itemName)) {
item.setItemQuantity(item.getItemQuantity() + 1);
System.out.println("Inventory updated for item: " + itemName);
return;
}else {
System.out.println("Item not found in inventory: " + itemName);
}
}
}
}
private int lastSlotNumber = 0; // Add this instance variable
public String generateNewItemSlot() {
lastSlotNumber++; // Increment the last slot number
String newSlot = String.valueOf(lastSlotNumber); // Convert the slot number to a string
System.out.println("New item slot generated: " + newSlot);
return newSlot;
}
public void addSlot(Slot slot) {
slots.add(slot);
}
private int itemCount;
public void setItemCount(int itemCount) {
this.itemCount = itemCount;
}
}