-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
1462 lines (1336 loc) · 52.5 KB
/
main.c
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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <windows.h>
#include <time.h>
// Define structures for items in the inventory
struct Item {
char id[20] ;
char name[50] ;
double price ;
int quantity ;
} ;
// Define structures for customers
struct Customer {
int id ;
char name[50] ;
char phoneNumber[20] ;
int pin ;
} ;
// Define structure for discounts
struct Discount {
int customerId ;
double discountPercentage ;
} ;
// Define structure for admin
struct Admin {
char username[50];
char password[50];
};
void gotoxy(int x, int y) ;
// Inventory management Functions
void displayInventory(void);
void addItem(struct Item newItem) ;
void removeItem(char *itemId) ;
void modifyItem(char *itemId) ;
void manageInventory(void) ;
void loadInventoryFromFile(const char *fileName) ;
void updateInventoryFile(const char *fileName) ;
void checkTransactions(void) ;
// Customer management Functions
void addCustomer(struct Customer newCustomer) ;
void removeCustomer(int customerId) ;
void displayCustomers(void) ;
void manageCustomers(void);
void loadCustomersWithDiscountsFromFile(void);
void writeCustomersWithDiscountsToFile(void) ;
// Discounts management Functions
void addDiscount(int customerId, double discountPercentage) ;
void removeDiscount(int customerId) ;
double getDiscount(int customerId) ;
void displayDiscounts(void) ;
void manageDiscounts(void) ;
// Admin Functions
void adminMenu(void) ;
void changeAdminKeys(void) ;
int authenticateAdmin(char *username, char *password) ;
// Shopping Functions
void storeCheckoutRecords(struct Item *cart, int cartItemCount, char *name, int customerId, double *checkoutPrices) ;
void shopping(struct Customer customer) ;
// Functions for managing inventory items
#define MAX_ITEMS 100
struct Item inventory[MAX_ITEMS]; // Array to hold all inventory items
int itemCount = 0; // To track the current number of items in inventory
// Was given in OLD Turbo C but declared for new 64-bit compilers as ConsoleCursor function is part of windows.h
void gotoxy(int x, int y) {
HANDLE hConsoleOutput;
COORD dwCursorPosition;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
dwCursorPosition.X = x;
dwCursorPosition.Y = y;
SetConsoleCursorPosition(hConsoleOutput, dwCursorPosition);
}
// Function to print formatted headings
void printMenu( char name[ ] ) {
printf("\033[1;36m") ;
for(int i = 0; i < 16 ; ++i ){
Sleep(50);
printf("\xDB");
}
for(int i = 0; i < strlen(name) ; ++i ){
Sleep(60);
printf(" %c", name[i] );
}
for(int i = 0; i < 16; i++){
Sleep(50);
printf("\xDB");
}
printf("\033[0m") ; // To reset to default color.
}
void loadInventoryFromFile(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
gotoxy(0 , 0) ;
printf("\033[1;31mError opening file %s.\n\033[0m", filename);
return;
}
// !feof(file) checks if the end-of-file indicator is not set at the current position in the file,
while (!feof(file) && itemCount < MAX_ITEMS) {
fscanf(file, "%s %s %lf %d", inventory[itemCount].id, inventory[itemCount].name,
&inventory[itemCount].price, &inventory[itemCount].quantity);
itemCount++;
}
fclose(file);
}
void displayInventory(void) {
if (itemCount == 0) {
gotoxy(20, 20);
printf("\033[1;31mInventory is empty.\033[0m\n");
return;
}
int row = 5 ;
gotoxy( 29, 1 ) ;
printMenu("INVENTORY-DASHBOARD ") ;
// printf("Inventory-Dashboard:\n");
// gotoxy( 55, 2 ) ;
// printf("********************\n") ;
gotoxy( 45 , row ) ;
printf("ID | Name | Price | Quantity\n");
for (int i = 0; i < itemCount; i++) {
gotoxy( 45, ++row ) ; // As previously used so I pre-incremented for not to be on same line
printf("%-6s | %-12s | %-7.2lf | %-3d\n", inventory[i].id, inventory[i].name, inventory[i].price, inventory[i].quantity);
}
}
void updateInventoryFile(const char *filename) {
FILE *file = fopen(filename, "w");
if (file == NULL) {
gotoxy( 20, 22) ;
printf("\033[1;31mError opening file %s for writing.\033[0m\n", filename);
return;
}
// Write the updated inventory to the file
for (int i = 0; i < itemCount; ++i) {
if( i == itemCount - 1 )
fprintf(file, "%s %s %.2lf %d", inventory[i].id, inventory[i].name, inventory[i].price, inventory[i].quantity);
else
fprintf(file, "%s %s %.2lf %d\n", inventory[i].id, inventory[i].name, inventory[i].price, inventory[i].quantity);
}
fclose(file);
}
void addItem(struct Item newItem) {
int found = 0 ;
if (itemCount >= MAX_ITEMS) {
gotoxy( 20 , 22 ) ;
printf("\033[1;31mInventory is full. Cannot add more items\033[0m\n\n");
Sleep(2000) ;
return;
}
// Check if the item ID already exists
for (int i = 0; i < itemCount; ++i) {
if ( strcmp (inventory[i].id , newItem.id ) == 0 ) {
gotoxy( 20, 22 );
printf("\033[1;31mAn item with the same ID already exists. Item not added.\033[0m\n");
Sleep(2000) ;
return;
}
else if( newItem.id[0] == 'D' || newItem.id[0] == 'G'|| newItem.id[0] == 'C'|| newItem.id[0] == 'S'|| newItem.id[0] == 'B' || newItem.id[0] == 'O') {
found = 1 ; // Don't break as need to check similiar IDs
}
}
if( ! found ) {
gotoxy( 20, 24 ) ;
printf("\033[1;31mItem unable to add cause of invalid ID-format\nTry Again!\033[0m\n") ;
Sleep(2000) ;
return ;
}
inventory[itemCount] = newItem;
itemCount++;
gotoxy( 20 , 22 );
printf("\033[1;32mItem added to inventory!\033[0m\n") ;
Sleep(1750) ;
updateInventoryFile("inventory_data.txt") ;
}
void removeItem(char *itemId) {
int found = 0;
for (int i = 0; i < itemCount; ++i) {
if ( strcmp (inventory[i].id , itemId ) == 0 ) {
// Shift elements to fill the gap caused by removing the item
for (int j = i; j < itemCount - 1; ++j) {
inventory[j] = inventory[j + 1];
}
itemCount--;
found = 1;
gotoxy( 20 , 20);
printf("\033[1;32mItem removed from inventory!\033[0m\n");
Sleep(1750) ;
updateInventoryFile("inventory_data.txt") ;
break;
}
}
if (!found) {
gotoxy( 20 , 20 );
printf("\033[1;31mItem with ID %s not found in inventory. Removal failed.\033[0m\n", itemId);
Sleep(2000) ;
}
}
void modifyItem(char *itemId) {
int itemIndex = -1;
for (int i = 0; i < itemCount; ++i) {
if (strcmp(inventory[i].id, itemId) == 0) {
itemIndex = i;
break;
}
}
if (itemIndex == -1) {
gotoxy( 20 , 20 ) ;
printf("\033[1;31mItem with ID %s not found in inventory. Update failed\033[0m\n", itemId);
Sleep(2000);
return;
}
char userChoice[20];
gotoxy( 20 , 20 ) ;
printf("Enter which thing to update (Quantity or Price or Both): ");
fgets(userChoice, sizeof(userChoice), stdin);
userChoice[strcspn(userChoice, "\n")] = '\0';
if (strcasecmp(userChoice, "quantity") == 0) {
int newQuantity;
gotoxy( 20 , 22 ) ;
printf("Enter new quantity: ");
if (scanf("%d", &newQuantity) != 1) {
fflush(stdin) ;
gotoxy( 20 , 24 ) ;
printf("\033[1;31mInvalid input for quantity. Update failed\033[0m\n");
Sleep(2000);
return;
}
inventory[itemIndex].quantity = newQuantity;
}
else if (strcasecmp(userChoice, "price") == 0) {
double newPrice;
gotoxy( 20 , 22 ) ;
printf("Enter new price: ");
if (scanf("%lf", &newPrice) != 1) {
fflush(stdin) ;
gotoxy( 20 , 24 ) ;
printf("\033[1;31mInvalid input for price. Update failed\033[0m\n");
Sleep(2000);
return;
}
inventory[itemIndex].price = newPrice;
}
else if (strcasecmp(userChoice, "both") == 0) {
int newQuantity;
gotoxy( 20 , 22 ) ;
printf("Enter new quantity: ");
if (scanf("%d", &newQuantity) != 1) {
fflush(stdin) ;
gotoxy( 20 , 24 ) ;
printf("\033[1;31mInvalid input for quantity. Update failed\033[0m\n");
Sleep(2000);
return;
}
inventory[itemIndex].quantity = newQuantity;
double newPrice;
gotoxy( 20 , 24 ) ;
printf("Enter new price: ");
if (scanf("%lf", &newPrice) != 1) {
fflush(stdin) ;
gotoxy( 20 , 26 ) ;
printf("\033[1;31mInvalid input for price. Update failed\033[0m\n");
Sleep(2000);
return;
}
inventory[itemIndex].price = newPrice;
}
else {
gotoxy( 20 , 22 ) ;
printf("\033[1;31mWrong Choice Entered!\033[0m\n");
return;
}
gotoxy( 18 , 26 ) ;
printf("\033[1;32mItem modified to %d quantity and %.2lf price!\033[0m\n", inventory[itemIndex].quantity, inventory[itemIndex].price);
Sleep(1750);
updateInventoryFile("inventory_data.txt");
}
// Functions for managing customers
#define MAX_CUSTOMERS 150
struct Customer customers[MAX_CUSTOMERS]; // Array to hold all customer details
int customerCount = 0; // Variable to track the current number of customers
// Functions for managing discounted customers
#define MAX_DISCOUNTS 100
struct Discount discounts[MAX_DISCOUNTS]; // Array to hold all discounts
int discountCount = 0; // Variable to track the current number of discounts
void loadCustomersWithDiscountsFromFile(void) {
FILE *file = fopen("customers_data.bin", "rb");
if (file == NULL) {
printf("\033[1;31mError opening file %s for reading.\033[0m\n", "customers_data.bin");
return;
}
fread(&customerCount, sizeof(int), 1, file); // Read the total number of customers first
for (int i = 0; i < customerCount; ++i) {
fread(&customers[i], sizeof(struct Customer), 1, file); // Read each customer structure
}
fread(&discountCount, sizeof(int), 1, file) ;
for (int i = 0; i < customerCount; ++i) {
fread(&discounts[i], sizeof(struct Discount), 1, file); // Read each discount structure then in sequence
}
fclose(file);
}
void writeCustomersWithDiscountsToFile(void) {
FILE *file = fopen("customers_data.bin", "wb");
if (file == NULL) {
printf("\033[1;31mError opening file %s for writing.\033[0m\n", "customers_data.bin");
return;
}
fwrite(&customerCount, sizeof(int), 1, file); // Write the total number of customers first
for (int i = 0; i < customerCount; ++i) {
fwrite(&customers[i], sizeof(struct Customer), 1, file); // Write each customer structure
}
fwrite(&discountCount, sizeof(int), 1, file) ;
for (int i = 0; i < customerCount; ++i) {
fwrite(&discounts[i], sizeof(struct Discount), 1, file); // Write each discount structure then in sequence
}
fclose(file);
}
void addCustomer(struct Customer newCustomer) {
if (customerCount >= MAX_CUSTOMERS) {
gotoxy(20,23);
printf("\033[1;31mMaximum customers reached. Cannot add more customers\033[0m\n");
Sleep(2000) ;
return;
}
customers[customerCount] = newCustomer;
customerCount++;
gotoxy(20,23);
printf("\033[1;32m%s Customer is allotted with ID-Number %d.\033[0m\n", newCustomer.name, newCustomer.id);
Sleep(1750) ;
// After adding the customer, save the updated data to the file. Otherwise it was not properly reading .
writeCustomersWithDiscountsToFile( ); // This function should be called separately not integrated with in it otherwise not properly updating
}
void removeCustomer(int customerId) {
int found = 0;
for (int i = 0; i < customerCount; ++i) {
if (customers[i].id == customerId) {
// Shift elements to fill the gap caused by removing the customer
for (int j = i; j < customerCount - 1; ++j) {
customers[j] = customers[j + 1];
customers[j].id -- ; // -1 from IDs also
}
customerCount--;
found = 1;
gotoxy( 20 , 20);
printf("\033[1;32mCustomer with %d-ID removed!\033[0m\n", customerId );
Sleep(1750) ;
writeCustomersWithDiscountsToFile( ) ;
break;
}
}
if (!found) {
gotoxy(20,20);
printf("\033[1;31mCustomer with ID %d not found. Removal failed.\033[0m\n", customerId);
Sleep(2000) ;
}
}
void displayCustomers(void) {
if (customerCount == 0) {
gotoxy(20,16);
printf("\033[1;31mNo customers added in the list.\033[0m\n");
Sleep(2000) ;
return;
}
int row = 5;
gotoxy(15,2);
printMenu("CUSTOMERS-DASHBOARD ") ;
// printf("==========Customers Dasboard==========\n");
// gotoxy(15,3);
// printf("\t\t ******************\n") ;
gotoxy(20,4);
printf("ID | Name | Phone_Num\n");
for (int i = 0; i < customerCount; ++i) {
gotoxy( 20 , ++row );
printf("%d | %-12s | %s\n", customers[i].id, customers[i].name, customers[i].phoneNumber);
}
}
// Functions for managing discounts
void addDiscount(int customerId, double discountPercentage) {
if (discountCount >= MAX_DISCOUNTS) {
gotoxy(20,18);
printf("\033[1;31mMaximum number of discounts reached. Cannot add more discounts\033[0m\n");
Sleep(2000) ;
return;
}
else if ( customerId > customerCount ) {
gotoxy(20,18);
printf("\033[1;31mCustomer with %d-ID not exists\033[0m\n", customerId ) ;
Sleep(2000) ;
return ;
}
// Check if the discount for the customer already exists
for (int i = 0; i < discountCount; ++i) {
if (discounts[i].customerId == customerId) {
gotoxy(20,18);
printf("\033[1;31mA discount for the same customer already exists. Discount not added\033[0m\n");
Sleep(2000) ;
return ;
}
}
discounts[discountCount].customerId = customerId;
discounts[discountCount].discountPercentage = discountPercentage;
discountCount++;
gotoxy(20,18);
printf("\033[1;32mDiscount added for the %d-ID Customer!\033[0m\n", customerId ) ;
Sleep(1750) ;
writeCustomersWithDiscountsToFile( ) ;
}
void removeDiscount(int customerId) {
int found = 0;
for (int i = 0; i < discountCount; ++i) {
if (discounts[i].customerId == customerId) {
// Shift elements to fill the gap caused by removing the discount
for (int j = i; j < discountCount - 1; ++j) {
discounts[j] = discounts[j + 1];
}
discountCount--;
found = 1;
gotoxy( 20 , 16 );
printf("\033[1;32mDiscount removed for the %d-ID customer!\033[0m\n", customerId );
Sleep( 1750 ) ;
writeCustomersWithDiscountsToFile( ) ;
break;
}
}
if (!found) {
gotoxy(20,16);
printf("\033[1;31mNo discount found for customer ID %d. Removal failed\033[0m\n", customerId);
Sleep( 2000 ) ;
}
}
double getDiscount(int customerId) {
double discount = 0.0;
for (int i = 0; i < discountCount; ++i) {
if (discounts[i].customerId == customerId) {
discount = discounts[i].discountPercentage;
break;
}
}
return discount;
}
void displayDiscounts(void) {
int row = 10 ;
gotoxy( 32, 5 ) ;
printMenu("ALL DISCOUNTEES ") ;
// printf("--- All Discounts ---\n");
// gotoxy( 50, 6 ) ;
// printf(" ************\n") ;
gotoxy( 50, 8 ) ;
printf("|Customer ID|Discount %%|\n");
for (int i = 0; i < discountCount; ++i) {
gotoxy( 50, row++ ) ; // Now here since not previously used so post-incremenr is better
printf("| %-9d | %-7.2lf%% |\n", discounts[i].customerId, discounts[i].discountPercentage);
}
}
void checkTransactions(void) {
FILE *file = fopen("Records.txt", "r") ;
if(file == NULL) {
gotoxy( 20, 18 ) ;
printf("\033[1;31mError opening file %s for writing.\033[0m\n", "Records.bin");
return;
}
char info ;
while( ( info = fgetc(file) ) != EOF ) {
printf("%c", info ) ;
}
}
// Functions for admin interface
struct Admin adminUser = {"admin", "pass123"}; // Sample password
int authenticateAdmin(char *username, char *password) {
if (strcmp(username, adminUser.username) == 0 && strcmp(password, adminUser.password) == 0)
return 1;
else
return 0;
}
// Functions for managing inventory in the admin menu
void manageInventory(void) {
int choice;
do {
gotoxy(15,3);
printf("========Inventory Management========\n");
gotoxy(20,4);
printf("1. Add Item\n");
gotoxy(20,6);
printf("2. Remove Item\n");
gotoxy(20,8);
printf("3. Modify Item\n");
gotoxy(20,10);
printf("4. Display Inventory\n");
gotoxy(20,12);
printf("5. Display All Transactions\n");
gotoxy(20,14);
printf("0. Back to Admin Menu\n");
gotoxy(20,16);
printf("Enter your choice: ");
scanf("%d", &choice);
fflush( stdin ) ;
switch (choice) {
case 1: {
system("cls");
struct Item newItem;
gotoxy(12,3);
printf("\033[1;31mInstructions:-\033[0m");
gotoxy(15,4);
printf("ID must follow these prefixes for specific sections in the format: 1stFixedChar(NUM or CHAR)_NUM ") ;
gotoxy(15,5);
printf("For Dairy (D) section: ID should be like DD_188\n");
gotoxy(15,6);
printf("For Cooking Products (G or C) section: ID should be like G/C_NUM\n");
gotoxy(15,7);
printf("For Snacks (S) section: ID should be like S_NUM\n");
gotoxy(15,8);
printf("For Beverages (B) section: ID should be like B_NUM\n");
gotoxy(15,9);
printf("For Other Products (O) section: ID should be like O_NUM\n") ;
gotoxy(20,14);
printf("Enter item ID: ");
fgets(newItem.id, sizeof(newItem.id), stdin ) ;
newItem.id[ strcspn( newItem.id, "\n" ) ] = '\0' ;
gotoxy(20,16);
printf("Enter item name: ");
fgets(newItem.name, sizeof(newItem.name), stdin ) ;
newItem.name[ strcspn( newItem.name, "\n" ) ] = '\0' ;
gotoxy(20,18);
printf("Enter item price: ");
scanf("%lf", &newItem.price);
fflush(stdin) ;
gotoxy(20,20);
printf("Enter item quantity: ");
scanf("%d", &newItem.quantity);
fflush(stdin) ;
addItem(newItem) ;
system("cls") ;
break;
}
case 2: {
char itemId[20] ;
gotoxy(20,18);
printf("Enter item ID to remove: ");
fgets(itemId, sizeof(itemId), stdin) ;
itemId[ strcspn( itemId, "\n" ) ] = '\0' ;
removeItem(itemId);
system("cls") ;
break;
}
case 3: {
char itemId[20] ;
int newQuantity ;
double newPrice ;
gotoxy(20,18);
printf("Enter item ID to update: ");
fgets(itemId, sizeof(itemId), stdin) ;
itemId[ strcspn( itemId, "\n" ) ] = '\0' ;
modifyItem(itemId);
system("cls") ;
break;
}
case 4:
system("cls") ;
displayInventory();
printf("\n\033[1;33;5mPress any key to exit viewing.\033[0m\n") ;
getch( ) ;
system("cls") ;
break;
case 5:
system("cls") ;
checkTransactions() ;
printf("\n\033[1;33;5mPress any key to go back.\033[0m\n") ;
getch( ) ;
fflush(stdin) ;
system("cls") ;
case 0:
gotoxy(20,19);
printf("Returning to Admin Menu...\n");
Sleep(1750) ;
system("cls");
break;
default:
fflush(stdin) ;
gotoxy(20,19);
printf("\033[1;31mInvalid choice! Please enter a valid option..\033[0m\n");
break ;
}
} while (choice != 0);
}
// Functions for managing customers in the admin menu
void manageCustomers(void) {
int choice;
do {
gotoxy(15,3);
printf("========Customer Management========");
gotoxy(20,6);
printf("1. Add Customer\n");
gotoxy(20,8);
printf("2. Remove Customer\n");
gotoxy(20,10);
printf("3. Display Customers\n");
gotoxy(20,12);
printf("0. Back to Admin Menu\n");
gotoxy(20,14);
printf("Enter your choice: ") ;
scanf("%d", &choice);
fflush(stdin) ;
switch (choice) {
case 1: {
struct Customer newCustomer;
newCustomer.id = customerCount + 1 ;
gotoxy(20,17);
printf("Enter customer name: ");
gotoxy(20,19);
fgets(newCustomer.name, sizeof(newCustomer.name), stdin ) ;
newCustomer.name[ strcspn( newCustomer.name,"\n" ) ] = '\0' ;
gotoxy( 20 , 21 ) ;
printf("Enter customer phone_number: ") ;
fgets(newCustomer.phoneNumber, sizeof(newCustomer.phoneNumber), stdin ) ;
newCustomer.phoneNumber[ strcspn( newCustomer.phoneNumber, "\n" ) ] = '\0' ;
for(int i = 0; i < customerCount; ++i) {
if( strcmp ( newCustomer.phoneNumber , customers[i].phoneNumber ) == 0 ) {
gotoxy(25, 24) ;
printf("\033[1;31m%s Phone_Number is already registered.\n\t\t\t\tPlease Try Again!\033[0m\n", newCustomer.phoneNumber ) ;
Sleep( 2000 ) ;
system("cls") ;
return ;
}
}
addCustomer(newCustomer);
system("cls") ;
break;
}
case 2: {
int customerId;
gotoxy(20,17);
printf("Enter customer ID to remove: ");
scanf("%d", &customerId);
fflush(stdin) ;
removeCustomer(customerId);
system("cls") ;
break;
}
case 3:
system("cls") ;
displayCustomers();
printf("\n\033[1;33;5mPress Any Key to Go back to Management menu:\033[0m\n");
getch( ) ;
system("cls") ;
break;
case 0:
gotoxy(20,17);
printf("Returning to Admin Menu.\n");
Sleep(1750) ;
system("cls") ;
break;
default:
fflush(stdin) ;
gotoxy(20,17);
system("cls") ;
printf("\033[1;31mInvalid choice! Please enter a valid option.\033[0m\n");
}
} while (choice != 0);
}
// Functions for managing discounts in the admin menu
void manageDiscounts(void) {
int choice;
do {
gotoxy(12,2);
printf("========Discount Management========");
gotoxy(20,4);
printf("1. Add Discount\n");
gotoxy(20,6);
printf("2. Remove Discount\n");
gotoxy(20,8);
printf("3. Display Customer Discount List\n");
gotoxy(20, 10);
printf("0. Back to Admin Menu\n");
gotoxy(20,12);
printf("Enter your choice: ");
scanf("%d", &choice);
fflush( stdin ) ;
switch (choice) {
case 1: {
int customerId;
double discountPercentage;
gotoxy(20,14);
printf("Enter customer ID for discount: ");
scanf("%d", &customerId);
fflush( stdin ) ;
gotoxy(20,16);
printf("Enter discount percentage: ");
scanf("%lf", &discountPercentage);
fflush( stdin ) ;
addDiscount(customerId, discountPercentage);
system("cls") ;
break;
}
case 2: {
int customerId;
gotoxy(20,14);
printf("Enter customer ID to remove discount: ");
scanf("%d", &customerId);
fflush( stdin ) ;
removeDiscount(customerId);
system("cls") ;
break;
}
case 3:
system("cls") ;
displayDiscounts();
printf("\n\033[1;33;5mPress any key to go back.\033[0m\n") ;
getch( ) ;
system("cls") ;
break;
case 0:
gotoxy(20,14);
printf("Returning to Admin Menu.\n") ;
Sleep(1750) ;
system("cls") ;
break;
default:
fflush( stdin ) ;
gotoxy(20,14);
printf("\033[1;31mInvalid choice! Please enter a valid option.\033[0m\n");
Sleep(2000) ;
system("cls") ;
}
} while (choice != 0);
}
void changeAdminKeys( void ) {
char existingPass[50] ;
char newUsername[50] ;
char newPassword[50] ;
gotoxy( 20, 4 );
printf("Enter the previous password for admin : ") ;
int i = 0;
while (i < 49) {
if (_kbhit()) {
char ch = _getch() ;
if (ch == 13) {
existingPass[i] = '\0';
break;
} else if (ch == 8) {
if (i > 0) {
printf("\b \b");
i--;
}
} else {
existingPass[i] = ch;
printf("*");
i++;
}
}
}
gotoxy( 0 , 7 ) ;
printf("PLEASE WAIT!!\n\n\t\t\033[1;33;5mVALIDATING CREDENTIALS...\033[0m\n") ;
Sleep(2250) ;
if ( strcmp (existingPass, adminUser.password) == 0 ) {
gotoxy(15, 9) ;
printf("\033[1;31mNote:-\033[0mChange Password At your own risk.\n") ;
gotoxy(15, 11) ;
printf("Enter new admin username: ");
fgets(newUsername, sizeof(newUsername), stdin ) ;
newUsername[ strcspn(newUsername, "\n") ] = '\0' ;
gotoxy(15, 13) ;
printf("Enter new admin password: ");
fgets(newPassword, sizeof(newPassword), stdin ) ;
newPassword[ strcspn(newPassword, "\n") ] = '\0' ;
// Update admin credentials
strcpy(adminUser.username, newUsername);
strcpy(adminUser.password, newPassword);
gotoxy(20, 18) ;
printf("\033[1;32mAdmin credentials updated successfully!\033[0m\n");
}
else {
gotoxy(20, 18) ;
printf("\n\033[1;31mSecurity-Alert Sent to Server!\033[0m") ;
// Playing siren like sound via Beep
int initialFrequency = 1000 ;
for (int i = 0; i < 20; ++i) {
Beep(initialFrequency, 200);
initialFrequency += 50;
Sleep(100);
}
exit(1) ;
}
}
void adminMenu(void) {
char username[50];
char password[50];
int choice;
system("cls");
gotoxy(20,5);
printMenu( "Authentication For Admin ") ;
// printf("========Authentication For Admin========");
// gotoxy(18,6);
// printf("*********************************************\n");
gotoxy(24,8);
printf("Enter Admin Username: ");
fgets(username, sizeof(username), stdin) ;
username[ strcspn( username, "\n" ) ] = '\0';
fflush(stdin);
gotoxy(24,10);
printf("Enter Admin Password: ");
int i = 0;
while (i < 49) {
if (_kbhit()) {
char ch = _getch() ;
if (ch == 13) { // If character is not ENTER KEY (\r For Conio) then
password[i] = '\0';
break;
} else if (ch == 8) { // Backspace
if (i > 0) {
printf("\b \b"); // Move cursor back, erase character, move cursor back again
i--;
}
} else {
password[i] = ch;
printf("*"); // Masking the input
i++;
}
}
}
gotoxy( 0, 14 ) ;
printf("PLEASE WAIT....\n\nYOUR DATA IS BEING PROCESSED....");
Sleep( 1500 ) ;
if (authenticateAdmin(username, password)) {
system("cls");
gotoxy(10,3);
printf("\033[1;32m========Authentication successful! Welcome, Admin========\033[0m\n");
do {
gotoxy(20,5);
printf("========Admin Menu========");
gotoxy(20,7);
printf("1. Manage Inventory");
gotoxy(20,9);
printf("2. Manage Customers\n");
gotoxy(20,11);
printf("3. Manage Discounts\n");
gotoxy(20,13);
printf("4. Change Admin Credentials\n");
gotoxy(20,15);
printf("5. Exit Admin Menu\n");
gotoxy(20,17);
printf("Enter your choice: ");
scanf("%d", &choice);
fflush( stdin ) ;
switch (choice) {
case 1:
system("cls") ;
manageInventory() ;
break;
case 2:
system("cls") ;
manageCustomers() ;
break;
case 3:
system("cls") ;
manageDiscounts() ;
break;
case 4:
system("cls") ;
changeAdminKeys() ;
Sleep(1750) ;
system("cls") ;
break ;
case 5:
gotoxy(20,20);
printf("Exiting Admin Menu.\n");
Sleep(1750) ;
system("cls") ;
break;
default:
fflush(stdin) ;
gotoxy(20,20);
printf("\033[1;31mInvalid choice! Please enter a valid option\033[0m\n");
break ;
}
} while (choice != 5);
}
else {
gotoxy(15,18);
printf("\033[1;31m!!!!!Authentication failed! Access denied!!!!!.\033[0m\n") ;
Sleep(1750) ;
system("cls");
}
}
// Function to store checkout records in a file with date and time
void storeCheckoutRecords(struct Item *cart, int cartItemCount, char *name, int customerId , double *checkoutPrices ) {
FILE *file = fopen("Records.txt", "a");
if (file == NULL) {
printf("Error opening file for storing records.\n");
return;
}
// Get current date and time
time_t currentTime;
struct tm *localTime;
time(¤tTime);
localTime = localtime(¤tTime);
double totalPrice = 0.0 ;
if ( getDiscount( customerId ) > 0 ) {
fprintf(file, "\n (Old SignedUp Customer) Name : %s", name ) ;
// Write timestamp to file
fprintf(file, "\t\tCheckout Date and Time: %s", asctime(localTime));
// Write cart details to file
fprintf(file, "ID | Name | Price | Quantity\n");
for (int i = 0; i < cartItemCount; ++i) {
fprintf(file, "%-6s | %-12s | %-7.2lf | %-3d\n", cart[i].id, cart[i].name, checkoutPrices[i] , cart[i].quantity);
totalPrice += checkoutPrices[i] ;