-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCafeManagementSystem.java
More file actions
256 lines (208 loc) · 10.2 KB
/
Copy pathCafeManagementSystem.java
File metadata and controls
256 lines (208 loc) · 10.2 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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class CafeManagementSystem {
public static void displayMenu(
String[] itemNames,
double[] itemPrices,
int itemCount
) {
System.out.println("\n===== MENU =====");
for (int i = 0; i < itemCount; i++) {
System.out.printf(
"%d. %s - %.2f\n",
(i + 1),
itemNames[i],
itemPrices[i]
);
}
}
public static void saveMenuToFile(String[] itemNames, double[] itemPrices, int itemCount) {
try {
FileWriter fw = new FileWriter("menu.txt");
PrintWriter pw = new PrintWriter(fw);
for (int i = 0; i < itemCount; i++) {
pw.println(itemNames[i] + "," + itemPrices[i]);
}
pw.close();
} catch (IOException e) {
System.out.println("Error saving menu.");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] itemNames = new String[20];
double[] itemPrices = new double[20];
int itemCount = 0;
try {
File file = new File("menu.txt");
Scanner fileReader = new Scanner(file);
while (fileReader.hasNextLine()) {
String line = fileReader.nextLine();
String[] parts = line.split(",");
itemNames[itemCount] = parts[0];
itemPrices[itemCount] = Double.parseDouble(parts[1]);
itemCount++;
}
fileReader.close();
} catch (FileNotFoundException e) {
System.out.println("Menu file not found");
}
int choice;
do {
System.out.println("\n===== CAFE MANAGEMENT SYSTEM =====");
System.out.println("1. Customer Panel");
System.out.println("2. Admin Panel");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1 -> {
int customerChoice = 0;
do {
System.out.println("\n===== CUSTOMER PANEL =====");
System.out.println("1. View Menu");
System.out.println("2. Place Order");
System.out.println("3. Back");
System.out.print("Enter your choice: ");
customerChoice = sc.nextInt();
switch (customerChoice) {
case 1 -> displayMenu(
itemNames,
itemPrices,
itemCount
);
case 2 -> {
int itemChoice = 0;
double totalBill = 0;
String[] orderedItems = new String[20];
int[] orderedQuantities = new int[20];
int orderedCount = 0;
do {
displayMenu(
itemNames,
itemPrices,
itemCount
);
System.out.println(
"Press 0 to confirm your order."
);
System.out.print("Enter your choice: ");
itemChoice = sc.nextInt();
if (
itemChoice >= 1 &&
itemChoice <= itemCount
) {
System.out.print("Enter quantity: ");
int quantity = sc.nextInt();
totalBill +=
quantity *
itemPrices[itemChoice - 1];
boolean found = false;
for (int i = 0; i < orderedCount; i++) {
if (
orderedItems[i].equals(
itemNames[itemChoice - 1]
)
) {
orderedQuantities[i] +=
quantity;
found = true;
}
}
if (!found) {
orderedItems[orderedCount] =
itemNames[itemChoice - 1];
orderedQuantities[orderedCount] =
quantity;
orderedCount++;
}
} else if (itemChoice != 0) {
System.out.println(
"Invalid choice! Try again."
);
}
} while (itemChoice != 0);
System.out.println(
"\n===== CUSTOMER RECEIPT ====="
);
for (int i = 0; i < orderedCount; i++) {
System.out.printf(
"%s x%d%n",
orderedItems[i],
orderedQuantities[i]
);
}
System.out.println("Total Bill: " + totalBill);
try {
FileWriter fw = new FileWriter("orders.txt", true);
PrintWriter pw = new PrintWriter(fw);
pw.println("===== CUSTOMER RECEIPT =====");
for(int i = 0; i < orderedCount; i++) {
pw.printf("%s x%d%n", orderedItems[i], orderedQuantities[i]);
}
pw.println("Total Bill: " + totalBill);
pw.println("------------------------");
pw.close();
System.out.println("Order saved to the file successfully.");
} catch (IOException e) {
System.out.println("Error saving file.");
}
}
case 3 -> System.out.println("Going back...");
default -> System.out.println(
"Invalid choice! Try again."
);
}
} while (customerChoice != 3);
}
case 2 -> {
int adminChoice;
do {
System.out.println("\n===== ADMIN PANEL =====");
System.out.println("1. View Menu");
System.out.println("2. Add Item");
System.out.println("3. Remove Item");
System.out.println("4. Back");
System.out.print("Enter your choice: ");
adminChoice = sc.nextInt();
switch(adminChoice) {
case 1 -> displayMenu(itemNames, itemPrices, itemCount);
case 2 -> {
sc.nextLine();
System.out.print("Enter item name: ");
String itemName = sc.nextLine();
System.out.print("Enter item price: ");
double itemPrice = sc.nextDouble();
itemNames[itemCount] = itemName;
itemPrices[itemCount] = itemPrice;
itemCount++;
saveMenuToFile(itemNames, itemPrices, itemCount);
System.out.println("Item added successfully!");
}
case 3 -> {
displayMenu(itemNames, itemPrices, itemCount);
System.out.print("Enter item number: ");
int itemNumber = sc.nextInt();
for (int i = itemNumber - 1; i < itemCount - 1; i++) {
itemNames[i] = itemNames[i + 1];
itemPrices[i] = itemPrices[i + 1];
}
itemCount--;
saveMenuToFile(itemNames, itemPrices, itemCount);
System.out.println("Item removed successfully!");
}
case 4 -> System.out.println("Going back....");
default -> System.out.println("Invalid choice! Try again.");
}
} while (adminChoice != 4);
}
case 3 -> System.out.println("Exiting program...");
default -> System.out.println("Invalid choice! Try again");
}
} while (choice != 3);
}
}