-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMenu.java
715 lines (616 loc) · 19.6 KB
/
Menu.java
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
package CZ2002_Assignment;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Menu class for all menu related methods
*/
public class Menu {
private ArrayList<MenuItem> itemList = new ArrayList<MenuItem> ();
private ArrayList<Promotion> menuPromoList = new ArrayList<Promotion> ();
/**
* Loads the base menu and concatenates the list of base menu items to current item list.
*/
public void LoadBaseMenu() {
BaseMenu bm = new BaseMenu();
itemList.addAll(bm.CreateBaseItemList());
menuPromoList.addAll(bm.CreateBasePromoList());
PrintMenu();
}
/**
* Serves as an interface to create items for both menu and promotion lists
*/
public void Create() {
Scanner sc = new Scanner(System.in);
System.out.println("(1) Create New Item");
System.out.println("(2) Create New Promotion");
InputChecker ic = new InputChecker();
int choice = 0;
boolean choiceBool = false;
for(int k = 0; k < ic.getNumOfTries(); k++) {
choice = ic.intChecker(choice, sc);
if(choice == -1 || !ic.inRange(choice, 0, 10000)) {
System.out.println("Invalid input for price, Number of tries: " + (k + 1));
sc.nextLine();
continue;
}
else {
choiceBool = true;
break;
}
}
if(!choiceBool) {
System.out.println("Exceeded number of tries, exiting");
return;
}
if(choice == 1) {
CreateNewMenuItem();
}
else {
CreateNewPromotion();
}
}
/**
* Key in number of menu items you want to add to the menu and adds them, values keyed must be case specific
*/
public void CreateNewMenuItem() {
Scanner sc = new Scanner(System.in);
System.out.println("How many items you wish to add?");
int numItems = sc.nextInt();
InputChecker ic = new InputChecker();
if(numItems <= 0) {
System.out.println("Invalid size declaration. Exiting.......");
return;
}
// Clears Buffer
sc.nextLine();
// Ask for input for each item to be added.
for(int i =0; i < numItems; i++) {
System.out.printf("Item %d\n",i + 1);
// Read Name
System.out.println("Name:");
String itemName = sc.nextLine();
// If a duplicate name was keyed to be added, ask for another name again
if(CheckList(itemName)) {
System.out.println("(1) Try Again");
System.out.println("(2) Exit");
int choice = sc.nextInt();
sc.nextLine();
if(choice == 1) {
i -= 1;
continue;
}
else {
System.out.println("Exiting, sucessful previous items added will remain in the list");
break;
}
}
// Read Description
System.out.println("Description:");
String desp = sc.nextLine();
// Read Price
System.out.println("Price:");
double price = 0;
boolean priceBool = false;
for(int k = 0; k < ic.getNumOfTries(); k++) {
price = ic.doubleChecker(price, sc);
if(price == -1 || !ic.inRange(price, 0, 10000)) {
System.out.println("Invalid input for price, Number of tries: " + (k + 1));
sc.nextLine();
continue;
}
else {
priceBool = true;
break;
}
}
if(!priceBool) {
System.out.println("Exceeded number of tries, exiting");
break;
}
// Clears buffer
sc.nextLine();
// Read category
System.out.println("Category:");
MenuCategory category = MenuCategory.valueOf(sc.nextLine().toUpperCase());
// Creates menuItem to be added
MenuItem m = new MenuItem(itemName,desp,price,category);
AddToItemList(m, true);
}
//sc.close();
}
/**
* Interface to create new instance of a promotion object in the menu
*/
public void CreateNewPromotion() {
Scanner sc = new Scanner(System.in);
System.out.println("How many Promotional Sets you wish to add?");
int numOfSets = sc.nextInt();
if(numOfSets <= 0) {
System.out.println("Invalid size declaration. Exiting.......");
return;
}
// Clears Buffer
sc.nextLine();
// Ask for input for each item to be added.
for(int i =0; i < numOfSets; i++) {
System.out.printf("New Promotion Set %d\n",i + 1);
System.out.println("Promotion Name:");
String promoName = sc.nextLine();
// If a duplicate name was keyed to be added, ask for another name again
if(checkPromoList(promoName)) {
System.out.println("(1) Try Again");
System.out.println("(2) Exit");
int choice = sc.nextInt();
sc.nextLine();
if(choice == 1) {
i-=1;
continue;
}
else {
System.out.println("Exiting, sucessful previous Promotions Set added will reamain in the list");
return;
}
}
System.out.println("Size of new Promotional set");
int size = sc.nextInt();
Promotion curPromoSet = new Promotion();
addItemsToPromoSet(curPromoSet, size);
//After finishing current promotional set add to promotion set menu
curPromoSet.setPromoName(promoName);
AddToMenuPromoList(curPromoSet, true);
sc = new Scanner(System.in);//reset buffer
}
}
/**
* Add items to a newly create promo set
* @param curPromoset current promo set for item to be added to
* @param size the number of items to be added to the set
*/
private void addItemsToPromoSet(Promotion curPromoSet,int size) {
Scanner sc = new Scanner(System.in);
InputChecker ic = new InputChecker();
for(int j = 0;j<size;j++) {
System.out.printf("Cur Promotion Set item no. %d\n",j + 1);
System.out.println("Add mene Item(s) to Promotion Set or unique Promotional Item");
System.out.println("(1) Add Selected Item(s) from menu to Promotion Set");
System.out.println("(2) Unique Promotional Item(s)");
int option = sc.nextInt();
if(option == 1) {
//display menu and add item to promotional Set
printItemsMenu();
System.out.println("Select menu item number to add to current Promotion Set");
int itemNo = sc.nextInt();
//0 base index recall
MenuItem itemTobeAdded = itemList.get(itemNo -1);
curPromoSet.addMenuItem(itemTobeAdded);
}
else if (option == 2) {
System.out.println("Enter unique item");
System.out.println("name:");
sc.nextLine();
String itemName = sc.nextLine();
// If item enter already in menu
if(CheckList(itemName)) {
System.out.println("Item already in menu not unique please use option 1 for this item!!!");
j = j - 1; //no count so can retry
continue;
}
//unique item only for promotion not in menu
// Read Description
System.out.println("Description:");
String desp = sc.nextLine();
// Read Price
System.out.println("Price:");
//double price = sc.nextDouble();
double price = 0;
boolean priceBool = false;
for(int k = 0; k < ic.getNumOfTries(); k++) {
price = ic.doubleChecker(price, sc);
if(price == -1 || !ic.inRange(price, 0, 10000)) {
System.out.println("Invalid input for price, Number of tries: " + (k + 1));
sc.nextLine();
continue;
}
else {
priceBool = true;
break;
}
}
if(!priceBool) {
System.out.println("Exceeded number of tries, exiting");
break;
}
// Clears buffer
sc.nextLine();
// Read category
System.out.println("Category:");
MenuCategory category = MenuCategory.valueOf(sc.nextLine().toUpperCase());
// Creates menuItem to be added
MenuItem itemTobeAdded = new MenuItem(itemName,desp,price,category);
curPromoSet.addMenuItem(itemTobeAdded);
}
}
//Set the current promotional set price
System.out.println("current promotion set price base on original item price: $%.2f" + curPromoSet.getOriginalPrice());
System.out.println("Set current promotion Set price: ");
double curPromoPrice = sc.nextDouble();
curPromoSet.setPromoPrice(curPromoPrice);
}
/**
* Checks through mennu item list to see if there are any duplicates of name == parameter in both Lists
* @param name reference name of item to search duplicate of
* @return returns wether a duplicate is found or not
*/
private boolean CheckList(String name) {
for(int i = 0; i < itemList.size();i++) {
String itemName = itemList.get(i).getItemName();
if(itemName.compareToIgnoreCase(name) == 0) {
System.out.println("Duplicate detected in List of Items. Please try again.");
return true;
}
}
return false;
}
/**
* Check promoList if have any duplicate promotional names
* @param name reference name of item to search duplicate of
* @return returns wether a duplicate is found or not
*/
private boolean checkPromoList(String name) {
for(Promotion pSet : menuPromoList) {
if(pSet.getPromoName().equals(name)) {
System.out.println("Duplicate Promotion-Set in List of Promotinal Items. Please try again.");
return true;
}
}
return false;
}
/**
* Adds menu item to its list data structure
* @param m menuitem to be added to the list of menu items
* @param print if a notification should be printed after adding
*/
public void AddToItemList(MenuItem m, boolean print) {
itemList.add(m);
if(print) {
int latestIndex = itemList.lastIndexOf(m);
System.out.println("");
System.out.println("=====================ADDED ITEM===========================");
PrintFromMenuListItem(latestIndex);
System.out.println("=========================================================");
System.out.println("");
PrintMenu();
}
}
/**
* Adds promotion to its list data structure
* @param p object to be added to the list
* @param print if a notification should be printed after adding
*/
public void AddToMenuPromoList(Promotion p, boolean print) {
menuPromoList.add(p);
if(print) {
int latestIndex = menuPromoList.lastIndexOf(p);
System.out.println("");
System.out.println("=====================ADDED PROMOTIONAL SET===========================");
PrintFromPromoListItem(latestIndex);
System.out.println("=========================================================");
System.out.println("");
}
}
/**
* Serves as an interface to update for both promotion and menu item
*/
public void Update() {
Scanner sc = new Scanner(System.in);
System.out.println("(1) Update a Menu Item");
System.out.println("(2) Update a Promotion");
int choice = sc.nextInt();
if(choice == 1) {
//PrintMenu();
//display all menu excludind promotion
printItemsMenu();
System.out.println("Select Item Number to update:");
int index = sc.nextInt();
UpdateMenuItem(index - 1);
}
else {
//display all promotion and their items
printPromotionMenu();
System.out.println("Select Promotion Number to update:");
int index = sc.nextInt();
UpdatePromotion(index -1);
}
}
/**
* Updates selected menu item of index
* @param index index of item to be updated
*/
public void UpdateMenuItem(int index) {
Scanner sc = new Scanner(System.in);
MenuItem m = itemList.get(index);
System.out.println("");
System.out.println("================ SELECTED ITEM ===================");
PrintFromMenuListItem(index);
System.out.println("==================================================");
System.out.println("(1)Change Name");
System.out.println("(2)Change Price");
System.out.println("(3)Change Description");
System.out.println("(4)Change Category");
int option = sc.nextInt();
switch(option) {
case 1:
sc.nextLine();
System.out.println("New Name");
String newName = sc.nextLine();
m.setItemName(newName, false);
break;
case 2:
System.out.println("New Price");
double newPrice = sc.nextDouble();
m.setItemPrice(newPrice, false);
break;
case 3:
sc.nextLine();
System.out.println("New Description");
String newDescript = sc.nextLine();
m.setItemDescription(newDescript, false);
break;
case 4:
sc.nextLine();
System.out.println("New Category");
MenuCategory newCate = MenuCategory.valueOf(sc.nextLine().toUpperCase());
m.setMenuCategory(newCate, false);
break;
}
System.out.println("");
System.out.println("======================= UPDATED =========================");
PrintFromMenuListItem(index);
System.out.println("=========================================================");
System.out.println("");
}
/**
* Updates selected promotion of index
* @param index index of promotion to be updated
*/
public void UpdatePromotion(int index) {
Scanner sc = new Scanner(System.in);
Promotion p = menuPromoList.get(index);
System.out.println("(1) Change Name");
System.out.println("(2) Change Price");
System.out.println("(3) Add Item");
System.out.println("(4) Remove Item");
int option = sc.nextInt();
switch(option) {
case 1:
sc.nextLine();
System.out.println("New Name");
String newName = sc.nextLine();
p.setPromoName(newName);
break;
case 2:
System.out.println("New Price");
double newPrice = sc.nextDouble();
p.setPromoPrice(newPrice);
break;
case 3:
System.out.println("Number of items to add");
int num = sc.nextInt();
addItemsToPromoSet(p, num);
break;
case 4:
p.displayPromotionItems();
System.out.println("Select which item to be remove from current promotion set");
int itemIndex = sc.nextInt();
p.getPromoList().remove(itemIndex -1);
//Set the current promotional set price
System.out.println("current promotion set price base on original item price: $" + p.getOriginalPrice());
System.out.println("Set current promotion Set price: ");
double curPromoPrice = sc.nextDouble();
p.setPromoPrice(curPromoPrice);
break;
}
System.out.println("");
System.out.println("=====================UPDATED PROMO========================");
PrintFromPromoListItem(index);
System.out.println();
System.out.println("=========================================================");
System.out.println("");
}
/**
* Interface for removing from any of the promotion and menu items list
*/
public void Remove() {
Scanner sc = new Scanner(System.in);
System.out.println("(1) Remove a Menu Item");
System.out.println("(2) Remove a Promotion");
int choice = sc.nextInt();
if(choice == 1) {
PrintMenu();
System.out.println("Item Number:");
int index = sc.nextInt();
RemoveFromItemList(index - 1);
PrintMenu();
}
else {
printPromotionMenu();
System.out.println("Promotion Number:");
int index = sc.nextInt();
RemoveFromPromoList(index - 1);
}
}
/**
* Removes selected item of index from menu item list
* @param index index of menu item to be removed
*/
public void RemoveFromItemList(int index) {
MenuItem m = itemList.get(index);
itemList.remove(index);
System.out.println("");
System.out.println("======================= REMOVED ITEM =========================");
PrintItem(m);
System.out.println("=========================================================");
System.out.println("");
}
/**
* Remove entire promotion Set from list
* @param index index of promotion to be removed
*/
public void RemoveFromPromoList(int index) {
Promotion p = menuPromoList.get(index);
menuPromoList.remove(index);
System.out.println("");
System.out.println("======================= REMOVED PROMOTION SET =========================");
PrintPromotion(p);
System.out.println("=========================================================");
System.out.println("");
}
/**
* Prints entire current itemList and promotion set
*/
public void PrintMenu() {
System.out.println("");
System.out.println("==================== MENU ========================");
System.out.println("");
System.out.println("==================== ITEMS ========================");
for(int i =0; i < itemList.size();i++) {
PrintFromMenuListItem(i);
}
System.out.println("");
System.out.println("==================== PROMOTIONS ========================");
printPromotionMenu();
System.out.println("");
System.out.println("=========================================================");
System.out.println("");
}
/**
* Prints only the selected index from item list
* @param index index of item from list to be printed
*/
public void PrintFromMenuListItem(int index) {
System.out.println("Item: " + (index+1));
PrintItem(itemList.get(index));
}
/**
* Prints only the item portion of the menu
*/
public void printItemsMenu() {
System.out.println("");
System.out.println("==================== MENU ========================");
System.out.println("");
System.out.println("==================== ITEMS ========================");
for(int i =0; i < itemList.size();i++) {
PrintFromMenuListItem(i);
}
System.out.println("");
}
/**
* Prints only the promotion portion of the menu
*/
public void printPromotionMenu() {
System.out.println("");
System.out.println("==================== PROMOTION MENU ========================");
System.out.println("");
//System.out.println("");
System.out.println("==================== PROMOTIONS SETS ========================");
// Prints all promotion items in MENU
for(int i =0; i < menuPromoList.size();i++) {
PrintFromPromoListItem(i);
}
System.out.println("");
System.out.println("=========================================================");
System.out.println("");
}
/**
* Prints selected index from promotion list
* @param index to be printed
*/
public void PrintFromPromoListItem(int index) {
System.out.println("");
System.out.println("Promotion: " + (index+1));
PrintPromotion(menuPromoList.get(index));
System.out.println("");
}
/**
* Prints Menu Item in stated format
* @param m object to be printed
*/
public void PrintItem(MenuItem m) {
System.out.println("");
System.out.println("Name: " + m.getItemName());
System.out.printf("Price: $%.2f\n" ,m.getItemPrice());
System.out.println("Description: " + m.getItemDescription());
System.out.println("Category: " + m.getMenuCategory());
System.out.println("");
}
/**
* Prints the specific promotion
* @param p object to be printed
*/
public void PrintPromotion(Promotion p) {
System.out.println("");
System.out.println("Promotion set: " +p.getPromoName());
System.out.println("Promotion set price: " + p.getPromoPrice());
System.out.println("Ala cart price");
p.displayPromotionItems();
System.out.println("");
}
/**
* Prints current size of itemList & promoList
*/
public void PrintSize() {
System.out.println("---------------------------------------------------");
System.out.println("ITEM SIZE = " + getMainMenuSize());
System.out.println("PROMOTION SIZE = " + getPromotionMenuSize());
System.out.println("---------------------------------------------------");
}
/**
* Gets the size of the menu list
* @return size of menu list
*/
public int getMainMenuSize() {
return itemList.size();
}
/**
* Gets the size of the promotion list
* @return size of promotion list
*/
public int getPromotionMenuSize() {
return menuPromoList.size();
}
/**
* Gets the list reference of the menu item list
* @return menu item list
*/
public ArrayList<MenuItem> getItemList() {
return itemList;
}
/**
* Get specific menu item from list
* @param index index reference
* @return menu item to be returned
*/
public MenuItem getMenuItem(int index) {
//get item from array,array 0 base index
//item display from 1 onwards but start from 0 base index
return itemList.get(index);
}
/**
* Gets the list reference of the promotion list
* @return promotion list
*/
public ArrayList<Promotion> getMenuPromoList() {
return menuPromoList;
}
/**
* Get specific promotion from list
* @param index index reference
* @return promotion to be returned
*/
public Promotion getPromotion(int index) {
//index 0 is first promotion
//but on display 1)promo 1 is index 0
//so typing 1 will give promotion 1 at index 0
return menuPromoList.get(index);
}
}