-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShopping_Application.java
738 lines (716 loc) · 28.2 KB
/
Shopping_Application.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
// Admin Name : admin,Password : 12345;
// Merchant ID : MID1,User Password : 12345;
// User Mail ID : [email protected],User Password : 12345;
import java.util.ArrayList;
import java.util.Scanner;
class Merchant {
String businessName;
int password;
String name;
String status;
String iD;
Merchant(String businessName,int password,String name,String status,String iD){
this.businessName = businessName;
this.password = password;
this.name=name;
this.status=status;
this.iD=iD;
}
}
class Approval{
String businessName;
int password;
String name;
String status;
Approval(String businessName,int password,String name,String status){
this.businessName = businessName;
this.password = password;
this.name=name;
this.status=status;
}
}
class User {
String mailID;
int password;
String name;
int balance;
String usId;
User(String mailID,int password,String name,int balance,String usId){
this.mailID=mailID;
this.password=password;
this.name=name;
this.balance=balance;
this.usId=usId;
}
}
class Orders {
String userId;
String productId;
String pName;
int noUnits;
int price;
Orders(String userId,String productId,int noUnits,int price,String pName){
this.userId=userId;
this.productId=productId;
this.noUnits=noUnits;
this.price=price;
this.pName=pName;
}
}
class Products {
int productNo;
String productName;
String productId;
int noOfUnits;
int price;
int disPrice;
String merID;
Products(int productNo,String productName,String productId,int noOfUnits,int price,int disPrice,String merID){
this.productNo=productNo;
this.productName = productName;
this.productId = productId;
this.noOfUnits = noOfUnits;
this.price = price;
this.disPrice = disPrice;
this.merID = merID;
}
}
public class Shopping {
Scanner sc = new Scanner(System.in);
static ArrayList<User> userList = new ArrayList<>();
static ArrayList<Merchant> merchantList = new ArrayList<>();
static ArrayList<Approval> approvalList = new ArrayList<>();
static ArrayList<Products> productList = new ArrayList<>();
static ArrayList<Orders> orderList = new ArrayList<>();
static int userBill = 0;
// To check Existing Business Name in merchantList,returns true if business name not found
static boolean checkExistingMe(String name){
for(int i=0;i<merchantList.size();i++){
if(merchantList.get(i).businessName.equals(name)){
return false;
}
}
return true;
}
// To check Existing Business Name in approvalList,returns true if business name not found
static boolean checkExistingInUser(String mailId){
for(int i=0;i<userList.size();i++){
if(userList.get(i).mailID.equals(mailId)){
return false;
}
}
return true;
}
// To check Existing Business Name in approvalList,returns true if business name not found
static boolean checkExistingInApproval(String name){
for(int i=0;i<approvalList.size();i++){
if(approvalList.get(i).businessName.equals(name)){
return false;
}
}
return true;
}
// To approve/reject merchant login request,approved details will be added to merchantList,rejected details will be deleted from approvalList
static void merchantApproval() {
for(int i=0;i<approvalList.size();i++){
if(approvalList.get(i).status.equals("No")){
System.out.println("Merchant Business Name : "+approvalList.get(i).businessName);
System.out.println("Merchant Name : "+approvalList.get(i).name);
System.out.println("1.Approve or 2.Reject");
int approval = sc.nextInt();
if(approval==1){
approvalList.get(i).status="Yes";
int meId = (merchantList.size()+1);
Merchant user = new Merchant(approvalList.get(i).businessName,approvalList.get(i).password,approvalList.get(i).name,"Yes","MID"+meId);
merchantList.add(user);
System.out.println("Approved Successfully");
}else if(approval==2){
approvalList.remove(i);
}
else System.out.println("Invalid Input");
}
}
System.out.println("No more Pending Approvals");
}
// To add merchant by admin,details will be added to merchantList
static void addMerchant() {
System.out.println("Enter Business Name : ");
sc.nextLine();String bName = sc.nextLine();
boolean flag = checkExistingMe(bName);
if (flag) {
System.out.println("Enter Business Password : ");
int mePassword = sc.nextInt();
String meId = "MID" + (merchantList.size() + 1);
System.out.println("Enter Your Name : ");
String meName = sc.next();
Merchant user = new Merchant(bName, mePassword,meName, "Yes", meId);
merchantList.add(user);
System.out.println("Merchant Added Successfully ");
} else {
System.out.println("Business Name already Exist");
}
}
// To remove merchant by admin,details will be removed from merchantList
static void removeMerchant() {
System.out.println("Enter Merchant Business Name to Remove : ");
sc.nextLine();String merName = sc.nextLine();
boolean flag = checkExistingMe(merName);
//System.out.println(fla);
if(flag==false){
for(int i=0;i<merchantList.size();i++) {
if (merchantList.get(i).businessName.equals(merName)) {
merchantList.remove(i);
System.out.println("Merchant Removed Successfully");
break;
}
}
}else{
System.out.println("Merchant Business Name Not Found!");
}
}
// List all products in productList
static void listAllProducts() {
System.out.println("S_No Product_Name Product_ID No_Of_Units Product_Price Discount_Price Merchant_ID");
for(int i=0;i<productList.size();i++){
if(productList.get(i).noOfUnits>0) {
System.out.println((i+1)+String.format("%-5s",".")+String.format("%-13s", productList.get(i).productName)
+String.format("%-11s", productList.get(i).productId) + String.format("%-12s", productList.get(i).noOfUnits)
+String.format("%-14s", productList.get(i).price) +String.format("%-15s",productList.get(i).disPrice)
+ String.format("%-10s", productList.get(i).merID));
}
}
}
// New merchant Sign in
static void becomeMerchant() {
System.out.println("Enter Business Name : ");
sc.nextLine();
String bName = sc.nextLine();
boolean flag = checkExistingMe(bName);
boolean flag1 = checkExistingInApproval(bName);
if(flag && flag1) {
System.out.println("Enter Business Password : ");
int mePassword = sc.nextInt();
System.out.println("Enter Your Name : ");
String meName = sc.next();
Approval merchant = new Approval(bName,mePassword,meName,"No");
approvalList.add(merchant);
System.out.println("Waiting for Approval Your");
} else {
System.out.println("Business Name already Exist !");
}
}
// To get merchant ID
static void getMerchantId(){
System.out.println("Enter Business Name to get ID : ");
sc.nextLine();
String bName = sc.nextLine();
int password;
boolean flag = checkExistingMe(bName);
boolean flag1 = checkExistingInApproval(bName);
int k=0;
if(flag==false) k=1;
else if (flag1==false) k=2;
switch(k) {
case 1:
System.out.println("Enter Business Password : ");
password = sc.nextInt();
for (int i = 0; i < merchantList.size(); i++) {
if (merchantList.get(i).businessName.equals(bName) && merchantList.get(i).password==password) {
System.out.println("Your Business ID is " + merchantList.get(i).iD);
}
}
break;
case 2:
System.out.println("Enter Business Password : ");
password = sc.nextInt();
for (int i = 0; i < approvalList.size(); i++) {
if (approvalList.get(i).businessName.equals(bName) && approvalList.get(i).password==password) {
System.out.println(" Your Business Profile Waiting for Approval ");
}
}
break;
default:
System.out.println("Business ID Not Found !");
}
}
// Merchant Login verification
static void merchantSigning() {
System.out.println("----- You Have Chosen Merchant Login ----- ");
System.out.println("Enter Merchant ID : ");
String merchantId = sc.next();
System.out.println("Enter User Password : ");
int merchantPassword = sc.nextInt();
for(int i=0;i<merchantList.size();i++){
if(merchantList.get(i).iD.equals(merchantId) && merchantList.get(i).password==merchantPassword){
System.out.println("----- Hi! "+merchantList.get(i).name+" -----");
merchantFunction(i);
}
}
}
// return no of product's the user is selling
static int noOfProduct(String k){
int n=0;
for(int i=0;i<productList.size();i++){
if(productList.get(i).merID.equals(k)){
n++;
}
}
return n;
}
// Merchant function
static void merchantFunction(int k){
int meOp = 0;
do {
System.out.println("----- Welcome Back -----");
System.out.println("1.Add Product");
System.out.println("2.Update Product");
System.out.println("3.Remove Product");
System.out.println("4.List My Product");
System.out.println("5.Exit");
System.out.print("Enter Your Choice : ");
meOp = sc.nextInt();
String s = merchantList.get(k).iD;
int no = noOfProduct(s);
switch (meOp) {
case 1:
addProducts(k,no);
break;
case 2:
updateProduct(k);
break;
case 3:
removeProducts(k);
break;
case 4:
listMyProduct(k);
break;
case 5:
break;
default:
System.out.println("Invalid Input");
break;
}
} while (meOp != 5);
}
// To add products by merchant,details will be added to productList
static void addProducts(int k,int n){
int pNo = n+101;
System.out.println("Enter Product Name : ");
sc.nextLine();String pName = sc.nextLine();
String pId = merchantList.get(k).iD+(n+1);
System.out.println("Enter Available Units : ");
int aUnit = sc.nextInt();
System.out.println("Enter Product Price : ");
int pPrice = sc.nextInt();
System.out.println("Enter Discount Price : ");
int dPrice = sc.nextInt();
Products addProduct = new Products(pNo,pName,pId,aUnit,pPrice,dPrice,merchantList.get(k).iD);
productList.add(addProduct);
}
// To update product details by merchant,details will be updated in productList
static void updateProduct(int k){
int n = noOfProduct(merchantList.get(k).iD);
if(n>0) {
System.out.println("Enter product ID : ");
sc.nextLine();
String pId = sc.nextLine();
int up = 0;
int g = 0;
for (int i = 0; i < productList.size(); i++) {
if (productList.get(i).productId.equals(pId)) {
g = i;
break;
}
}
do {
System.out.println("1.Update Product Name");
System.out.println("2.Update No Units");
System.out.println("3.Update Product Price");
System.out.println("4.Update Product Discount Price");
System.out.println("5.Exit");
up = sc.nextInt();
switch (up) {
case 1:
System.out.println("Enter Product Name : ");
sc.nextLine();
String pName = sc.nextLine();
productList.get(g).productName = pName;
break;
case 2:
System.out.println("Enter Available Units : ");
int aUnit = sc.nextInt();
productList.get(g).noOfUnits = aUnit;
break;
case 3:
System.out.println("Enter Product Price : ");
int pPrice = sc.nextInt();
productList.get(g).price = pPrice;
break;
case 4:
System.out.println("Enter Discount Price : ");
int dPrice = sc.nextInt();
productList.get(g).disPrice = dPrice;
break;
case 5:
break;
default:
System.out.println("Invalid Input");
}
} while (up != 5);
}else System.out.println("No Product Found !");
}
// To remove products by merchant,details will be removed from productList
static void removeProducts(int k){
System.out.println("Enter product ID : ");
sc.nextLine();String pId = sc.nextLine();
for(int i=0;i<productList.size();i++){
if(productList.get(i).productId.equals(pId) && productList.get(i).merID.equals(merchantList.get(k).iD)){
productList.remove(i);
break;
}
}
System.out.println("product Removed Successfully!");
}
// Will list all products sold by merchant from productList
static void listMyProduct(int k){
int n = noOfProduct(merchantList.get(k).iD);
if(n>0) {
String a = merchantList.get(k).iD;
System.out.println("Product_No Product_Name Product_ID No_Of_Units Product_Price Discount_Price Merchant_ID");
for(int i=0;i<productList.size();i++){
if(productList.get(i).merID.equals(a)){
System.out.println(String.format("%-12s",productList.get(i).productNo)+String.format("%-13s", productList.get(i).productName)
+String.format("%-11s", productList.get(i).productId) + String.format("%-12s", productList.get(i).noOfUnits)
+String.format("%-14s", productList.get(i).price) +String.format("%-15s", productList.get(i).disPrice)
+ String.format("%-10s", productList.get(i).merID));
}
}
}else System.out.println("No Product Found!");
}
// Return product index No in productList
static int productId(String productId){
int i;
for(i=0;i<productList.size();i++){
if(productId.equals(productList.get(i).productId)){
return i;
}
}
return i;
}
// List All products and total cost in users cart from orderList
static void viewProduct(int k){
int total = 0;
for(int i=0;i<orderList.size();i++){
if(userList.get(k).usId.equals(orderList.get(i).userId)){
System.out.println("Product ID : "+orderList.get(i).productId+"; Product Name : "+orderList.get(i).pName+"; No of Units : "+orderList.get(i).noUnits);
total+=orderList.get(i).price*orderList.get(i).noUnits;
}
}
System.out.println("Total Amount : Rs."+total+".00");
}
// Remove products in users cart from orderList
static void removeProduct(int k){
System.out.println("Enter Remove product ID or Enter 0 : ");
sc.nextLine();
String a = sc.nextLine();
for(int i=0;i<orderList.size();i++){
if(userList.get(k).usId.equals(orderList.get(i).userId) && orderList.get(i).productId.equals(a)){
orderList.remove(i);
break;
}
}
}
// View cart and actions
static void viewCart(int k) {
int ch=0;
do{
System.out.println("1.Remove Products from cart");
System.out.println("2.Check Out");
System.out.println("3.Continue Shopping");
ch=sc.nextInt();
switch (ch) {
case 1:
if(userBill>0) {
for (int i = 0; i < orderList.size(); i++) {
if (userList.get(k).usId.equals(orderList.get(i).userId)) {
System.out.println("Product ID : " + orderList.get(i).productId + "; Product Name : " + orderList.get(i).pName + "; No of Units : " + orderList.get(i).noUnits);
}
}
removeProduct(k);
}else System.out.println("No Product Found on Your Cart!\n Add Product To Your Cart");
break;
case 2:
if(userBill>0) {
checkOut(k);
}else System.out.println("No Product Found on Your Cart!\n Add Product To Your Cart");
break;
case 3:
break;
default:
System.out.println("Invalid Input");
}
}while (ch!=3);
}
// Return product price
static int productPrice(String productId){
int price=0;
for(int i=0;i<productList.size();i++){
if(productId.equals(productList.get(i).productId)){
return productList.get(i).price;
}
}
return price;
}
// Return Product Name
static String productName(String productId){
String price="";
for(int i=0;i<productList.size();i++){
if(productId.equals(productList.get(i).productId)){
return productList.get(i).productName;
}
}
return price;
}
// To add products to cart
static void addToCart(int k){
int no=0;
System.out.println("Enter no of Items : ");
no = sc.nextInt();
for(int i=0;i<no;i++){
System.out.println("Enter no of Product ID : ");
sc.nextLine();String productId=sc.nextLine();
System.out.println("Enter no of Units : ");
int noUnits = sc.nextInt();
int proPrice = productPrice(productId);
String pName = productName(productId);
Orders addToCart = new Orders(userList.get(k).usId,productId,noUnits,proPrice,pName);
orderList.add(addToCart);
}
userBill = totalPrice(k);
}
// To Check Out
static void checkOut(int k){
int total = 0;
for(int i=0;i<orderList.size();i++) {
if (userList.get(k).usId.equals(orderList.get(i).userId)) {
int a = productId(orderList.get(i).productId);
total+=orderList.get(i).price*orderList.get(i).noUnits;
productList.get(a).noOfUnits-=orderList.get(i).noUnits;
orderList.remove(i);
userBill=total;
}
}
userList.get(k).balance-=total;
userBill=0;
System.out.println("Thanks for Shopping!");
}
// Return User Bill Amount
static int totalPrice(int k){
int total = 0;
for(int i=0;i<orderList.size();i++){
if(userList.get(k).usId.equals(orderList.get(i).userId)){
total+=orderList.get(i).price*orderList.get(i).noUnits;
}
}
return total;
}
// User actions
static void UserFunction(int k){
int usOp = 0;
do {
System.out.println("----- Welcome "+userList.get(k).name+" -----");
System.out.println("1.View Product");
System.out.println("2.View Cart");
System.out.println("3.Check Balance");
System.out.println("4.Exit");
System.out.print("Enter Your Choice : ");
usOp = sc.nextInt();
switch (usOp) {
case 1:
listAllProducts();
addToCart(k);
break;
case 2:
if(userBill>0) {
viewProduct(k);
viewCart(k);
}else System.out.println("No Product Found on Your Cart!\n Add Product To Your Cart");
break;
case 3:
System.out.println("Your Balance : "+userList.get(k).balance);
break;
default:
System.out.println("Invalid Input!");
break;
}
} while (usOp != 4);
}
//User Sign in
static void userSignup() {
System.out.println("Enter User Mail ID : ");
sc.nextLine();String usMail = sc.nextLine();
boolean flag = checkExistingInUser(usMail);
if(flag) {
System.out.println("Enter User Password : ");
int usPassword = sc.nextInt();
System.out.println("Enter User Name : ");
sc.nextLine();
String usName = sc.nextLine();
System.out.println("Enter Deposit Amount : ");
int usBalance = sc.nextInt();
User user = new User(usMail, usPassword, usName, usBalance, "UID" + (userList.size() + 1));
userList.add(user);
System.out.println("Account Created Successfully!");
}else System.out.println("User Mail ID Already Exist!");
}
//User Login
static void userSigning() {
System.out.println("----- You Have Chosen User Login ----- ");
System.out.println("Enter User Mail ID : ");
String usMail = sc.next();
System.out.println("Enter User Password : ");
int usPassword = sc.nextInt();
int pro=0;
for(int i=0;i<userList.size();i++){
if(userList.get(i).mailID.equals(usMail) && userList.get(i).password==usPassword){
UserFunction(i);
pro++;
break;
}
}
if(pro==0) System.out.println("User ID and Password Mismatch! \nRetry!");
}
//Admin actions
static void admin() {
System.out.println("----- You Have Chosen Admin Login -----");
System.out.println("Enter Admin Name : ");
String adminName = sc.next();
System.out.println("Enter Password");
int adminPassword = sc.nextInt();
int adop = 0;
if (adminName.equals("admin") && adminPassword == 12345) {
do {
System.out.println("-- Welcome Admin --");
System.out.println("1.Merchant Approval List");
System.out.println("2.Add Merchant");
System.out.println("3.Remove Merchant");
System.out.println("4.List All Products");
System.out.println("5.Exit");
System.out.print("Enter Your Choice : ");
adop = sc.nextInt();
switch (adop) {
case 1:
merchantApproval();
break;
case 2:
addMerchant();
break;
case 3:
removeMerchant();
break;
case 4:
listAllProducts();
break;
case 5:
break;
default:
System.out.println("Invalid Input");
break;
}
} while (adop != 5);
} else {
System.out.println("check Admin Name and Password");
}
}
//Merchant actions
static void merchant(){
int meOp=0;
do{
System.out.println("----- You Have Chosen Merchant Login -----");
System.out.println("1. Become a Merchant");
System.out.println("2. Get Merchant ID");
System.out.println("3. Merchant Signing");
System.out.println("4. Exit");
System.out.println("Enter Choice : ");
meOp = sc.nextInt();
switch (meOp){
case 1:
becomeMerchant();
break;
case 2:
getMerchantId();
break;
case 3:
merchantSigning();
break;
case 4:
break;
default:
System.out.println("Invalid Input!");
}
}while(meOp!=4);
}
//User actions
static void user() {
int usOp=0;
do{
System.out.println("----- You Have Chosen User Login -----");
System.out.println("1.User Signup");
System.out.println("2.User Signing");
System.out.println("3.Exit");
System.out.println("Enter Choice : ");
usOp = sc.nextInt();
switch (usOp){
case 1:
userSignup();
break;
case 2:
userSigning();
break;
case 3:
break;
default:
System.out.println("Invalid Input!");
}
}while(usOp!=3);
}
public static void main(String[] args) {
Merchant u1 = new Merchant("business 1",12345,"Charan","Yes","MID1");
Merchant u2 = new Merchant("business 2",12345,"Arun","Yes","MID2");
merchantList.add(u1);
merchantList.add(u2);
Products p1 = new Products(101,"Mobile01","MID11",100,10000,100,"MID1");
productList.add(p1);
Products p2 = new Products(102,"TV01","MID12",250,10000,100,"MID1");
productList.add(p2);
User us1 = new User("[email protected]",12345,"Ram",100000,"UID1");
userList.add(us1);
int ch = 0;
do{
System.out.println("----- Welcome To Shopping -----");
System.out.println("1.Admin Login");
System.out.println("2.Merchant Login");
System.out.println("3.User Login");
System.out.println("4.Exit");
System.out.print("Enter Choice : ");
ch = sc.nextInt();
switch (ch) {
case 1:
admin();
break;
case 2:
merchant();
break;
case 3:
user();
break;
case 4:
break;
default:
System.out.println("Invalid Input Retry!");
break;
}
}while( ch!=4);
System.out.println("Thanks for Using!");
}
}