-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupermarket.cpp
825 lines (681 loc) · 14.7 KB
/
supermarket.cpp
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
struct Item
{
char name[40];
long barcode;
int price;
int stock;
void getdata();
void showdata();
int checkN(char nm[]);
int checkB(long bc);
int checkS(int st);
void Edit();
};
// ******************Input a new item*****************
void Item::getdata()
{
cout<<setw(25)<<"\n\n Enter Name : ";
gets(name);
cout<<setw(25)<<"\n Enter barcode number : ";
cin>>barcode;
cout<<setw(25)<<"\n Enter price : ";
cin>>price;
cout<<setw(25)<<"\n Enter quantity in stock : ";
cin>>stock;
}
// ****************** Display an item****************
void Item::showdata()
{
cout<<setw(25)<<"\n\n Name : "<<name;
cout<<setw(25)<<"\n Barcode Number : " <<barcode;
cout<<setw(25)<<"\n Price : "<<price;
cout<<setw(25)<<" \n Quantity in stock : "<<stock;
}
// ******************Check by Name********************
int Item::checkN(char *nm)
{
if(strcmp(nm,name)==0)
return 1;
else
return 0;
}
// ******************Check by barcode*****************
int Item::checkB(long bc)
{
if(bc==barcode)
return 1;
else
return 0;
}
// ******************Check by stock*****************
int Item::checkS(int st)
{
if(stock<st)
return 0;
else if(st==stock)
return 1;
else
return 2;
}
// ******************Edit A record******************
void Item::Edit()
{
char ch;
do{
cout<<"Details of the record : \n";
showdata();
cout<<"\n Press A if you wish to update the name \n";
cout<<"\n Press B if you wish to update the barcode \n";
cout<<"\n Press C if you wish to update the price \n";
cout<<"\n Press D if you wish to update the stock \n";
cout<<"\n Press E to make no futher changes\n";
ch=getche();
switch(ch)
{
case 'A':
case 'a': cout<<setw(30)<<"\n Enter new name : ";
gets(name);
break;
case 'B':
case 'b': cout<<setw(30)<<"\n Enter new Barcode Number : ";
cin>>barcode;
break;
case 'C':
case 'c': cout<<setw(30)<<"\n Enter new price : ";
cin>>price;
break;
case 'D':
case 'd': cout<<setw(30)<<"\n Enter new stock left : ";
cin>>stock;
break;
case 'E':
case 'e': break;
default : cout<<"\n Invalid Input !! \n Please enter a relevant choice. \n";
}
}while(ch!='E' && ch!='e');
}
// ******************Append(ADD) a new item***********
void Append()
{
Item I;
cout<<"\n Enter the Details of the item : \n";
I.getdata();
fstream f1;
f1.open("INVENTORY.DAT",ios::binary|ios::app);
f1.write((char*) &I,sizeof(I));
f1.close();
}
// ******************Display Inventory****************
void DisplayInventory()
{
Item I;
fstream f1;
f1.open("INVENTORY.DAT",ios::binary|ios::in);
while(f1.read((char*) &I,sizeof(I)) )
{
I.showdata();
}
f1.close();
}
// ******************Search by Name*******************
void SearchN()
{
char sn[40];
Item I;
int found =0;
cout<<"\n\n Enter the name to be searched : ";
gets(sn);
fstream f1("INVENTORY.DAT",ios::binary|ios::in);
while(f1.read((char*)&I,sizeof(I)))
{
if(I.checkN(sn)==1)
{
found++;
I.showdata();
}
}
f1.close();
if (found==0)
cout<<"\n NO MATCH FOUND ! \N";
else
cout<<"\n TOTAL "<<found<<" ITEMS FOUND. \N";
}
// ******************Search by Barcode****************
void SearchB()
{
long sb;
Item I;
int found =0;
cout<<"\n\n Enter the barcode to be searched : ";
cin>>sb;
fstream f1;
f1.open("INVENTORY.DAT",ios::binary|ios::in);
while(f1.read((char*)&I,sizeof(I)))
{
if(I.checkB(sb)==1)
{
found++;
I.showdata();
}
}
f1.close();
if (found==0)
cout<<"\n NO MATCH FOUND ! \N";
else
cout<<"\n TOTAL "<<found<<" ITEMS FOUND. \N";
}
// ******************search by stock******************
void SearchS()
{
int ss;
Item I;
int found=0;
fstream f1;
char sp;
cout<<"\n\n Select choice for search by stock quantity";
cout<<"\n A.To search stock quantiy less than x";
cout<<"\n B.To search stock quantity greater than x";
cout<<"\n C.To search stock quantity equal to x";
sp=getche();
switch(sp)
{
case 'a':
case 'A': cout<<"\n\n Enter the value of x : ";
cin>>ss;
f1.open("INVENTORY.DAT",ios::binary|ios::in);
while(f1.read((char*)&I,sizeof(I)))
{
if(I.checkS(ss)==0)
{
found++;
I.showdata();
}
}
f1.close();
if (found==0)
cout<<"\n NO MATCH FOUND ! \N";
else
cout<<"\n TOTAL "<<found<<" ITEMS FOUND. \N";
break;
case 'B':
case 'b': cout<<"\n\n Enter the value of x : ";
cin>>ss;
f1.open("INVENTORY.DAT",ios::binary|ios::in);
while(f1.read((char*)&I,sizeof(I)))
{
if(I.checkS(ss)==2)
{
found++;
I.showdata();
}
}
f1.close();
if (found==0)
cout<<"\n NO MATCH FOUND ! \N";
else
cout<<"\n TOTAL "<<found<<" ITEMS FOUND. \N";
break;
case 'c':
case 'C': cout<<"\n\n Enter the value of x : ";
cin>>ss;
f1.open("INVENTORY.DAT",ios::binary|ios::in);
while(f1.read((char*)&I,sizeof(I)))
{
if(I.checkS(ss)==1)
{
found++;
I.showdata();
}
}
f1.close();
if (found==0)
cout<<"\n NO MATCH FOUND ! \N";
else
cout<<"\n TOTAL "<<found<<" ITEMS FOUND. \N";
break;
default: cout<<"\n Invalid Input !! \n Please enter a relevant choice. \n";
}
}
//******************Modify a record*******************
void modify()
{
char sn[40];
Item I;
int Modified=0;
cout<<"\n\n Enter the Item to be modified : ";
gets(sn);
fstream f1,f2;
f1.open("INVENTORY.DAT",ios::binary|ios::in);
f2.open("TEMP.DAT", ios::binary|ios::out);
while( f1.read((char*) &I, sizeof(I)) )
{
if(I.checkN(sn)==1)
{
Modified++ ;
I.Edit();
}
f2.write((char *) &I, sizeof(I));
}
f1.close();
f2.close();
if (Modified == 0)
cout<<"\n\n NO MATCH FOUND !! \n\n";
else
{
remove("INVENTORY.DAT");
rename("TEMP.DAT" , "INVENTORY.DAT");
}
}
//******************Delete a record*******************
void delete1()
{
char sn[40];
Item I;
int Deleted=0;
cout<<"\n Enter Item to be deleted : ";
gets(sn);
fstream f1,f2;
f1.open("INVENTORY.DAT",ios::binary|ios::in);
f2.open("TEMP.DAT", ios::binary|ios::out);
while( f1.read((char*) &I, sizeof(I)) )
{
if(I.checkN(sn)==1)
Deleted ++ ;
else
f2.write((char *) &I, sizeof(I));
}
f1.close();
f2.close();
if (Deleted == 0)
cout<<"\n\n NO MATCH FOUND !! \n\n";
else
{
remove("INVENTORY.DAT");
rename("TEMP.DAT" , "INVENTORY.DAT");
}
}
void UpdateInventory()
{
char choice;
do{
clrscr();
cout<<"Menu\n";
cout<<"Press 1 for APPEND AN ITEM \n";
cout<<"Press 2 for DISPLAY COMPLETE INVENTORY \n";
cout<<"Press 3 for SEARCH BY NAME \n";
cout<<"Press 4 for SEARCH BY BARCODE NUMBER \n";
cout<<"Press 5 for SEARCH BY QUANTITY IN STOCK \n";
cout<<"Press 6 for MODIFY A ITEM \n";
cout<<"Press 7 for DELETE A DELETE \n";
cout<<"Press 8 for MAIN MENU \n";
cout<<"Enter your choice : ";
choice=getche();
switch(choice)
{
case '1': Append();
break;
case '2': DisplayInventory();
break;
case '3': SearchN();
break;
case '4': SearchB();
break;
case '5': SearchS();
break;
case '6': modify();
break;
case '7': delete1();
break;
case '8':
break;
default : cout<<"\n Invalid Input !! \n Please enter a relevant choice. \n";
}
cout<<"\n\n Press any key to continue!";
getch();
}while(choice!='8');
}
//********************** ADD TO CART******************
void AddtoCart()
{
Item I;
long bc;
cout<<"\n\nEnter Barcode of Item:";
cin>>bc;
int flag=0,quantity;
ifstream f1("INVENTORY.DAT",ios::binary);
while(f1.read((char*)&I,sizeof(I))&&flag==0)
{
if(I.checkB(bc)==1)
{
cout<<"\n\nItem Selected:\n";
I.showdata();
flag=1;
}
}
f1.close();
if (flag==0)
cout<<"\n NO MATCH FOUND ! \n";
else
{
cout<<"\n\nEnter Quantiy to be bught:\n";
cin>>quantity;
if(quantity>I.stock)
cout<<"\nNot enough in stock!!\n";
else
{
cout<<quantity<<" "<<I.name<<" added to cart.\n\n";
ofstream f2("CART.DAT",ios::binary|ios::app);
I.stock=quantity;
f2.write((char*)&I,sizeof(I));
f2.close();
fstream f3,f4;
f3.open("INVENTORY.DAT",ios::binary|ios::in);
f4.open("TEMP.DAT",ios::binary|ios::out);
Item N;
while(f3.read((char*) &N,sizeof(N)) )
{
if(N.checkB(bc)==1)
{
N.stock-=I.stock;
f4.write((char*)&N,sizeof(N));
}
else
f4.write((char*)&N,sizeof(N));
}
f3.close();
f4.close();
remove("INVENTORY.DAT");
rename("TEMP.DAT","INVENTORY.DAT");
}
}
}
//*************DELETE FROM CART***************
void DelCart()
{
Item I;
long bc;
cout<<"\n\nEnter Barcode of Item:";
cin>>bc;
int flag=0;
fstream f1,f2;
f1.open("CART.DAT",ios::binary|ios::in);
f2.open("TEMP.DAT",ios::binary|ios::out);
while(f1.read((char*) &I,sizeof(I))&&flag==0)
{
if(I.checkB(bc)==1)
{
cout<<"\n\nItem DELETED:\n";
I.showdata();
flag=1;
fstream f3,f4;
f3.open("INVENTORY.DAT",ios::binary|ios::in);
f4.open("TEMP.DAT",ios::binary|ios::out);
Item N;
while(f3.read((char*) &N,sizeof(N)) )
{
if(N.checkB(bc)==1)
{
N.stock+=I.stock;
f4.write((char*)&N,sizeof(N));
}
else
f4.write((char*)&N,sizeof(N));
}
f3.close();
f4.close();
remove("INVENTORY.DAT");
rename("TEMP.DAT","INVENTORY.DAT");
}
else
f2.write((char*)&I,sizeof(I));
}
if(flag==0)
cout<<"No match found!!";
f1.close();
f2.close();
remove("CART.DAT");
rename("TEMP.DAT","CART.DAT");
}
void UpdateCart()
{
char choice;
do{
clrscr();
cout<<"Menu\n";
cout<<"Press 1 to DISPLAY INVENTORY \n";
cout<<"Press 2 to SEARCH FOR ITEM BY NAME \n";
cout<<"Press 3 to SEARCH FOR ITEM BY BARCODE \n";
cout<<"Press 4 to ADD ITEM TO CART \n";
cout<<"Press 5 to DELETE ITEM FROM CART \n";
cout<<"Press 6 to FINISH SHOPPING \n";
cout<<"Enter your choice : ";
choice=getche();
switch(choice)
{
case '1': DisplayInventory();
break;
case '2': SearchN();
break;
case '3': SearchB();
break;
case '4': AddtoCart();
break;
case '5': DelCart();
break;
case '6':
break;
default : cout<<"\n Invalid Input !! \n Please enter a relevant choice. \n";
}
cout<<"\n\n Press any key to continue!";
getch();
}while(choice!='6');
}
void ShowCart()
{
Item I;
fstream f1;
f1.open("CART.DAT",ios::binary|ios::in);
while(f1.read((char*) &I,sizeof(I)) )
{
I.showdata();
}
f1.close();
}
void CheckOut()
{
cout<<"\n\n Your Bill:\n\n";
Item I;
int amount=0;
fstream f1;
f1.open("CART.DAT",ios::binary|ios::in);
while(f1.read((char*) &I,sizeof(I)) )
{
I.showdata();
amount+=(I.price*I.stock);
}
f1.close();
amount*=1.18; //GST
cout<<"\n Total amount : "<<amount;
cout<<"\n\n Thank You for shopping with us today!\n";
}
void Sale()
{
char choice;
do{
clrscr();
cout<<"Menu\n";
cout<<"Press 1 to ADD OR DELETE ITEMS FROM CART \n";
cout<<"Press 2 to DISPLAY COMPLETE CART \n";
cout<<"Press 3 to CHECK OUT\n";
cout<<"Enter your choice : ";
choice=getche();
switch(choice)
{
case '1': UpdateCart();
break;
case '2': ShowCart();
break;
case '3':
break;
default : cout<<"\n Invalid Input !! \n Please enter a relevant choice. \n";
}
cout<<"\n\n Press any key to continue!";
getch();
}while(choice!='3');
CheckOut();
}
void ReducePrice()
{
cout<<"\n\nEnter Maximum stock threshold for standard pricing";
cout<<" above which a discount will be applied:";
int threshold=0,discount=0;
cin>>threshold;
dis:
cout<<"\n\nEnter %age discount for items above maximum threshold:";
cin>>discount;
if(discount>100)
{
cout<<"\nDiscount cannot be more than 100%!!\n Try Again.\n";
goto dis;
}
Item I;
fstream f1,f2;
f1.open("INVENTORY.DAT",ios::binary|ios::in);
f2.open("TEMP.DAT",ios::binary|ios::out);
while(f1.read((char*) &I,sizeof(I)) )
{
if(I.stock>threshold)
{
cout<<"Original Pricing :\n ";
I.showdata();
I.stock*=((100-discount)/100);
cout<<"Updated Pricing :\n";
f2.write((char*)&I,sizeof(I));
}
else
f2.write((char*)&I,sizeof(I));
}
f1.close();
f2.close();
remove("INVENTORY.DAT");
rename("TEMP.DAT","INVENTORY.DAT");
}
void IncreasePrice()
{
cout<<"\n\nEnter Minimum stock threshold for standard pricing";
cout<<" below which a surcharge will be applied:";
int threshold=0,surcharge=0;
cin>>threshold;
cout<<"\n\nEnter %age surcharge for items below minimum threshold:";
cin>>surcharge;
Item I;
fstream f1,f2;
f1.open("INVENTORY.DAT",ios::binary|ios::in);
f2.open("TEMP.DAT",ios::binary|ios::out);
while(f1.read((char*) &I,sizeof(I)) )
{
if(I.stock>threshold)
{
cout<<"Original Pricing :\n ";
I.showdata();
I.stock*=((100+surcharge)/100);
cout<<"Updated Pricing :\n";
f2.write((char*)&I,sizeof(I));
}
else
f2.write((char*)&I,sizeof(I));
}
f1.close();
f2.close();
remove("INVENTORY.DAT");
rename("TEMP.DAT","INVENTORY.DAT");
}
void Discount()
{
char choice;
do{
clrscr();
cout<<"Menu\n";
cout<<"Press 1 to REDUCE PRICES OF ITEMS WITH SURPLUS STOCK \n";
cout<<"Press 2 to INCREASE PRICES OF ITEMS WITH DEPLETING STOCK \n";
cout<<"Press 3 for MAIN MENU \n";
cout<<"Enter your choice : ";
choice=getche();
switch(choice)
{
case '1': ReducePrice();
break;
case '2': IncreasePrice();
break;
case '3':
break;
default : cout<<"\n Invalid Input !! \n Please enter a relevant choice. \n";
}
cout<<"\n\n Press any key to continue!";
getch();
}while(choice!='3');
}
void main()
{
int introsequence=0;
if(introsequence==0)
{
gotoxy(30,10);
cout<<"Welcome to Shreyas' Supermart!!"<<endl; //Game name
for(int i=0;i<1000;i++)
for(int j=0;j<1000;j++)
for(int l=0;l<1000;l++); //Delay using empty loop
clrscr();
gotoxy(28,10);
cout<<"Booting"<<endl;
for( i=0;i<4;i++)
{
gotoxy(36+i,10);
cout<<".";
for(int j=0;j<1000;j++)
for(int k=0;k<250;k++)
for(int l=0;l<1000;l++);
}
clrscr();
gotoxy(27,10);
cout<<"Good Day! How may I help you today?"<<endl;
for( i=0;i<1000;i++)
for(int j=0;j<1000;j++)
for(int l=0;l<1000;l++);
clrscr();
introsequence=1;
}
mainmenu:
char choice;
do{
clrscr();
cout<<"Main Menu\n";
cout<<"Press 1 to UPDATE INVENTORY\n";
cout<<"Press 2 to MAKE A SALE \n";
cout<<"Press 3 to CHANGE PRICES \n";
cout<<"Press 4 to QUIT\n";
cout<<"Enter your choice : ";
choice=getche();
switch(choice)
{
case '1': UpdateInventory();
break;
case '2': Sale();
break;
case '3': Discount();
break;
case '4':
break;
default : cout<<"\n Invalid Input !! \n Please enter a relevant choice. \n";
}
cout<<"\n\n Press any key to continue!";
getch();
}while(choice!='4');
remove("CART.DAT");
}