forked from Praneeth-2602/Warehouse-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1112 lines (991 loc) · 31.4 KB
/
main.cpp
File metadata and controls
1112 lines (991 loc) · 31.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 <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
// Function to trim whitespace
string trim(const string &str)
{
size_t first = str.find_first_not_of(' ');
if (string::npos == first)
{
return str;
}
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last - first + 1));
}
// Hash function for password
string hashPassword(const string &password)
{
unsigned long hash = 0;
for (char c : password)
{
hash = (hash * 31) + c;
}
stringstream ss;
ss << hex << hash; // Convert the hash to a hexadecimal string
return ss.str();
}
// Product Class
class Product
{
private:
string productID;
string name;
int quantity;
double price;
public:
Product(string id, string name, int qty, double price) : productID(id), name(name), quantity(qty), price(price)
{
}
string getProductID() const
{
return productID;
}
string getName() const
{
return name;
}
int getQuantity() const
{
return quantity;
}
double getPrice() const
{
return price;
}
void updateQuantity(int qty)
{
quantity = qty;
}
void updatePrice(double newPrice)
{
price = newPrice;
}
void updateName(string newName)
{
name = newName;
}
void displayProduct() const
{
cout << "ID: " << productID << ", Name: " << name << ", Quantity: " << quantity << ", Price: $" << price
<< endl;
}
string toFileFormat() const
{
return productID + "," + name + "," + to_string(quantity) + "," + to_string(price);
}
static Product fromFileFormat(const string &line)
{
size_t pos = 0;
vector<string> tokens;
string modifiableLine = line;
while ((pos = modifiableLine.find(',')) != string::npos)
{
tokens.push_back(modifiableLine.substr(0, pos));
modifiableLine.erase(0, pos + 1);
}
tokens.push_back(modifiableLine);
return Product(tokens[0], tokens[1], stoi(tokens[2]), stod(tokens[3]));
}
};
// Order Class
class Order
{
private:
string orderID; // Unique identifier for the order
vector<string> orderedProductNames; // List of product names in the order
vector<int> quantities; // List of quantities for each product
time_t orderDate; // Date of the order
static int orderCounter; // Counter for order IDs
public:
Order(string id, time_t date) : orderID(id), orderDate(date)
{
}
void addProduct(const string &productName, int quantity)
{
orderedProductNames.push_back(productName);
quantities.push_back(quantity);
}
vector<string> getOrderProductNames() const
{
return orderedProductNames;
}
void extractProductNamesFromOrders()
{
ifstream ordersFile("orders.txt");
if (!ordersFile.is_open())
{
cerr << "Unable to open orders.txt for reading." << endl;
return;
}
string line;
while (getline(ordersFile, line))
{
Order order = Order::fromFileFormat(line);
const auto &productNames = order.getOrderProductNames();
orderedProductNames.insert(orderedProductNames.end(), productNames.begin(), productNames.end());
}
ordersFile.close();
}
vector<int> getQuantities() const
{
return quantities;
}
string getOrderID() const
{
return orderID;
}
time_t getOrderDate() const
{
return orderDate;
}
string toFileFormat() const
{
string result = orderID + "," + to_string(orderDate);
for (size_t i = 0; i < orderedProductNames.size(); ++i)
{
result += "|" + orderedProductNames[i] + "," + to_string(quantities[i]);
}
return result;
}
void displayOrder(const vector<Product> &inventory) const
{
cout << "Order ID: " << orderID << "\n";
cout << "Order Date: " << ctime(&orderDate);
cout << "Products:\n";
for (size_t i = 0; i < orderedProductNames.size(); ++i)
{
auto it = find_if(inventory.begin(), inventory.end(),
[&](const Product &product)
{ return product.getName() == orderedProductNames[i]; });
if (it != inventory.end())
{
cout << " - " << orderedProductNames[i] << " (Quantity: " << quantities[i] << ", Price: $"
<< it->getPrice() << ")\n";
}
else
{
cout << " - " << orderedProductNames[i] << " (Quantity: " << quantities[i] << ", Price: N/A)\n";
}
}
}
static Order fromFileFormat(const string &line)
{
stringstream ss(line);
string orderID, productData;
string dateStr;
// Extract order ID and order date
getline(ss, orderID, ','); // Order ID
getline(ss, dateStr, '|'); // Order date (separated by '|')
time_t orderDate;
try
{
orderDate = static_cast<time_t>(stoll(dateStr)); // Convert date string to time_t
}
catch (const std::invalid_argument &e)
{
cerr << "Invalid date format in order file: " << dateStr << endl;
orderDate = time(0); // Fallback to current time
}
// Create an order instance
Order order(orderID, orderDate);
// Extract products and quantities
while (getline(ss, productData, '|')) // Read product entries separated by '|'
{
size_t pos = productData.find(','); // Find the comma separating product name and quantity
if (pos != string::npos)
{
string productName = productData.substr(0, pos);
int quantity = stoi(productData.substr(pos + 1)); // Parse quantity
order.addProduct(productName, quantity);
}
else
{
cerr << "Invalid product data format: " << productData << endl;
}
}
return order;
}
};
int Order::orderCounter = 1;
// Warehouse Class
class Warehouse
{
private:
vector<Product> inventory;
vector<Order> orders;
public:
void addProduct(const Product &product)
{
inventory.push_back(product);
}
void addOrder()
{
// Generate a new order ID
string orderID = "O" + to_string(orders.size() + 1);
time_t now = time(0);
Order newOrder(orderID, now);
string productName;
int quantity;
char addMore;
cout << "Adding a new order: " << orderID << "\n";
cout << "Order Date and Time: " << ctime(&now);
do
{
cout << "Enter Product Name to add to order: ";
cin.ignore(); // Clear the buffer
getline(cin, productName);
cout << "Enter Quantity: ";
cin >> quantity;
// Find the product in the inventory by name
auto it = find_if(inventory.begin(), inventory.end(),
[&](Product &product)
{ return product.getName() == productName; });
if (it != inventory.end())
{
if (it->getQuantity() >= quantity)
{
newOrder.addProduct(it->getName(), quantity); // Use product name
it->updateQuantity(it->getQuantity() - quantity);
cout << "Product \"" << productName << "\" found and added to the order successfully.\n";
}
else
{
cout << "Insufficient quantity in inventory.\n";
}
}
else
{
cout << "Product \"" << productName << "\" not found in inventory.\n";
}
cout << "Add more products to the order? (y/n): ";
cin >> addMore;
} while (addMore == 'y' || addMore == 'Y');
// Add the completed order to the order list
orders.push_back(newOrder);
// Generate the order details in the format O1,1731520409|ProductName,Quantity and save it to orders.txt
ofstream ordersFile("orders.txt", ios::app);
if (ordersFile.is_open())
{
stringstream orderDetails;
orderDetails << orderID << "," << newOrder.getOrderDate() << "|";
// Add product details in the format ProductName,Quantity (separated by commas)
const auto &productNames = newOrder.getOrderProductNames();
const auto &quantities = newOrder.getQuantities();
for (size_t i = 0; i < productNames.size(); ++i)
{
orderDetails << productNames[i] << "," << quantities[i] << ",";
}
string orderData = orderDetails.str();
orderData.pop_back(); // Remove the trailing comma
ordersFile << orderData << endl;
ordersFile.close();
// Display the structured invoice
cout << "\n===================== INVOICE =====================\n";
cout << "Order ID: " << orderID << "\n";
cout << "Date: " << ctime(&now);
cout << "---------------------------------------------------\n";
cout << "Product Name Quantity Price\n";
cout << "---------------------------------------------------\n";
double totalCost = 0.0;
for (size_t i = 0; i < newOrder.getOrderProductNames().size(); ++i)
{
auto it = find_if(inventory.begin(), inventory.end(), [&](const Product &product)
{ return product.getName() == newOrder.getOrderProductNames()[i]; });
if (it != inventory.end())
{
double itemCost = it->getPrice() * newOrder.getQuantities()[i];
totalCost += itemCost;
cout << left << setw(18) << it->getName() << setw(12) << newOrder.getQuantities()[i] << fixed
<< setprecision(2) << itemCost << "\n";
}
}
cout << "---------------------------------------------------\n";
cout << right << setw(44) << "Total Cost: " << fixed << setprecision(2) << totalCost << "\n";
cout << "===================================================\n";
}
else
{
cout << "Unable to open orders.txt for writing.\n";
}
cout << "Order " << orderID << " added successfully!\n";
system("pause"); // Pause after generating the invoice
}
void viewInventory() const
{
cout << "Inventory:" << endl;
for (const auto &product : inventory)
{
product.displayProduct();
}
system("pause"); // Pause after viewing inventory
}
void viewOrders() const
{
cout << "Orders:" << endl;
for (const auto &order : orders)
{
order.displayOrder(inventory);
}
system("pause"); // Pause after viewing orders
}
void searchProduct(const string &searchTerm)
{
cout << "Search Results for: " << searchTerm << endl;
bool found = false;
for (const auto &product : inventory)
{
if (product.getProductID() == searchTerm || product.getName() == searchTerm)
{
product.displayProduct();
found = true;
}
}
if (!found)
{
cout << "No products found matching: " << searchTerm << endl;
}
system("pause"); // Pause after search results
}
void deleteProduct(const string &id)
{
auto it = remove_if(inventory.begin(), inventory.end(),
[&](const Product &product)
{ return product.getProductID() == id; });
if (it != inventory.end())
{
inventory.erase(it, inventory.end());
cout << "Product deleted successfully!" << endl;
}
else
{
cout << "Product ID not found!" << endl;
}
system("pause"); // Pause after deleting a product
}
void updateProduct(const string &id)
{
auto it =
find_if(inventory.begin(), inventory.end(), [&](Product &product)
{ return product.getProductID() == id; });
if (it != inventory.end())
{
int choice;
cout << "1. Update Name\n";
cout << "2. Update Quantity\n";
cout << "3. Update Price\n";
cout << "Enter the attribute to update: ";
cin >> choice;
switch (choice)
{
case 1:
{
string newName;
cout << "Enter new name: ";
cin >> newName;
it->updateName(newName);
cout << "Product name updated successfully.\n";
break;
}
case 2:
{
int newQty;
cout << "Enter new quantity: ";
cin >> newQty;
it->updateQuantity(newQty);
cout << "Product quantity updated successfully.\n";
break;
}
case 3:
{
double newPrice;
cout << "Enter new price: ";
cin >> newPrice;
it->updatePrice(newPrice);
cout << "Product price updated successfully.\n";
break;
}
default:
cout << "Invalid choice.\n";
}
}
else
{
cout << "Product ID not found!\n";
}
system("pause"); // Pause after updating a product
}
void saveInventoryToFile(const string &filename) const
{
ofstream outFile(filename);
for (const auto &product : inventory)
{
outFile << product.toFileFormat() << endl;
}
outFile.close();
}
void loadInventoryFromFile(const string &filename)
{
ifstream inFile(filename);
string line;
while (getline(inFile, line))
{
inventory.push_back(Product::fromFileFormat(line));
}
inFile.close();
}
void saveOrdersToFile(const string &filename) const
{
ofstream outFile(filename);
for (const auto &order : orders)
{
outFile << order.toFileFormat() << endl;
}
outFile.close();
}
void loadOrdersFromFile(const string &filename)
{
ifstream inFile(filename);
string line;
while (getline(inFile, line))
{
orders.push_back(Order::fromFileFormat(line));
}
inFile.close();
}
};
class SalesReport
{
public:
virtual void generateSalesReport(const vector<Order> &orders) = 0; // Pure virtual function
protected:
vector<Order> filterOrders(const vector<Order> &orders, time_t startTime, time_t endTime)
{
vector<Order> filteredOrders;
for (const auto &order : orders)
{
time_t orderDate = order.getOrderDate();
if (orderDate >= startTime && orderDate <= endTime)
{
filteredOrders.push_back(order);
}
}
return filteredOrders;
}
unordered_map<string, int> aggregateSalesData(const vector<Order> &filteredOrders)
{
unordered_map<string, int> salesData;
for (const auto &order : filteredOrders)
{
const auto &productNames = order.getOrderProductNames();
const auto &quantities = order.getQuantities();
for (size_t i = 0; i < productNames.size(); ++i)
{
salesData[productNames[i]] += quantities[i];
}
}
return salesData;
}
void printBarChart(const unordered_map<string, int> &salesData)
{
int maxSales = 0;
for (const auto &data : salesData)
{
maxSales = max(maxSales, data.second);
}
cout << "\nSales Report Bar Chart:\n";
cout << "Product Name | Sales Quantity\n";
cout << "-------------------------------------\n";
for (const auto &data : salesData)
{
cout << setw(20) << left << data.first << " | ";
int barLength = static_cast<int>((data.second / static_cast<double>(maxSales)) * 50);
for (int i = 0; i < barLength; ++i)
{
cout << "*";
}
cout << " " << data.second << endl;
}
}
void printSalesSummary(const unordered_map<string, int> &salesData)
{
cout << "\nSales Summary:\n";
cout << "Product Name | Total Quantity Sold\n";
cout << "-----------------------------------------\n";
for (const auto &data : salesData)
{
cout << setw(20) << left << data.first << " | " << data.second << endl;
}
}
void printTopSellingProducts(const unordered_map<string, int> &salesData)
{
vector<pair<string, int>> sortedSalesData(salesData.begin(), salesData.end());
sort(sortedSalesData.begin(), sortedSalesData.end(), [](const pair<string, int> &a, const pair<string, int> &b)
{ return b.second < a.second; });
cout << "\nTop Selling Products:\n";
cout << "Product Name | Total Quantity Sold\n";
cout << "-----------------------------------------\n";
for (size_t i = 0; i < min(sortedSalesData.size(), size_t(5)); ++i)
{
cout << setw(20) << left << sortedSalesData[i].first << " | " << sortedSalesData[i].second << endl;
}
}
void printAverageSales(size_t totalOrders)
{
cout << "\nAverage Sales:\n";
cout << "Total Orders: " << totalOrders << endl;
cout << "Average Orders per Day: " << (totalOrders / 7.0) << endl;
}
};
class WeeklyReport : public SalesReport
{
public:
void generateSalesReport(const vector<Order> &orders) override
{
time_t now = time(0);
tm lastWeek = *localtime(&now);
lastWeek.tm_mday -= 7;
mktime(&lastWeek);
vector<Order> filteredOrders = filterOrders(orders, mktime(&lastWeek), now);
if (filteredOrders.empty())
{
cout << "No orders found for the last week.\n";
return;
}
unordered_map<string, int> salesData = aggregateSalesData(filteredOrders);
printBarChart(salesData);
printSalesSummary(salesData);
printTopSellingProducts(salesData);
printAverageSales(filteredOrders.size());
}
};
class MonthlyReport : public SalesReport
{
public:
void generateSalesReport(const vector<Order> &orders) override
{
time_t now = time(0);
tm lastMonth = *localtime(&now);
lastMonth.tm_mon -= 1;
mktime(&lastMonth);
vector<Order> filteredOrders = filterOrders(orders, mktime(&lastMonth), now);
if (filteredOrders.empty())
{
cout << "No orders found for the last month.\n";
return;
}
unordered_map<string, int> salesData = aggregateSalesData(filteredOrders);
printBarChart(salesData);
printSalesSummary(salesData);
printTopSellingProducts(salesData);
printAverageSales(filteredOrders.size());
}
};
class YearlyReport : public SalesReport
{
public:
void generateSalesReport(const vector<Order> &orders) override
{
time_t now = time(0);
tm lastYear = *localtime(&now);
lastYear.tm_year -= 1;
mktime(&lastYear);
vector<Order> filteredOrders = filterOrders(orders, mktime(&lastYear), now);
if (filteredOrders.empty())
{
cout << "No orders found for the last year.\n";
return;
}
unordered_map<string, int> salesData = aggregateSalesData(filteredOrders);
printBarChart(salesData);
printSalesSummary(salesData);
printTopSellingProducts(salesData);
printAverageSales(filteredOrders.size());
}
};
// Admin Registration
bool registerAdmin()
{
string username, password;
cout << "\tRegister Admin" << endl;
cout << "\tUsername: ";
cin.ignore();
getline(cin, username);
username = trim(username);
cout << "\tPassword: ";
getline(cin, password);
password = trim(password);
string hashedPassword = hashPassword(password);
ofstream outFile("admin_credentials.csv", ios::app);
if (!outFile.is_open())
{
cout << "\tError: Unable to open admin credentials file." << endl;
return false;
}
outFile << username << "," << hashedPassword << endl;
outFile.close();
cout << "\tAdmin registered successfully!" << endl;
system("pause"); // Pause after admin registration
return true;
}
// Admin Login
bool loginAdmin()
{
string username, password;
int failedAttempts = 0;
const int MAX_FAILED_ATTEMPTS = 3;
time_t lockTime = 0;
const int LOCK_TIME_MINUTES = 15;
cout << "\tLogin Admin" << endl;
cout << "\tUsername: ";
cin.ignore();
getline(cin, username);
username = trim(username);
cout << "\tPassword: ";
getline(cin, password);
password = trim(password);
string hashedPassword = hashPassword(password);
ifstream inFile("admin_credentials.csv");
if (!inFile.is_open())
{
cout << "\tError: Unable to open admin credentials file." << endl;
return false;
}
string line, storedUsername, storedPassword;
while (getline(inFile, line))
{
stringstream ss(line);
getline(ss, storedUsername, ',');
getline(ss, storedPassword, ',');
if (storedUsername == username && storedPassword == hashedPassword)
{
cout << "\tAdmin logged in successfully!" << endl;
inFile.close();
return true;
}
}
inFile.close();
if (failedAttempts < MAX_FAILED_ATTEMPTS)
{
failedAttempts++;
cout << "\tInvalid username or password. You have " << MAX_FAILED_ATTEMPTS - failedAttempts << " attempts left."
<< endl;
}
else
{
time_t currentTime = time(0);
if (currentTime < lockTime)
{
cout << "\tAccount locked. Please wait " << (LOCK_TIME_MINUTES - (difftime(currentTime, lockTime) / 60))
<< " minutes before trying again." << endl;
}
else
{
lockTime = currentTime + (LOCK_TIME_MINUTES * 60);
cout << "\tAccount locked for " << LOCK_TIME_MINUTES << " minutes due to too many failed login attempts."
<< endl;
}
}
return false;
}
// Customer Registration
bool registerCustomer()
{
string username, password;
cout << "\tRegister Customer" << endl;
cout << "\tUsername: ";
cin.ignore();
getline(cin, username);
username = trim(username);
cout << "\tPassword: ";
getline(cin, password);
password = trim(password);
string hashedPassword = hashPassword(password);
ofstream outFile("customer_credentials.csv", ios::app);
if (!outFile.is_open())
{
cout << "\tError: Unable to open customer credentials file." << endl;
return false;
}
outFile << username << "," << hashedPassword << endl;
outFile.close();
cout << "\tCustomer registered successfully!" << endl;
system("pause"); // Pause after customer registration
return true;
}
// Customer Login
bool loginCustomer()
{
string username, password;
int failedAttempts = 0;
const int MAX_FAILED_ATTEMPTS = 3;
time_t lockTime = 0;
const int LOCK_TIME_MINUTES = 15;
cout << "\tLogin Customer" << endl;
cout << "\tUsername: ";
cin.ignore();
getline(cin, username);
username = trim(username);
cout << "\tPassword: ";
getline(cin, password);
password = trim(password);
string hashedPassword = hashPassword(password);
ifstream inFile("customer_credentials.csv");
if (!inFile.is_open())
{
cout << "\tError: Unable to open customer credentials file." << endl;
return false;
}
string line, storedUsername, storedPassword;
while (getline(inFile, line))
{
stringstream ss(line);
getline(ss, storedUsername, ',');
getline(ss, storedPassword, ',');
// Check if the account is locked
if (failedAttempts >= MAX_FAILED_ATTEMPTS)
{
time_t currentTime = time(0);
if (currentTime < lockTime)
{
cout << "\tAccount locked. Please wait " << (LOCK_TIME_MINUTES - (difftime(currentTime, lockTime) / 60))
<< " minutes before trying again." << endl;
inFile.close();
return false;
}
else
{
// Reset failed attempts after lock time
failedAttempts = 0;
}
}
// Validate username and password
if (storedUsername == username && storedPassword == hashedPassword)
{
cout << "\tCustomer logged in successfully!" << endl;
inFile.close();
return true;
}
}
inFile.close();
failedAttempts++;
cout << "\tInvalid username or password. You have " << (MAX_FAILED_ATTEMPTS - failedAttempts) << " attempts left."
<< endl;
// Lock the account if max attempts are reached
if (failedAttempts >= MAX_FAILED_ATTEMPTS)
{
lockTime = time(0) + (LOCK_TIME_MINUTES * 60); // Set the lock time
cout << "\tAccount locked for " << LOCK_TIME_MINUTES << " minutes due to too many failed login attempts."
<< endl;
}
return false;
}
// Main Menu Functions
void displayHeader(const string &title)
{
system("cls");
cout << "\n\t************************************************************\n";
cout << "\t* *\n";
cout << "\t* " << title << " *\n";
cout << "\t* *\n";
cout << "\t************************************************************\n\n";
}
void salesReportMenu()
{
int choice;
do
{
displayHeader("Sales Report Menu");
cout << "1. Last Week\n";
cout << "2. Last Month\n";
cout << "3. Last Year\n";
cout << "4. Back to Admin Menu\n";
cout << "Enter your choice: ";
cin >> choice;
vector<Order> orders;
ifstream ordersFile("orders.txt");
string line;
while (getline(ordersFile, line))
{
orders.push_back(Order::fromFileFormat(line));
}
ordersFile.close();
WeeklyReport weeklyReport;
MonthlyReport monthlyReport;
YearlyReport yearlyReport;
switch (choice)
{
case 1:
weeklyReport.generateSalesReport(orders);
break;
case 2:
monthlyReport.generateSalesReport(orders);
break;
case 3:
yearlyReport.generateSalesReport(orders);
break;
case 4:
cout << "Returning to Admin Menu..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
system("pause"); // Pause after sales report menu
} while (choice != 4);
}
// Admin menu
void adminMenu(Warehouse &warehouse)
{
int choice;
do
{
displayHeader("Admin Menu");
cout << "1. Add Product\n";
cout << "2. Update Product\n";
cout << "3. Remove Product\n";
cout << "4. View Inventory\n";
cout << "5. View Orders\n";
cout << "6. Generate Sales Report\n"; // New option for sales report
cout << "7. Logout\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
{
string id, name;
int qty;
double price;
cout << "Enter Product ID: ";
cin >> id;
cout << "Enter Name: ";
cin >> name;
cout << "Enter Quantity: ";
cin >> qty;
cout << "Enter Price: ";
cin >> price;
warehouse.addProduct(Product(id, name, qty, price));
cout << "Product added successfully!" << endl;
system("pause"); // Pause after adding a product
break;
}
case 2:
{
string id;
cout << "Enter Product ID to Update: ";
cin >> id;
warehouse.updateProduct(id);
break;
}
case 3:
{