Repository navigation
Expand file tree
/
Copy pathmainmenu.java
More file actions
154 lines (134 loc) · 5.35 KB
/
Copy pathmainmenu.java
File metadata and controls
154 lines (134 loc) · 5.35 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
import java.util.Scanner;
import java.util.ArrayList;
public class mainmenu
{
int choice;
int balance = 100;
Scanner scanner = new Scanner(System.in);
ArrayList<menulist> menu = new ArrayList<>();
ArrayList<String> transactionLog = new ArrayList<>(); // Add transaction tracking
public void print(String string){
System.out.println(string);
}
public mainmenu()
{
initializeMenu();
do {
printMenu();
getOrder();
if(choice == -1) { // Check admin access first
new adminmenu(transactionLog, menu);
} else if(choice < 0 || choice > 10) {
print("Invalid selection, please try again.");
} else if(choice != 0) {
getMenu(choice);
}
} while(choice != 0);
}
public void initializeMenu() {
// Indonesian Snacks (codes 1-5) - all with stock of 5
menu.add(new menulist(1, "Sukro", 8, 5));
menu.add(new menulist(2, "GoodTime", 12, 5));
menu.add(new menulist(3, "Chitato", 10, 5));
menu.add(new menulist(4, "Taro", 9, 5));
menu.add(new menulist(5, "Richeese", 15, 5));
// Soft Drinks (codes 6-10) - all with stock of 5
menu.add(new menulist(6, "Coke", 5, 5));
menu.add(new menulist(7, "Pepsi", 5, 5));
menu.add(new menulist(8, "Sprite", 4, 5));
menu.add(new menulist(9, "Fanta", 4, 5));
menu.add(new menulist(10, "Lemonade", 6, 5));
}
public void getMenu(int choice) {
boolean found = false;
String buy = "NULL";
int productprice = -1;
menulist selectedItem = null;
for(menulist menulist : menu) {
if(menulist.getCode() == choice) {
print(menulist.toString());
found = true;
productprice = menulist.price;
selectedItem = menulist;
if(selectedItem.getStock() <= 0) {
print("Sorry, this item is out of stock!");
return;
}
System.out.println("Would you like to buy the selected item? Y/N");
buy = scanner.nextLine().toLowerCase();
break;
}
}
if(!found) {
print("Item not found.");
return;
}
if(buy.contains("y")) {
System.out.println("How many would you like to buy? (Available: " + selectedItem.getStock() + ")");
int quantity = scanner.nextInt();
scanner.nextLine(); // consume newline
if(quantity <= 0) {
print("Invalid quantity. Purchase cancelled.");
return;
}
if(quantity > selectedItem.getStock()) {
print("Sorry, we only have " + selectedItem.getStock() + " items in stock.");
return;
}
int totalCost = productprice * quantity;
if(this.balance >= totalCost) {
this.balance -= totalCost;
selectedItem.reduceStock(quantity);
// Log the transaction
String transaction = "Purchased " + quantity + " x " + selectedItem.getName() +
" for $" + totalCost + " (Remaining balance: $" + this.balance + ")";
transactionLog.add(transaction);
print("Thank you for purchasing " + quantity + " x " + selectedItem.getName() +
" for $" + totalCost + ". Remaining balance: " + this.balance + " USD");
} else {
print("You do not have sufficient balance. Total cost: $" + totalCost +
". You have " + this.balance + " USD");
// Log the failed transaction
transactionLog.add("FAILED: Attempted to buy " + quantity + " x " + selectedItem.getName() +
" for $" + totalCost + " (Insufficient funds - had $" + this.balance + ")");
}
} else if (buy.contains("n")) {
print("Purchase cancelled.");
} else {
print("Invalid input. Purchase cancelled.");
}
}
public void getOrder() {
choice = scanner.nextInt();
scanner.nextLine();
}
public void printMenu() {
print("MENU : \n");
print("Indonesian Snacks \n-------------------- \n");
print("1. Sukro \n");
print("2. GoodTime \n");
print("3. Chitato \n");
print("4. Taro \n");
print("5. Richeese \n");
print("-------------------- \n");
print("Soft Drinks \n-------------------- \n");
print("6. Coke \n");
print("7. Pepsi \n");
print("8. Sprite \n");
print("9. Fanta \n");
print("10. Lemonade \n");
print("-------------------- \n");
print("What would you like to order? (Enter the item number), '0' to exit, '-1' for admin \n");
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int sampleMethod(int y)
{
return 0;
// put your code here
}
}