Repository navigation
Expand file tree
/
Copy pathMenu
More file actions
387 lines (345 loc) · 14 KB
/
Copy pathMenu
File metadata and controls
387 lines (345 loc) · 14 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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;
public class Main {
private static final String REVIEWS_FILE = "reviews.txt";
public static final String RECORDS_FILE = "UsernameNpassword";
List<String> cartitems = new ArrayList<>();
List<Double> cartPrices = new ArrayList<>();
List<Integer> cartQuantities = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String currentUser = null; //Store Username
String[] coffeeNames = {"Americano", "Espresso", "Latte", "Cappuccino", "Mocha", "Flat White", "Back to Menu"};
double[] coffeePrices = {2.50, 2.75, 3.00, 3.50, 4.00, 3.25, 0.00};
String[] foodNames = {"Fried rice", "Noodle", "Beef stewed with bread", "Brownie", "Back to Menu"};
double[] foodPrices = {3.25, 3.00, 2.50, 1.00, 0.00};//Store the Username
while (true) {
System.out.println("Choose an operation:");
System.out.println("1. Sign-Up");
System.out.println("2. Login");
System.out.println("3. Exit");
String choice = scanner.nextLine();
switch (choice) {
case "1":
currentUser = register();
if (currentUser != null) {
System.out.println("Welcome to Javalicious " + currentUser);
} else {
System.out.println("Registration failed");
continue;
}
break;
case "2":
currentUser = checkLoginUsernamePassword();
if (currentUser == null) {
System.out.println("Welcome to Javalicious " + currentUser);
continue;
}
break;
case "3":
System.out.println("Exiting... Goodbye!");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
continue;
}
break;
}
List<CartItem> cart = new ArrayList<>();
//Delivery or Pick Up
while (true) {
System.out.println("Pick up or order from our app?");
String pickUpOrOrder = scanner.nextLine().trim();
if (pickUpOrOrder.equalsIgnoreCase("order")) {
System.out.print("Please provide your Gmail address: ");
String gmailAddress = scanner.nextLine();
break;
} else if (pickUpOrOrder.equalsIgnoreCase("pick up")) {
System.out.println("Got it, thank you!");
break;
} else {
System.out.println("Invalid information.");
}
}
while (true) {
System.out.println("What would you like to do?");
System.out.println("1. Coffee");
System.out.println("2. Food");
System.out.println("3. Go to Cart");
System.out.println("4. Remove item");
System.out.println("5. Proceed to checkout");
System.out.println("6. Exit");
System.out.println("Please choose an option:");
String choice = scanner.nextLine().trim();
switch (choice) {
case "1":
addItem(scanner, "Coffee", coffeeNames, coffeePrices, cart);
break;
case "2":
addItem(scanner, "Food", foodNames, foodPrices, cart);
break;
case "3":
viewCart(cart);
break;
case "4":
removeItem(scanner, cart);
break;
case "5":
printCart(cart);
System.out.println("Thank you for your order 🙏 ");
break;
case "6": System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
continue;
}
if (choice.equals("5")) break;
}
System.out.println("Would You like to leave a review 😊");
Scanner input = new Scanner(System.in);
String choice = input.nextLine();
while (true) {
if (choice.equals("yes")) {
leaveReview(currentUser);
break;
} else if (choice.equals("no")) {
System.out.println("Have a great day!!");
break;
}
else {
System.out.println("Invalid choice. Please try again.");
}
}
displayReviews();
scanner.close();
}
public static void addItem (Scanner scanner, String type, String[] items, double[] prices, List<CartItem> cart) {
System.out.println("\n===== " + type + " Menu =====");
for (int i = 0; i < items.length; i++) {
System.out.printf("%d. %s - $%.2f%n" , i + 1, items[i], prices[i]);
}
int choice = -1;
while (true) {
System.out.print("Enter the number from the list: ");
if (scanner.hasNextInt()) {
choice = scanner.nextInt();
scanner.nextLine();
if (choice >= 1 && choice <= items.length) {
break;
} else {
System.out.println("Please enter the number of the item from the list.");
}
} else {
System.out.println("Sorry, please enter a number on the list.");
scanner.nextLine();
}
}
String item = items[choice - 1];
double price = prices[choice - 1];
if (item.equalsIgnoreCase("Back to Menu")) return;
int quantity = -1;
while (true) {
System.out.print("Enter quantity: ");
if (scanner.hasNextInt()) {
quantity = scanner.nextInt();
scanner.nextLine(); // consume newline
if (quantity > 0) {
break;
} else {
System.out.println("Hmm. Please buy at least one item.");
}
} else {
System.out.println("Sorry, please enter a number.");
scanner.nextLine(); // clear invalid input
}
}
cart.add(new CartItem(item, quantity, price));
System.out.println(item + " added to the cart.");
}
public static void removeItem (Scanner scanner, List<CartItem> cart) {
if (cart.isEmpty()) {
System.out.println("Sorry, You currently have no item in the cart.");
return;
}
viewCart(cart);
System.out.print("Enter the number of the item you would like to remove:");
int index = scanner.nextInt();
scanner.nextLine();
if (index < 1 || index > cart.size()) {
System.out.println("Sorry that number is not on the list");
return;
}
CartItem removed = cart.remove(index -1 );
return;
}
public static void viewCart(List<CartItem> cart) {
if (cart.isEmpty()) {
System.out.println("There is no item currently in your cart :(");
return;
}
System.out.println("---------------------------");
System.out.println("==== Your Items ====");
System.out.println("---------------------------");
double totalPrice = 0.0;
int index = 1;
for (CartItem item : cart) {
System.out.println(index++ + ". " + item);
totalPrice += item.getTotalPrice();
}
System.out.println("\n=====================");
System.out.println("Total price: $" + totalPrice);
}
public static void printCart(List<CartItem> cart) {
if (cart.isEmpty()) {
System.out.println("There is no item currently in your cart :(");
Scanner scanner = new Scanner(System.in);
return;
}
System.out.println("---------------------------");
System.out.println("==== Purchase Overview ====");
System.out.println("---------------------------");
double totalPrice = 0.0;
int index = 1;
for (CartItem item : cart) {
System.out.println(index++ + ". " + item);
totalPrice += item.getTotalPrice();
}
System.out.println("\n=====================");
System.out.println("Total price: $" + totalPrice);
}
// Register Function
public static String register() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your Username:");
String username = scanner.nextLine();
while (!username.matches("[a-zA-Z0-9]+")) {
System.out.println("Username should only contain alphanumeric characters. Please try again.");
username = scanner.nextLine();
}
System.out.println("Enter your Password:");
String password = scanner.nextLine();
while (!password.matches("[a-zA-Z0-9]+")) {
System.out.println("Password should only contain alphanumeric characters. Please try again.");
password = scanner.nextLine();
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(RECORDS_FILE, true))) {
writer.write(username + "," + password + "\n");
System.out.println("You've successfully registered!");
return username;
} catch (IOException e) {
System.out.println("Something went wrong.");
return null;
}
}
// Login Function
public static String checkLoginUsernamePassword() {
try {
int attempts = 3;
Scanner input = new Scanner(System.in);
File file = new File(RECORDS_FILE);
while (attempts > 0) {
System.out.print("Enter username: ");
String enteredUsername = input.nextLine();
System.out.print("Enter password: ");
String enteredPassword = input.nextLine();
Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
String[] parts = line.split(",");
if (parts.length == 2) {
String username = parts[0];
String password = parts[1];
if (enteredUsername.equals(username) && enteredPassword.equals(password)) {
fileScanner.close();
System.out.println("You have successfully logged in!");
return username;
}
}
}
fileScanner.close();
attempts--;
System.out.println("Invalid username or password. " + attempts + " attempts remaining...");
}
System.out.println("Seems like we can't find your account. Let's create a new one");
} catch (FileNotFoundException e) {
System.out.println("Username not found.");
}
return null;
}
public static void displayReviews() {
File file = new File(REVIEWS_FILE);
if (!file.exists() || file.length() == 0) {
System.out.println("No reviews available.");
return;
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
boolean hasReviews = false;
while ((line = reader.readLine()) != null) {
hasReviews = true;
String[] parts = line.split(",", 3);
if (parts.length == 3) {
System.out.println("User: " + parts[0]);
System.out.println("Rating: " + parts[1]);
System.out.println("Comment: " + parts[2]);
System.out.println("------------------------------");
}
}
if (!hasReviews) {
System.out.println("No reviews available.");
}
} catch (IOException e) {
System.out.println("Error reading reviews.");
}
}
public static void leaveReview(String currentUser) {
Scanner scanner = new Scanner(System.in);
String ratingInput;
while (true) {
System.out.print("Enter your rating (1-5 stars): ");
ratingInput = scanner.nextLine().trim();
if (ratingInput.matches("[1-5]")) {
ratingInput = "*".repeat(Integer.parseInt(ratingInput));
break;
} else if (ratingInput.matches("[*]{1,5}")) {
break;
} else {
System.out.println("Invalid rating format. Please use 1 to 5 stars (*) or numbers (1-5).");
}
}
System.out.print("Enter your review: ");
String comment = scanner.nextLine();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(REVIEWS_FILE, true))) {
writer.write(currentUser + "," + ratingInput + "," + comment + "\n");
System.out.println("Thank you for your review," + currentUser);
} catch (IOException e) {
System.out.println("Error saving your review.");
}
}
}
class CartItem {
String name;
int quantity;
double price;
public CartItem(String name, int quantity, double price) {
this.name = name;
this.quantity = quantity;
this.price = price;
}
public double getTotalPrice() {
return price * quantity;
}
public String toString() {
return name + "x" + quantity + " - $" + getTotalPrice();
}
}