-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLemonade.java
More file actions
760 lines (725 loc) · 22.1 KB
/
Copy pathLemonade.java
File metadata and controls
760 lines (725 loc) · 22.1 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
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
import java.util.*;
class Lemonade{
public static void exit(){
clearConsole();
System.out.println("+--------------------------------------------------------------+");
System.out.println("| QUICK LEMONADE EXITED |");
System.out.println("+--------------------------------------------------------------+");
System.exit(0);
}
public static void updateOrderDetails(){
L1:do
{
clearConsole();
System.out.println("+--------------------------------------------------------------+");
System.out.println("| Update Order Detais |");
System.out.println("+--------------------------------------------------------------+");
System.out.println();
System.out.print("Enter Order Id - ");
String id = input.next();
int index = indexOfOrderId(id);
if (index == -1)
{
System.out.println("Invalid Order Id");
L2:do
{
System.out.print("Do you want to update another order details/ (Y/N) - ");
char op = input.next().charAt(0);
if (op=='y'||op=='Y')
{
continue L1;
}
if (op=='n'||op=='N')
{
return;
}
System.out.println("wrong option");
} while (true);
}
int status = orderStatus[index];
if (status==1||status==2)
{
System.out.println("The Order is "+orderStatusString[status]+" Can not Update This Order" );
L2:do
{
System.out.print("Do you want to update another order details/ (Y/N) - ");
char op = input.next().charAt(0);
if (op=='y'||op=='Y')
{
continue L1;
}
if (op=='n'||op=='N')
{
return;
}
System.out.println("wrong option");
} while (true);
}
System.out.println("OrderId - "+orderId[index]);
System.out.println("CustomerId - "+customerId[index]);
System.out.println("Name - "+customerName[index]);
System.out.println("Quantity - "+orderQuantity[index]);
System.out.println("OrderValue - "+orderValue[index]);
System.out.println("OrderStatus- "+orderStatusString[status]);
System.out.println();
System.out.println("What do you want to update ?");
System.out.println("\t(1)Quantity");
System.out.println("\t(2)Status");
System.out.println();
L2:do
{
System.out.print("Enter Your Option - ");
int op = input.nextInt();
if (op==1)
{
clearConsole();
System.out.println("Quantity Update");
System.out.println("===============");
System.out.println();
System.out.println("OrderId - "+orderId[index]);
System.out.println("CustomerId - "+customerId[index]);
System.out.println("Name - "+customerName[index]);
System.out.println();
System.out.print("Enter Your Quantity Update Value - ");
orderQuantity[index] = input.nextInt();
orderValue[index] = orderQuantity[index]*lemonadePrice;
System.out.println();
System.out.println("\t update order quantity success fully....");
System.out.println();
System.out.println("new order quantity - "+orderQuantity[index]);
System.out.println("new order value - "+orderValue[index]);
System.out.println();
}
if (op==2)
{
clearConsole();
System.out.println("Status Update");
System.out.println("=============");
System.out.println();
System.out.println("OrderId - "+orderId[index]);
System.out.println("CustomerId - "+customerId[index]);
System.out.println("Name - "+customerName[index]);
System.out.println();
System.out.println("(1)Cancel");
System.out.println("(2)Preparing");
System.out.println("(3)Delivered");
System.out.println();
L3:do
{
System.out.print("Enter new order status - ");
int status2 = input.nextInt();
if (status2==1)
{
orderStatus[index] = 2;
break L3;
}
if (status==2)
{
orderStatus[index] = 0;
break L3;
}
if (status==3)
{
orderStatus[index] = 1;
break L3;
}
System.out.println("wrong option");
} while (true);
System.out.println();
System.out.println("\t Updated Order Status Success Fully....");
System.out.println();
System.out.println("New Order Status - "+orderStatusString[orderStatus[index]]);
System.out.println();
}
if (op!=1&&op!=2)
{
System.out.println("Wrong Option");
continue L2;
}
L3:do
{
System.out.print("Do you want to update another order details? (Y/N) - ");
char op2 = input.next().charAt(0);
if (op2=='y'||op2=='Y')
{
continue L1;
}
if (op2=='n'||op2=='N')
{
return;
}
System.out.println("wrong option");
} while (true);
} while (true);
} while (true);
}
public static void viewOrders(){
L1:do
{
clearConsole();
System.out.println("+--------------------------------------------------------------+");
System.out.println("| View Order List |");
System.out.println("+--------------------------------------------------------------+");
System.out.println();
System.out.println("[1] Preparing Order");
System.out.println("[2] Delivered Order");
System.out.println("[3] Cancel Order");
System.out.println();
int op;
L2:do
{
System.out.print("Enter An Option To Continue - ");
op = input.nextInt();
if (op==1)
{
System.out.println("+--------------------------------------------------------------+");
System.out.println("| Preparing Orders |");
System.out.println("+--------------------------------------------------------------+");
op = 0;
break L2;
}
if (op==2)
{
System.out.println("+--------------------------------------------------------------+");
System.out.println("| Dilivered Orders |");
System.out.println("+--------------------------------------------------------------+");
op = 1;
break L2;
}
if (op==3)
{
System.out.println("+--------------------------------------------------------------+");
System.out.println("| Canceled Orders |");
System.out.println("+--------------------------------------------------------------+");
op = 2;
break L2;
}
System.out.println("Wrong option");
} while (true);
System.out.println("---------------------------------------------------------");
System.out.println("|OrderID CustomerID name Quantitiy OrderValue|");
System.out.println("---------------------------------------------------------");
boolean isDetailsPrinted = false;
for (int i = 0; i < orderStatus.length; i++)
{
if (orderStatus[i]==op)
{
System.out.printf("|%-10s%-13s%-10s%-12d%-10.2f|\n",orderId[i],customerId[i],customerName[i],orderQuantity[i],orderValue[i]);
isDetailsPrinted = true;
}
}
if (!isDetailsPrinted)
{
System.out.println("|null null null null null |");
}
System.out.println("---------------------------------------------------------");
L2:do
{
System.out.println("Search Another Order List? (y/n) - ");
char op2 = input.next().charAt(0);
if (op2=='y'||op2=='Y')
{
continue L1;
}
if (op2=='n'||op2=='N')
{
return;
}
System.out.println("Wrong Option");
} while (true);
} while (true);
}
public static void searchCustomer(){
L1:do
{
clearConsole();
System.out.println("+--------------------------------------------------------------+");
System.out.println("| Search Customer |");
System.out.println("+--------------------------------------------------------------+");
System.out.println();
System.out.print("Enter Customer Id - ");
String cusId = input.next();
System.out.println();
if (!isValidCustomerId(cusId))
{
System.out.println("Invalid Customer Id");
L2:do
{
System.out.print("Search Another Customer Id? (y/n) - ");
char op = input.next().charAt(0);
if (op=='y'||op=='Y')
{
continue L1;
}
if (op=='n'||op=='N')
{
return;
}
System.out.println("Wrong Option");
} while (true);
}
int index = indexOfCustomerId(cusId);
if (index == -1)
{
L2:do
{
System.out.println("This Customer ID Is Not Added Yet......");
System.out.print("Search Another Customer Id (y/n) - ");
char op = input.next().charAt(0);
if (op=='y'||op=='Y')
{
continue L1;
}
if (op=='n'||op=='N')
{
return;
}
System.out.println("Wrong Option");
} while (true);
}
clearConsole();
System.out.println("CustomerID - "+customerId[index]);
System.out.println("Name - "+customerName[index]);
System.out.println();
System.out.println("Customer Order Details");
System.out.println("========================");
System.out.println();
System.out.println("-----------------------------------------------");
System.out.println("Order_ID Order_Quantity Total_value");
for (int i = 0; i < customerId.length; i++)
{
if (customerId[i].equals(cusId))
{
System.out.println("-----------------------------------------------");
System.out.printf("%-13s%-19d%.2f\n",orderId[i],orderQuantity[i],orderValue[i]);
}
}
System.out.println("-----------------------------------------------");
System.out.println();
System.out.println();
L2:do
{
System.out.print("Search Another Customer Id (y/n) - ");
char op = input.next().charAt(0);
if (op=='y'||op=='Y')
{
continue L1;
}else if (op=='n'||op=='N')
{
return;
}else
{
System.out.println("Wrong Option");
}
} while (true);
} while (true);
}
public static void searchOrder(){
L1:do
{
clearConsole();
System.out.println("+--------------------------------------------------------------+");
System.out.println("| Search Order |");
System.out.println("+--------------------------------------------------------------+");
System.out.println();
System.out.print("Enter Order Id - ");
String id = input.next();
int index = indexOfOrderId(id);
if (index==-1)
{
System.out.println("Invalid Order Id");
L2:do
{
System.out.print("Do You Want To Enter Again; (y/n) - ");
char op = input.next().charAt(0);
if (op=='y'||op=='Y')
{
continue L1;
}
if (op=='n'||op=='N')
{
return;
}
System.out.println("Wrong Option");
} while (true);
}
System.out.println();
System.out.println("------------------------------------------------------------------------");
System.out.println("|OrderID CustomerID name Quantitiy OrderValue OrderStatus |");
System.out.println("------------------------------------------------------------------------");
System.out.printf("|%-10s%-13s%-10s%-12d%-13.2f%-12s|\n",orderId[index],customerId[index],customerName[index],orderQuantity[index],orderValue[index],orderStatusString[orderStatus[index]]);
System.out.println("------------------------------------------------------------------------");
System.out.println();
L2:do
{
System.out.print("Search Another Order? (y/n) - ");
char op = input.next().charAt(0);
if (op=='y'||op=='Y')
{
continue L1;
}
if (op=='n'||op=='N')
{
return;
}
System.out.println("Wrong option");
} while (true);
} while (true);
}
public static void searchBestCustomer(){
L1:do
{
clearConsole();
System.out.println("+--------------------------------------------------------------+");
System.out.println("| Best Customer |");
System.out.println("+--------------------------------------------------------------+");
System.out.println();
//copy part (in this part copying all the data to new arrays without duplicating)
String[] sortCustomerId = new String[0];
String[] sortCustomerName = new String[0];
double[] customerTotalValue = new double[0];
for (int i = 0; i < customerId.length; i++)
{
boolean isExist = false;
for (int j = 0; j < sortCustomerId.length ; j++)
{
if (customerId[i].equals(sortCustomerId[j]))
{
if (orderStatus[i]!=2)
{
customerTotalValue[j] += orderValue[i];
}
isExist = true;
}
}
if(!isExist){
String[] tempSortCustomerId = new String[sortCustomerId.length+1];
String[] tempSortCustomerName = new String[sortCustomerName.length+1];
double[] tempCustomerTotalValue = new double[customerTotalValue.length+1];
for (int j = 0; j < sortCustomerId.length; j++)
{
tempSortCustomerId[j] = sortCustomerId[j];
tempSortCustomerName[j] = sortCustomerName[j];
tempCustomerTotalValue[j] = customerTotalValue[j];
}
sortCustomerId = tempSortCustomerId;
sortCustomerName = tempSortCustomerName;
customerTotalValue = tempCustomerTotalValue;
sortCustomerId[sortCustomerId.length-1] = customerId[i];
sortCustomerName[sortCustomerName.length-1] = customerName[i];
customerTotalValue[customerTotalValue.length-1] = orderValue[i];
}
}
//sort part
for (int i = 0; i < sortCustomerId.length; i++)
{
for (int j = 0; j < sortCustomerId.length-(1+i); j++)
{
if (customerTotalValue[j] < customerTotalValue[j+1])
{
String temp1 = sortCustomerId[j];
sortCustomerId[j] = sortCustomerId[j+1];
sortCustomerId[j+1] = temp1;
String temp2 = sortCustomerName[j];
sortCustomerName[j] = sortCustomerName[j+1];
sortCustomerName[j+1] = temp2;
double temp3 = customerTotalValue[j];
customerTotalValue[j] = customerTotalValue[j+1];
customerTotalValue[j+1] = temp3;
}
}
}
//print part
System.out.println("\n----------------------------------------");
String line1 = String.format("%-14s%-15s%8s", " CustomerID", "Name", "Total");
System.out.println(line1);
System.out.println("----------------------------------------");
for (int i = 0; i < sortCustomerId.length; i++)
{
String line = String.format("%1s%-14s%-15s%8.2f", " ", sortCustomerId[i], sortCustomerName[i], customerTotalValue[i]);
System.out.println(line);
System.out.println("----------------------------------------");
}
//////////////
L2:do
{
System.out.print("Go To The Home Page? (y/n) - ");
char op = input.next().charAt(0);
if (op=='y'||op=='Y')
{
return;
}
if (op=='n'||op=='N')
{
continue L1;
}
System.out.println("Wrong option");
} while (true);
} while (true);
}
public static void placeOrder(){
L1:do
{
clearConsole();
System.out.println("+--------------------------------------------------------------+");
System.out.println("| Place Order |");
System.out.println("+--------------------------------------------------------------+");
System.out.println();
String newOrderId = generateOrderId();
System.out.println("Order Id - "+newOrderId);
System.out.println();
//getting customer Id
System.out.print("Enter Customer Id (Phone Number) - ");
String newCustomerId = input.next();
if (!isValidCustomerId(newCustomerId))
{
System.out.println("Invalid Custoemr Id");
L2:do
{
System.out.print("Do You Want To Try Another Id (Y/N) - ");
char option = input.next().charAt(0);
if (option=='y'||option=='N')
{
continue L1;
}
if (option=='n'||option=='N')
{
return;
}
System.out.println("Wrong Option");
} while (true);
}
//getting customer name
String newCustomerName;
int index = indexOfCustomerId(newCustomerId);
if (index != -1)
{
newCustomerName = customerName[index];
System.out.println("Customer Name - "+newCustomerName);
}else{
System.out.print("Enter Customer Name - ");
newCustomerName = input.next();
}
//getting lemonade quantity
int quantity;
L2:do
{
System.out.print("Enter Lemonade Quantity - ");
quantity = input.nextInt();
if (quantity < 1)
{
System.out.println("Minimu order is 1 Lemonade");
}else{
break L2;
}
} while (true);
//getting total price
double totalPrice = quantity*lemonadePrice;
System.out.println("Total Price - "+totalPrice);
//getting confirming order
L2:do
{
System.out.print("Confirm The Order? (y/n) - ");
char op = input.next().charAt(0);
if (op=='y'||op=='Y')
{
break L2;
}
if (op=='n'||op=='N')
{
L3:do
{
System.out.println("Place Another Order? (y/n) - ");
char op2 = input.next().charAt(0);
if (op=='y'||op=='Y')
{
continue L1;
}
if (op=='n'||op=='N')
{
return;
}
System.out.println("Wrong Option");
} while (true);
}
System.out.println("Wrong Option");
} while (true);
extendArrays();
orderId[orderId.length-1] = newOrderId;
customerId[customerId.length-1] = newCustomerId;
customerName[customerName.length-1] = newCustomerName;
orderQuantity[orderQuantity.length-1] = quantity;
orderValue[orderValue.length-1] = totalPrice;
orderStatus[orderStatus.length-1] = 0;
System.out.println("Your Order Is Preparing");
//input.nextLine();
L2:do
{
System.out.print("Place Another Order (y/n) - ");
char op = input.next().charAt(0);
if (op=='y'||op=='Y')
{
break L2;
}else if (op=='n'||op=='N')
{
return;
}else
{
System.out.println("Wrong Option");
}
} while (true);
} while (true);
}
public static void homePage(){
L1:do
{
clearConsole();
System.out.println("+--------------------------------------------------------------+");
System.out.println("| QUICK LEMONADE |");
System.out.println("+--------------------------------------------------------------+");
System.out.println();
System.out.println("[1] Place Order [2] Search Best Customer");
System.out.println("[3] Search Order [4] Search Customer");
System.out.println("[5] View Orders [6] Update Order Details");
System.out.println("[7] Exit");
System.out.println();
L2:do
{
System.out.print("Enter an option to continue :- ");
int op = input.nextInt();
if (op==1)
{
placeOrder();
break L2;
}
if (op==2)
{
searchBestCustomer();
break L2;
}
if (op==3)
{
searchOrder();
break L2;
}
if (op==4)
{
searchCustomer();
break L2;
}
if (op==5)
{
viewOrders();
break L2;
}
if (op==6)
{
updateOrderDetails();
break L2;
}
if (op==7)
{
exit();
break L2;
}
System.out.println("Wrong option try again");
continue L2;
} while (true);
} while (true);
}
public static void main(String args[]){
homePage();
}
public static Scanner input = new Scanner(System.in);
public static String[] orderId = { "L0001", "L0002", "L0003", "L0004", "L0005", "L0006", "L0007", "L0008", "L0009", "L0010", "L0011", "L0012", "L0013", "L0014", "L0015" };
public static String[] customerId = { "0770000001", "0770000002", "0770000003", "0770000004", "0770000005", "0770000001", "0770000002", "0770000003", "0770000004", "0770000005", "0770000001", "0770000002", "0770000003", "0770000004", "0770000005" };
public static String[] customerName = { "Name01", "Name02", "Name03", "Name04", "Name05", "Name01", "Name02", "Name03", "Name04", "Name05", "Name01", "Name02", "Name03", "Name04", "Name05", };
public static int[] orderQuantity = { 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5 };
public static double[] orderValue = { 100, 200, 300, 400, 500, 500, 400, 300, 200, 100, 100, 200, 300, 400, 500 };
public static int[] orderStatus = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
public static String[] orderStatusString = { "Preparing", "Delivered", "Canceled" };
public static double lemonadePrice = 100;
public static void clearConsole(){
try {
final String os = System.getProperty("os.name");
if (os.contains("Windows")) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} else {
System.out.print("\033[H\033[2J");
System.out.flush();
}
} catch (final Exception e) {
e.printStackTrace();
// Handle any exceptions.
}
}
public static String generateOrderId(){
if (orderId.length==00)
{
return "L0001";
}
return String.format("L%04d",Integer.parseInt(orderId[orderId.length-1].substring(1))+1);
}
public static boolean isValidCustomerId(String id){
if (id.length()!=10)
{
return false;
}
if (id.charAt(0)!='0')
{
return false;
}
for (int i = 0; i < id.length(); i++)
{
if (id.charAt(i)<48||id.charAt(i)>57)
{
return false;
}
}
return true;
}
public static int indexOfCustomerId(String id){
for (int i = 0; i < customerId.length; i++)
{
if (customerId[i].equals(id))
{
return i;
}
}
return -1;
}
public static void extendArrays(){
int length = orderId.length;
String [] tempOrderId = new String [length+1];
String [] tempCustomerId = new String [length+1];
String [] tempCustomerName = new String [length+1];
int [] tempOrderQuantity = new int [length+1];
double [] tempOrderValue = new double [length+1];
int [] tempOrderStatus = new int [length+1];
for (int i = 0; i < length; i++)
{
tempOrderId[i] = orderId [i];
tempCustomerId[i] = customerId [i];
tempCustomerName[i] = customerName [i];
tempOrderQuantity[i] = orderQuantity [i];
tempOrderValue[i] = orderValue [i];
tempOrderStatus[i] = orderStatus [i];
}
orderId = tempOrderId;
customerId = tempCustomerId;
customerName = tempCustomerName;
orderQuantity = tempOrderQuantity;
orderValue = tempOrderValue;
orderStatus = tempOrderStatus;
}
public static int indexOfOrderId(String id){
for (int i = 0; i < orderId.length; i++)
{
if (orderId[i].equals(id))
{
return i;
}
}
return -1;
}
}