-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdmin.cpp
More file actions
1355 lines (1137 loc) · 42.4 KB
/
Admin.cpp
File metadata and controls
1355 lines (1137 loc) · 42.4 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
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 "Admin.h"
using namespace std;
// Constants for limits
const int MAX_CUSTOMERS = 100;
const int MAX_CART_SIZE = 500;
const int MAX_USERS = 200;
const int MAX_USERNAME_LENGTH = 50;
const int MAX_PASSWORD_LENGTH = 50;
const int ROLE_ADMIN = 0;
const int ROLE_EMPLOYEE = 1;
const int ROLE_CUSTOMER = 2;
const int OTP_LENGTH = 6;
const char* OTP_FILE = "admin_otp.txt";
const int MAX_SALES_RECORDS = 1000;
const int MAX_DAYS = 365;
const int MAX_CATEGORIES = 10;
const int MAX_PRODUCTS = 100;
const int MAX_ACTIVITY_LOGS = 1000;
const char* ACTIVITY_LOG_FILE = "ActivityLogs.txt";
const int MAX_ANNOUNCEMENTS = 50;
const int MAX_ANNOUNCEMENT_LENGTH = 200;
const int MAX_TITLE_LENGTH = 100;
// Activity Log Structures
char activityLogUsernames[MAX_ACTIVITY_LOGS][50];
char activityLogActions[MAX_ACTIVITY_LOGS][100];
long activityLogTimestamps[MAX_ACTIVITY_LOGS];
int activityLogCount = 0;
char* announcementTitles[MAX_ANNOUNCEMENTS];
char* announcementMessages[MAX_ANNOUNCEMENTS];
long announcementTimestamps[MAX_ANNOUNCEMENTS];
int announcementTargetRoles[MAX_ANNOUNCEMENTS];
int announcementCount = 0;
// Global arrays
char productnames[MAX_PRODUCTS][50];
int ProductIDs[MAX_PRODUCTS];
double productPrices[MAX_PRODUCTS];
int productstockQuantities[MAX_PRODUCTS];
char productCategories[MAX_PRODUCTS][50];
int productsoldQuantities[MAX_PRODUCTS] = {0};
char userUsernames[MAX_USERS][MAX_USERNAME_LENGTH];
char userPasswords[MAX_USERS][MAX_PASSWORD_LENGTH];
int userRoles[MAX_USERS];
bool userActiveStatus[MAX_USERS];
int userCount = 0;
int salesProductIDs[MAX_SALES_RECORDS];
double salesPrices[MAX_SALES_RECORDS];
int salesQuantities[MAX_SALES_RECORDS];
double salesRevenues[MAX_SALES_RECORDS];
long salesDates[MAX_SALES_RECORDS];
int salesRecordCount = 0;
char categoriesNames[MAX_CATEGORIES][50];
double categoriesRevenue[MAX_CATEGORIES] = {0};
int categoriesCount = 0;
int catalog_Size = 0;
void adminloginmenu() {
Loadcatalog(); // Load the catalog from file
loadSalesRecords();
//generateSalesReports();
char c;
while (true) {
cout << "\n\t\t\t====================================" << endl;
cout << "\t\t\t Welcome to SecureShop " << endl;
cout << "\t\t\t====================================" << endl;
// Main Menu Options
cout << "\n\t\t\t MAIN MENU \n";
cout << "\t\t\t------------------------------------" << endl;
cout << "\t\t\t 1. Login " << endl;
cout << "\t\t\t 2. Exit " << endl;
cout << "\t\t\t------------------------------------" << endl;
// Prompt for User Input
cout << "\t\t\t Enter your choice: ";
cin >> c;
switch (c) {
case '1':
cout << "\n\t\t\t Redirecting to Admin Login...\n";
adminlogin();
break;
case '2':
cout << "\n\t\t\t Saving Catalog...\n";
Savecatalog();
cout << "\t\t\t====================================" << endl;
cout << "\t\t\t Thank you for using SecureShop! " << endl;
cout << "\t\t\t====================================" << endl;
exit(0);
break;
default:
cout << "\n\t\t\t Invalid choice. Please try again.\n";
break;
}
}
}
// User Authentication
void adminlogin() {
char username[50], password[50];
cout << "\nUsername: ";
cin >> username;
cout << "Password: ";
cin >> password;
// Load users first
loadUsers();
// Find user and validate
int index = findUserIndex(username);
if (index != -1 &&
strcmp(userPasswords[index], password) == 0 &&
userRoles[index] == ROLE_ADMIN &&
userActiveStatus[index]) {
logActivity(username, "Successful Admin Login");
// Generate and send OTP
char otp[OTP_LENGTH + 1];
generateOTP(otp);
saveOTP(username, otp);
// Prompt for OTP
char enteredOTP[OTP_LENGTH + 1];
cout << "\nA 6-digit OTP has been generated. Please enter the OTP: ";
cin >> enteredOTP;
// Verify OTP
if (verifyOTP(enteredOTP)) {
cout << "\nTwo-Factor Authentication Successful!\n";
adminMenu();
return;
} else {
logActivity(username, "Failed 2FA Authentication");
cout << "\nInvalid OTP. Access Denied.\n";
return;
}
}
logActivity(username, "Failed Login Attempt");
cout << "\nInvalid username or password.\n";
}
// Admin Panel
void adminMenu() {
// Load users and logs before showing menu
loadUsers();
loadActivityLogs();
while (true) {
char choice;
// Admin Panel Banner
cout << "\n\t\t====================================" << endl;
cout << "\t\t| ADMIN PANEL |" << endl;
cout << "\t\t====================================" << endl;
// Menu Options
cout << "\t\t| 1. Add Product |\n";
cout << "\t\t| 2. Display Products |\n";
cout << "\t\t| 3. Advanced Search |\n";
cout << "\t\t| 4. Generate Reports |\n";
cout << "\t\t| 5. User Management |\n";
cout << "\t\t| 6. Security Monitoring |\n";
cout << "\t\t| 7. Add Anouncement |\n";
cout << "\t\t| 8. Logout |\n";
cout << "\t\t====================================" << endl;
// Prompt for User Input
cout << "\t\tEnter your choice: ";
cin >> choice;
cout << "\n----------------------------------------\n";
cout << endl;
switch (choice) {
case '1':
cout << "Redirecting to Add Product...\n";
AddProduct();
break;
case '2':
cout << "Displaying all products...\n";
displayProduct();
break;
case '3':
cout << "Redirecting to Advanced Search...\n";
advanceSearch();
break;
case '4':
cout << "Generating Reports...\n";
GenerateReports();
break;
case '5':
userManagementMenu();
break;
case '6':
cout << "Monitoring Security Logs...\n";
monitorSecurityThreats();
displayActivityLogs();
break;
case '7':
addAnnouncementMenu();
break;
case '8':
cout << "\t\t=======================================" << endl;
cout << "\t\t| Logging Out... |\n";
cout << "\t\t| Thank you for using Admin Panel! |\n";
cout << "\t\t=======================================" << endl;
return;
default:
cout << "Invalid choice. Please try again.\n";
cout << "\n----------------------------------------\n";
}
}
}
// Admin Functions
void AddProduct() {
if (catalog_Size >= MAX_PRODUCTS) {
cout << "\n====================================" << endl;
cout << " Catalog is Full! " << endl;
cout << "====================================" << endl;
cout << "Cannot add more products.\n";
return;
}
cout << "\n====================================" << endl;
cout << " Add New Product " << endl;
cout << "====================================" << endl;
cout << "Enter product name: ";
cin.ignore();
cin.getline(productnames[catalog_Size], 50);
cout << "Enter product ID: ";
cin >> ProductIDs[catalog_Size];
cout << "Enter price: ";
cin >> productPrices[catalog_Size];
cout << "Enter stock quantity: ";
cin >> productstockQuantities[catalog_Size];
cout << "Enter category: ";
cin.ignore();
cin.getline(productCategories[catalog_Size], 50);
catalog_Size++;
Savecatalog(); // Save the catalog immediately after adding a product
cout << "\n------------------------------------" << endl;
cout << "Product added and saved successfully!" << endl;
cout << "------------------------------------" << endl;
}
void displayProduct()
{
// Check if catalog is empty
if (catalog_Size == 0) {
cout << "\n====================================" << endl;
cout << " No products available in the catalog." << endl;
cout << "====================================\n";
return;
}
// Display product header
cout << "\n====================================" << endl;
cout << " Available Products " << endl;
cout << "====================================" << endl;
// Display table headers with spacing
cout << "ID Name Price Stock Category" << endl;
cout << "---------------------------------------------------------------------------" << endl;
// Display all products in catalog
for (int i = 0; i < catalog_Size; i++)
{
cout << ProductIDs[i] << " " << left << setw(20) << productnames[i] << " " << right << setw(8) << fixed << setprecision(2) <<
productPrices[i] << " " << right << setw(5) << productstockQuantities[i] << " " << left << setw(15) << productCategories[i] << endl;
}
cout << "--------------------------------------------------------------------------\n";
}
// [Previous code remains the same, adding the complete advancedSearch() and remaining functions]
void advanceSearch() {
char category[50] = "";
double minPrice = 0.0, maxPrice = 1e9;
int availabilityOption = -1;
cout << "\n====================================" << endl;
cout << " Advanced Product Search " << endl;
cout << "====================================" << endl;
// Get search criteria
cout << "Enter category (leave blank for any): ";
cin.ignore();
cin.getline(category, 50);
cout << "Enter minimum price (default 0): ";
cin >> minPrice;
cout << "Enter maximum price (leave blank or 0 for no limit): ";
cin >> maxPrice;
// Availability options
cout << "\nAvailability options:\n";
cout << "1. In Stock\n2. Out of Stock\n3. Any\n";
cout << "Enter choice: ";
cin >> availabilityOption;
cout << "\n====================================" << endl;
cout << " Search Results " << endl;
cout << "====================================" << endl;
cout << "ID Name Price Stock Category" << endl;
cout << "------------------------------------------------------------\n";
bool found = false;
for (int i = 0; i < catalog_Size; i++) {
bool categoryMatch = (strlen(category) == 0 || strcmp(productCategories[i], category) == 0);
bool priceMatch = (productPrices[i] >= minPrice && productPrices[i] <= maxPrice);
bool stockMatch = true;
// Check stock availability
if (availabilityOption == 1) stockMatch = (productstockQuantities[i] > 0);
else if (availabilityOption == 2) stockMatch = (productstockQuantities[i] == 0);
// If all conditions match, display the product
if (categoryMatch && priceMatch && stockMatch) {
cout << ProductIDs[i] << " "
<< left << setw(20) << productnames[i] << " "
<< right << setw(8) << fixed << setprecision(2) << productPrices[i] << " "
<< right << setw(5) << productstockQuantities[i] << " "
<< left << setw(15) << productCategories[i] << endl;
found = true;
}
}
// If no matching products are found
if (!found) cout << "No products match the search criteria.\n";
cout << "------------------------------------------------------------\n";
}
bool addUser(const char* username, const char* password, int role) {
// Check if username already exists
for (int i = 0; i < userCount; i++) {
if (strcmp(userUsernames[i], username) == 0) {
cout << "Username already exists!\n";
return false;
}
}
// Check if we've reached max users
if (userCount >= MAX_USERS) {
cout << "Maximum number of users reached!\n";
return false;
}
// Add new user
strncpy(userUsernames[userCount], username, MAX_USERNAME_LENGTH - 1);
strncpy(userPasswords[userCount], password, MAX_PASSWORD_LENGTH - 1);
userRoles[userCount] = role;
userActiveStatus[userCount] = true;
userCount++;
saveUsers(); // Immediately save to persistent storage
cout << "User added successfully!\n";
return true;
}
void addUser() {
char username[50], password[50];
int role;
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
cout << "Select role:\n";
cout << "0. Admin\n";
cout << "1. Employee\n";
cout << "2. Customer\n";
cout << "Enter role number: ";
cin >> role;
// Validate role input
if (role < ROLE_ADMIN || role > ROLE_CUSTOMER) {
cout << "Invalid role. Defaulting to Customer.\n";
role = ROLE_CUSTOMER;
}
// Call existing addUser function with input
addUser(username, password, role);
}
// Find User Index
int findUserIndex(const char* username) {
for (int i = 0; i < userCount; i++) {
if (strcmp(userUsernames[i], username) == 0) {
return i;
}
}
return -1;
}
bool modifyUserRole(const char* username, int newRole) {
int index = findUserIndex(username);
if (index != -1) {
userRoles[index] = newRole;
saveUsers(); // Save changes
cout << "User role updated successfully!\n";
return true;
}
cout << "User not found!\n";
return false;
}
void initializeDefaultAdmin() {
// Ensure a default admin account exists
bool adminExists = false;
for (int i = 0; i < userCount; i++) {
if (userRoles[i] == ROLE_ADMIN && strcmp(userUsernames[i], "admin") == 0) {
adminExists = true;
break;
}
}
if (!adminExists) {
addUser("admin", "admin", ROLE_ADMIN);
}
}
bool removeUser(const char* username) {
int index = findUserIndex(username);
if (index != -1) {
// Remove by shifting array elements
for (int j = index; j < userCount - 1; j++) {
strcpy(userUsernames[j], userUsernames[j + 1]);
strcpy(userPasswords[j], userPasswords[j + 1]);
userRoles[j] = userRoles[j + 1];
userActiveStatus[j] = userActiveStatus[j + 1];
}
userCount--;
saveUsers(); // Save changes
cout << "User removed successfully!\n";
return true;
}
cout << "User not found!\n";
return false;
}
void removeUser() {
char username[50];
cout << "Enter username to remove: ";
cin >> username;
// Call existing removeUser function
removeUser(username);
}
void displayUsers() {
cout << "\n====================================" << endl;
cout << " User Accounts " << endl;
cout << "====================================" << endl;
cout << "Username Role Status" << endl;
cout << "------------------------------------" << endl;
for (int i = 0; i < userCount; i++) {
// Directly use hardcoded role strings
const char* roleString;
switch(userRoles[i]) {
case ROLE_ADMIN: roleString = "Admin"; break;
case ROLE_EMPLOYEE: roleString = "Employee"; break;
case ROLE_CUSTOMER: roleString = "Customer"; break;
default: roleString = "Unknown"; break;
}
cout << left << setw(15) << userUsernames[i]
<< setw(15) << roleString
<< (userActiveStatus[i] ? "Active" : "Inactive")
<< endl;
}
cout << "------------------------------------" << endl;
}
void generateOTP(char* otp) {
// Seed the random number generator
srand(time(nullptr));
for (int i = 0; i < OTP_LENGTH; i++) {
// Generate random digit between 0-9
otp[i] = '0' + (rand() % 10);
}
otp[OTP_LENGTH] = '\0'; // Null-terminate the string
}
// Function to save OTP to a file
void saveOTP(const char* username, const char* otp) {
ofstream otpFile(OTP_FILE);
if (otpFile.is_open()) {
otpFile << username << '\n' << otp << '\n';
otpFile.close();
} else {
cout << "Error: Unable to save OTP.\n";
}
}
void logActivity(const char* username, const char* action) {
if (activityLogCount >= MAX_ACTIVITY_LOGS) {
// Rotate logs if maximum is reached
for (int i = 0; i < MAX_ACTIVITY_LOGS - 1; i++) {
strcpy(activityLogUsernames[i], activityLogUsernames[i+1]);
strcpy(activityLogActions[i], activityLogActions[i+1]);
activityLogTimestamps[i] = activityLogTimestamps[i+1];
}
activityLogCount--;
}
// Add new log entry
strncpy(activityLogUsernames[activityLogCount], username, 49);
strncpy(activityLogActions[activityLogCount], action, 99);
activityLogTimestamps[activityLogCount] = time(nullptr);
activityLogCount++;
// Save to file
saveActivityLogs();
}
void saveActivityLogs() {
ofstream file(ACTIVITY_LOG_FILE);
if (!file.is_open()) {
cout << "Error: Unable to save activity logs!\n";
return;
}
file << activityLogCount << '\n'; // Save number of logs
for (int i = 0; i < activityLogCount; i++) {
file << activityLogUsernames[i] << ' '
<< activityLogActions[i] << ' '
<< activityLogTimestamps[i] << '\n';
}
file.close();
}
void loadActivityLogs() {
ifstream file(ACTIVITY_LOG_FILE);
if (!file.is_open()) {
cout << "No existing activity logs found.\n";
return;
}
file >> activityLogCount;
for (int i = 0; i < activityLogCount; i++) {
file >> activityLogUsernames[i]
>> activityLogActions[i]
>> activityLogTimestamps[i];
}
file.close();
}
void timeNow(char* buffer, int bufferSize) {
time_t currentTime = time(0);
strftime(buffer, bufferSize, "%Y-%m-%d %H:%M:%S", localtime(¤tTime));
}
void displayActivityLogs() {
cout << "\n====================================" << endl;
cout << " Activity Logs " << endl;
cout << "====================================" << endl;
for (int i = 0; i < activityLogCount; i++) {
// Convert timestamp to readable format
char timeStr[100];
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", localtime(&activityLogTimestamps[i]));
// Display log entry
cout << "User: " << activityLogUsernames[i]
<< ", Action: " << activityLogActions[i]
<< ", Time: " << timeStr << endl;
}
// Display live time using the timeNow function
char liveTimeStr[100];
timeNow(liveTimeStr, sizeof(liveTimeStr));
cout << "\nLive Time: " << liveTimeStr << endl;
}
void monitorSecurityThreats() {
int failedLogins = 0;
// Count recent failed login attempts
for (int i = 0; i < activityLogCount; i++) {
if (strstr(activityLogActions[i], "Failed Login") != nullptr) {
failedLogins++;
}
}
// Alert for multiple failed login attempts
if (failedLogins > 5) {
cout << "\n!!! SECURITY ALERT !!!" << endl;
cout << "Multiple failed login attempts detected!" << endl;
cout << "Total failed attempts: " << failedLogins << endl;
cout << "Recommend reviewing security settings." << endl;
}
}
// Function to verify OTP
bool verifyOTP(const char* enteredOTP) {
ifstream otpFile(OTP_FILE);
char savedUsername[50], savedOTP[OTP_LENGTH + 1];
if (otpFile.is_open()) {
otpFile.getline(savedUsername, sizeof(savedUsername));
otpFile.getline(savedOTP, sizeof(savedOTP));
otpFile.close();
// Compare the entered OTP with saved OTP
return strcmp(enteredOTP, savedOTP) == 0;
}
return false;
}
void saveUsers() {
ofstream file("users.txt");
if (!file.is_open()) {
cout << "Error: Unable to save users!\n";
return;
}
for (int i = 0; i < userCount; i++) {
file << userUsernames[i] << ' '
<< userPasswords[i] << ' '
<< userRoles[i] << ' '
<< userActiveStatus[i] << '\n';
}
file.close();
}
// Load Users from File
void loadUsers() {
ifstream file("users.txt");
if (!file.is_open()) {
cout << "No existing user accounts found.\n";
initializeDefaultAdmin();
return;
}
userCount = 0;
while (file >> userUsernames[userCount]
>> userPasswords[userCount]
>> userRoles[userCount]
>> userActiveStatus[userCount]) {
userCount++;
}
file.close();
// Ensure at least one admin exists
initializeDefaultAdmin();
}
// User Management Menu for Admins
void userManagementMenu() {
char choice;
while (true) {
cout << "\n====================================" << endl;
cout << " User Management Menu " << endl;
cout << "====================================" << endl;
cout << "1. Add User" << endl;
cout << "2. Remove User" << endl;
cout << "3. Modify User Role" << endl;
cout << "4. Display Users" << endl;
cout << "5. Configure 2FA Settings" << endl; // New option
cout << "6. Return to Admin Menu" << endl;
cout << "------------------------------------" << endl;
cout << "Enter your choice: ";
cin >> choice;
char username[50], password[50];
int role;
switch (choice) {
case '1':
cout << "Redirecting to Add User...\n";
addUser();
break;
case '2':
cout << "Removing User...\n";
removeUser();
break;
case '3':
cout << "Modifying User Role...\n";
//userRoles();
break;
case '4':
cout << "Displaying Users...\n";
displayUsers();
break;
case '5': {
cout << "2FA Configuration:\n";
cout << "1. Enable 2FA for Admin\n";
cout << "2. Disable 2FA for Admin\n";
cout << "Enter choice: ";
char twoFAChoice;
cin >> twoFAChoice;
// Placeholder for 2FA configuration logic
switch (twoFAChoice) {
case '1':
cout << "2FA enabled for admin accounts.\n";
break;
case '2':
cout << "2FA disabled for admin accounts.\n";
break;
default:
cout << "Invalid choice.\n";
}
break;
}
case '6':
cout << "Logging out. Goodbye!\n";
return;
default:
cout << "Invalid choice. Please try again.\n";
}
}
}
void GenerateReports() {
char choice;
cout << "\n====================================" << endl;
cout << " Admin Reports " << endl;
cout << "====================================" << endl;
cout << "1. Low Stock Products (Stock < 5)" << endl;
cout << "2. High Selling Products (Sold > 0)" << endl;
cout << "3. Products by Price Range" << endl;
cout << "4. Sales Reports" << endl;
cout << "------------------------------------" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case '1': { // Low Stock Products
cout << "\n====================================" << endl;
cout << " Low Stock Products Report " << endl;
cout << "====================================" << endl;
bool found = false;
for (int i = 0; i < catalog_Size; i++) {
if (productstockQuantities[i] < 5) {
cout << "ID: " << ProductIDs[i] << "\n"
<< "Name: " << productnames[i] << "\n"
<< "Stock: " << productstockQuantities[i] << "\n"
<< "------------------------------------" << endl;
found = true;
}
}
if (!found) {
cout << "No products with low stock.\n";
}
break;
}
case '2': { // High Selling Products
cout << "\n====================================" << endl;
cout << " High Selling Products Report " << endl;
cout << "====================================" << endl;
bool found = false;
for (int i = 0; i < catalog_Size; i++) {
if (productsoldQuantities[i] > 0) {
cout << "ID: " << ProductIDs[i] << "\n"
<< "Name: " << productnames[i] << "\n"
<< "Sold: " << productsoldQuantities[i] << "\n"
<< "------------------------------------" << endl;
found = true;
}
}
if (!found) {
cout << "No high-selling products yet.\n";
}
break;
}
case '3': { // Products by Price Range
double minPrice, maxPrice;
cout << "\n====================================" << endl;
cout << " Products by Price Range " << endl;
cout << "====================================" << endl;
cout << "Enter minimum price: ";
cin >> minPrice;
cout << "Enter maximum price: ";
cin >> maxPrice;
cout << "\nProducts in Price Range [" << minPrice << ", " << maxPrice << "]:\n";
cout << "------------------------------------" << endl;
bool found = false;
for (int i = 0; i < catalog_Size; i++) {
if (productPrices[i] >= minPrice && productPrices[i] <= maxPrice) {
cout << "ID: " << ProductIDs[i] << "\n"
<< "Name: " << productnames[i] << "\n"
<< "Price: $" << productPrices[i] << "\n"
<< "Stock: " << productstockQuantities[i] << "\n"
<< "------------------------------------" << endl;
found = true;
}
}
if (!found) {
cout << "No products in the given price range.\n";
}
break;
}
case '4':{
generateSalesReports();
}
default:
cout << "\nInvalid choice. Returning to the menu...\n";
break;
}
}
void Loadcatalog() {
ifstream file("catalog.txt");
if (!file.is_open()) {
cout << "No existing catalog found. Starting fresh.\n";
return;
}
catalog_Size = 0; // Reset catalog size
while (file >> ProductIDs[catalog_Size]
>> productPrices[catalog_Size]
>> productstockQuantities[catalog_Size]
>> productsoldQuantities[catalog_Size]) {
file.ignore(); // Ignore the newline after the integers
file.getline(productnames[catalog_Size], 50);
file.getline(productCategories[catalog_Size], 50);
catalog_Size++;
}
file.close();
cout << "Catalog loaded successfully! " << catalog_Size << " products available.\n";
}
void cleanupAnnouncements() {
for (int i = 0; i < announcementCount; i++) {
delete[] announcementTitles[i];
delete[] announcementMessages[i];
}
}
bool addAnnouncement(const char* title, const char* message, int targetRole) {
if (announcementCount >= MAX_ANNOUNCEMENTS) {
cout << "Maximum number of announcements reached!\n";
return false;
}
// Validate input lengths
if (strlen(title) >= MAX_TITLE_LENGTH || strlen(message) >= MAX_ANNOUNCEMENT_LENGTH) {
cout << "Title or message too long!\n";
return false;
}
// Dynamically allocate memory for title and message
announcementTitles[announcementCount] = new char[strlen(title) + 1];
strcpy(announcementTitles[announcementCount], title);
announcementMessages[announcementCount] = new char[strlen(message) + 1];
strcpy(announcementMessages[announcementCount], message);
// Set other announcement details
announcementTimestamps[announcementCount] = time(nullptr);
announcementTargetRoles[announcementCount] = targetRole;
announcementCount++;
saveAnnouncements();
return true;
}
void loadAnnouncements() {
// Clean up existing announcements first
cleanupAnnouncements();
announcementCount = 0;
ifstream file("announcements.txt");
if (!file.is_open()) {
cout << "No existing announcements found.\n";
return;
}
file >> announcementCount;
file.ignore(); // Consume newline
for (int i = 0; i < announcementCount; i++) {
char tempTitle[MAX_TITLE_LENGTH];
char tempMessage[MAX_ANNOUNCEMENT_LENGTH];
file.getline(tempTitle, MAX_TITLE_LENGTH);
file.getline(tempMessage, MAX_ANNOUNCEMENT_LENGTH);
// Dynamically allocate memory
announcementTitles[i] = new char[strlen(tempTitle) + 1];
strcpy(announcementTitles[i], tempTitle);
announcementMessages[i] = new char[strlen(tempMessage) + 1];
strcpy(announcementMessages[i], tempMessage);
file >> announcementTimestamps[i];
file >> announcementTargetRoles[i];
file.ignore(); // Consume newline
}
file.close();
}
void displayAnnouncements(int userRole) {
cout << "\n====================================" << endl;
cout << " Announcements " << endl;
cout << "====================================" << endl;
bool found = false;
for (int i = 0; i < announcementCount; i++) {
// Check if announcement is for this role or for all roles
if (announcementTargetRoles[i] == userRole ||
announcementTargetRoles[i] == 3) {
// Convert timestamp to readable format
char timeStr[100];
time_t timestamp = announcementTimestamps[i];
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S",
localtime(×tamp));
cout << "Title: " << announcementTitles[i] << endl;
cout << "Message: " << announcementMessages[i] << endl;
cout << "Sent: " << timeStr << endl;
cout << "------------------------------------" << endl;
found = true;
}
}
if (!found) {
cout << "No announcements available for your role.\n";
}
}
void addAnnouncementMenu() {
char title[MAX_TITLE_LENGTH];
char message[MAX_ANNOUNCEMENT_LENGTH];
int targetRole;
cout << "\n====================================" << endl;
cout << " Create Announcement " << endl;
cout << "====================================" << endl;
cout << "Enter announcement title: ";
cin.ignore();
cin.getline(title, MAX_TITLE_LENGTH);
cout << "Enter announcement message: ";
cin.getline(message, MAX_ANNOUNCEMENT_LENGTH);
cout << "Select target role:\n";
cout << "0. Admin\n1. Employee\n2. Customer\n3. All Roles\n";
cout << "Enter role number: ";