-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRetailApp.h
More file actions
1197 lines (986 loc) · 40.1 KB
/
RetailApp.h
File metadata and controls
1197 lines (986 loc) · 40.1 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
#ifndef RetailApp_H
#define RetailApp_H
#include <iostream>
#include <string>
#include <vector>
#include <tuple>
#include <map>
// Include managers for managing different tables
#include "CustomerManager.h"
#include "SupplierManager.h"
#include "ProductManager.h"
#include "CartItemManager.h"
#include "TransactionManager.h"
#include "OrderItemManager.h"
// Include object representations of rows in our database
#include "Customer.h"
#include "Supplier.h"
#include "Product.h"
#include "CartItem.h"
#include "Transaction.h"
// Include utility function for getting numeric input
#include "utils.h"
class RetailApp {
private:
CustomerManager& customerManager;
SupplierManager& supplierManager;
ProductManager& productManager;
CartItemManager& cartItemManager;
TransactionManager& transactionManager;
OrderItemManager& orderItemManager;
/*
- Customer ID of the currently selected customer. We'll use this to
know which shopping cart we are managing.
- currentCustomer: Customer object that's displayed on the cart item menu. We'll
use a Customer object since, in the cart items screen, we'll need to be able
to change the 'points' attribute so it's value is in-sync with the database. Right now this is only used to display the current customer in the cart items menu.
*/
int currentCustomerID = 0;
Customer currentCustomer;
public:
RetailApp(
CustomerManager& customerManager,
SupplierManager& supplierManager,
ProductManager& productManager,
CartItemManager& cartItemManager,
TransactionManager& transactionManager,
OrderItemManager& orderItemManager
) :
customerManager(customerManager),
supplierManager(supplierManager),
productManager(productManager),
cartItemManager(cartItemManager),
transactionManager(transactionManager),
orderItemManager(orderItemManager) {}
// ********** Functions for customer related operations **********
// Displays and starts the customer menu
void handleCustomerMenu() {
int choice;
do {
try {
// Display main menu and prompt input
std::cout << "Customer Menu: " << std::endl;
std::cout << "1. Create Customer" << std::endl;
std::cout << "2. Update Customer" << std::endl;
std::cout << "3. Delete Customer" << std::endl;
std::cout << "4. Get Customer By ID" << std::endl;
std::cout << "5. Display all customers" << std::endl;
std::cout << "6. Select a current customer" << std::endl;
std::cout << "7. Exit Customer Menu" << std::endl;
std::cout << "Please enter a number to continue: ";
std::cin >> choice;
if (std::cin.fail()) {
std::cout << "Invalid input. Please enter a number!" << std::endl;
std::cin.clear(); // Clear the error flag
std::cin.ignore(64, '\n'); // Clear the input buffer
continue; // Restart the loop
}
switch (choice) {
case 1:
handleCreateCustomer();
break;
case 2:
handleUpdateCustomer();
break;
case 3:
handleDeleteCustomer();
break;
case 4:
handleGetCustomerByID();
break;
case 5:
displayAllCustomers();
break;
case 6:
handleSelectCustomer();
break;
case 7:
std::cout << "Exiting Customer Menu..." << std::endl;
break;
default:
std::cout << "Customer Menu: Invalid choice. Please enter a number between 1 and 6." << std::endl;
}
}
catch (const std::exception& ex) {
std::cerr << "Customer Menu Error: " << ex.what() << std::endl;
}
} while (choice != 7);
}
// Prompts input for creating a customer
void handleCreateCustomer() {
std::string fname, lname, email;
int points = 0;
// Ignore any new line characters in the buffer so that getline works.
std::cin.ignore();
std::cout << "Enter customer's first name: ";
std::getline(std::cin, fname);
std::cout << "Enter customer's last name: ";
std::getline(std::cin, lname);
std::cout << "Enter customer's email: ";
std::getline(std::cin, email);
// Do database operation to create customer
Customer customer = customerManager.createCustomer(fname, lname, email, points);
std::cout << "Success, new customer: " << customer << std::endl;
}
/*
- Prompt input for updating a customer.
NOTE: if customer with customer_id isn't found, then an error is thrown, and then caught
in the try/catch defined in handleCustomerMenu, effectively taking the user back to the
customer menu if the ID didn't correlate to a customer in the database.
*/
void handleUpdateCustomer() {
// Fetch all customers
std::vector<Customer> customers = customerManager.getAllCustomers();
if (customers.size() == 0) {
std::cout << "No customers available to update!" << std::endl;
return;
}
// Prompt user to pick customer to update from a paginated menu
Customer customer = selectPaginatedItems<Customer>(customers, 5, "Customer Menu List", "Enter list number for customer we're updating");
// IF they didn't pick a customer, stop function early
if (!customer) {
return;
}
// extract customer_id
const int customer_id = customer.getCustomerID();
// Prompt input for the attribute they want to update the customer on
int choice;
do {
std::cout << "Selected Customer: " << customer << std::endl;
std::cout << "1. First Name" << std::endl;
std::cout << "2. Last Name" << std::endl;
std::cout << "3. Email" << std::endl;
std::cout << "Pick an attribute to update: ";
std::cin >> choice;
if (std::cin.fail()) {
std::cout << "Invalid choice, please enter a number!" << std::endl;
std::cin.clear(); // Clear the error flag
std::cin.ignore(64, '\n'); // Clear input buffer up to 64 characters or until newline is encountered
continue; // Restart the loop
}
} while (choice < 1 || choice > 3);
// Prompt input for the value whether it be first name, last name, or email.
std::string value;
std::cin.ignore(); // clear new line so our getlines work in our switch statement.
/*
- Depending on their choice:
1. Prompt input for value
2. Then do database operation to update that customer with the new value
*/
switch (choice) {
case 1:
std::cout << "Enter new first name: ";
std::getline(std::cin, value);
customerManager.updateFirstName(customer_id, value);
break;
case 2:
std::cout << "Enter new last name: ";
std::getline(std::cin, value);
customerManager.updateLastName(customer_id, value);
break;
case 3:
std::cout << "Enter new email: ";
std::getline(std::cin, value);
customerManager.updateEmail(customer_id, value);
break;
}
std::cout << "Successfully, updated customer!" << std::endl;
}
// Handles prompting input to delete a customer
void handleDeleteCustomer() {
// Fetch all customers
std::vector<Customer> customers = customerManager.getAllCustomers();
if (customers.size() == 0) {
std::cout << "No customers available to delete!" << std::endl;
return;
}
// Prompt user to pick customer to delete from a paginated menu
Customer customer = selectPaginatedItems<Customer>(customers, 5, "Customer Menu List", "Enter list number for customer we're removing");
// IF they didn't pick a customer, stop function early
if (!customer) {
return;
}
// Extra customer_id from
const int customer_id = customer.getCustomerID();
/*
- If the customer we are deleting is also the currently selected customer
- reset currentCustomerID back to 0 to indicate no customer is currently selected.
*/
if (customer.getCustomerID() == currentCustomerID) {
currentCustomerID = 0;
}
// Delete all cart items that reference the customer that's going to be deleted
cartItemManager.deleteByCustomerID(customer_id);
// Nullify 'customer_id' column in transactions table for all rows that reference the deleted customer_id
transactionManager.nullifyCustomerID(customer_id);
// Delete customer, and on success display that the customer was successfully deleted.
customerManager.deleteCustomer(customer_id);
std::cout << "Customer Deleted: " << customer << std::endl;
}
// Handles prompting input for customer_id and searching for said customer in database
void handleGetCustomerByID() {
// Prompt input on the ID of the customer that we will search for; ensure it's a valid integer.
int customer_id = getValidNumericInput<int>("Enter a ID customer you want to see: ");
// Fetch the customer and print them
Customer customer = customerManager.getCustomerByID(customer_id);
std::cout << "Found Customer: " << customer << std::endl;
}
// Handles displaying all rows in customers table (if any)
void displayAllCustomers() {
std::vector<Customer> customers = customerManager.getAllCustomers();
// If no customers, display message and stop function execution early.
if (customers.size() == 0) {
std::cout << "No customers to display!" << std::endl;
return;
}
// Call function to display paginated list of customers
navigatePaginatedItems<Customer>(customers, 5, "Customer Menu List");
}
// Updates the current customer we're managing
void handleSelectCustomer() {
std::vector<Customer> customers = customerManager.getAllCustomers();
if (customers.size() == 0) {
std::cout << "No customers available to select!" << std::endl;
return;
}
// Prompt user to pick customer to select from a paginated menu
Customer customer = selectPaginatedItems<Customer>(customers, 5, "Customer Menu List", "Enter list number for customer we're selecting");
// IF they didn't pick a customer, stop function early
if (!customer) {
return;
}
// Extra customer_id from
const int customer_id = customer.getCustomerID();
// Update currentCustomerID
currentCustomerID = customer_id;
// Print out message displaying the currently selected customer
std::cout << "Current customer: " << customer << std::endl;
}
// ********** Functions for supplier related operations **********
// Displays and starts the supplier menu
void handleSupplierMenu() {
int choice;
do {
try {
// Display main menu and prompt input
std::cout << "Supplier Menu: " << std::endl;
std::cout << "1. Create Supplier" << std::endl;
std::cout << "2. Update Supplier" << std::endl;
std::cout << "3. Delete Supplier" << std::endl;
std::cout << "4. Get Supplier By ID" << std::endl;
std::cout << "5. Display all Suppliers" << std::endl;
std::cout << "6. Exit Supplier Menu" << std::endl;
std::cout << "Please enter a number to continue: ";
std::cin >> choice;
if (std::cin.fail()) {
std::cout << "Invalid input. Please enter a number!" << std::endl;
std::cin.clear(); // Clear the error flag
std::cin.ignore(64, '\n'); // Clear the input buffer
continue; // Restart the loop
}
switch (choice) {
case 1:
handleCreateSupplier();
break;
case 2:
handleUpdateSupplier();
break;
case 3:
handleDeleteSupplier();
break;
case 4:
handleGetSupplierByID();
break;
case 5:
displayAllSuppliers();
break;
case 6:
std::cout << "Exiting Supplier Menu..." << std::endl;
break;
default:
std::cout << "Supplier Menu: Invalid choice. Please enter a number between 1 and 6." << std::endl;
}
}
// Here you'll catch all of the errors thrown by the managers' methods
catch (const std::exception& ex) {
std::cerr << "Supplier Menu Error: " << ex.what() << std::endl;
}
} while (choice != 6);
}
// Handles prompting input for creating a supplier
void handleCreateSupplier() {
// Ignore to newline
std::cin.ignore();
// Prompt input for supplier information
std::string s_name, description, email, address;
std::cout << "Enter supplier name: ";
std::getline(std::cin, s_name);
std::cout << "Enter supplier description: ";
std::getline(std::cin, description);
std::cout << "Enter supplier email: ";
std::getline(std::cin, email);
std::cout << "Enter supplier's address: ";
std::getline(std::cin, address);
// Attempt to save supplier to the database
Supplier supplier = supplierManager.createSupplier(s_name, description, email, address);
std::cout << "Success, new supplier: " << supplier << std::endl;
}
// Handles prompting input for updating the attributes of a supplier
void handleUpdateSupplier() {
// Fetch all suppliers
std::vector<Supplier> suppliers = supplierManager.getAllSuppliers();
if (suppliers.size() == 0) {
std::cout << "No suppliers available to update!" << std::endl;
return;
}
// Prompt user to pick supplier to update from a paginated menu
Supplier supplier = selectPaginatedItems<Supplier>(suppliers, 5, "Supplier Menu List", "Enter list number for supplier we're updating");
// IF they didn't pick a supplier, stop function early
if (!supplier) {
return;
}
// extract supplier_id
const int supplier_id = supplier.getSupplierID();
// Prompt input for attribute to be updated
int choice;
do {
std::cout << "Selected Supplier: " << supplier << std::endl;
std::cout << "1. Supplier Name" << std::endl;
std::cout << "2. Description" << std::endl;
std::cout << "3. Email" << std::endl;
std::cout << "4. Address" << std::endl;
std::cout << "Pick an attribute to update: ";
std::cin >> choice;
if (std::cin.fail()) {
std::cout << "Invalid choice, please enter a number!" << std::endl;
std::cin.clear(); // Clear the error flag
std::cin.ignore(64, '\n'); // Clear input buffer up to 64 characters or until newline is encountered
continue; // Restart the loop
}
} while (choice < 1 || choice > 4);
// Prompt input for the value, whether it be the supplier name, description etc.
std::string value;
std::cin.ignore(); // clear new line so our getlines work in our switch statement.
/*
- Depending on their choice:
1. Prompt input for value
2. Then do database operation to update that customer with the new value
3. Use a setter on updatedCustomer to reflect new change. Then we can print updatedCustomer
out to the console to show the user their new changes.
*/
switch (choice) {
case 1:
std::cout << "Enter new supplier name: ";
std::getline(std::cin, value);
supplierManager.updateName(supplier_id, value);
break;
case 2:
std::cout << "Enter new description: ";
std::getline(std::cin, value);
supplierManager.updateDescription(supplier_id, value);
break;
case 3:
std::cout << "Enter new email: ";
std::getline(std::cin, value);
supplierManager.updateEmail(supplier_id, value);
break;
case 4:
std::cout << "Enter new address: ";
std::getline(std::cin, value);
supplierManager.updateAddress(supplier_id, value);
break;
}
std::cout << "Successfully, updated supplier!" << std::endl;
}
// Prompts input for deleting a supplier
void handleDeleteSupplier() {
// Fetch all suppliers
std::vector<Supplier> suppliers = supplierManager.getAllSuppliers();
if (suppliers.size() == 0) {
std::cout << "No suppliers available to delete!" << std::endl;
return;
}
// Prompt user to pick supplier to update from a paginated menu
Supplier supplier = selectPaginatedItems<Supplier>(suppliers, 5, "Supplier Menu List", "Enter list number for supplier we're deleting");
// IF they didn't pick a supplier, stop function early
if (!supplier) {
return;
}
// extract supplier_id
const int supplier_id = supplier.getSupplierID();
// Delete all cart items that reference a product, where the product has a supplier_id of the deleted supplier
cartItemManager.deleteBySupplierID(supplier_id);
// Nullify all order items that reference products that the supplier associated with
orderItemManager.nullifyProductIDBySupplierID(supplier_id);
// Delete all products associated with supplier
productManager.deleteBySupplierID(supplier_id);
// Then delete the supplier, which will also delete the supplier name
supplierManager.deleteSupplier(supplier_id);
std::cout << "Supplier Deleted: " << supplier << std::endl;
}
// Prompts input for supplier ID and then displaying that supplier
void handleGetSupplierByID() {
// Prompt input on the ID of the supplier that we will search for; ensure it's a valid integer.
int supplier_id = getValidNumericInput<int>("Enter the ID of the supplier you want to display: ");
// Fetch the customer and print them
Supplier supplier = supplierManager.getSupplierByID(supplier_id);
/*
- Print out the found supplier. We've customized the output stream for the Supplier class to exclude the description,
which could be lengthy. Here, we first display the supplier's details using the custom ostream operator, and then
print the description separately to ensure all information is visible to the user.
*/
std::cout << "Found Supplier: " << std::endl;
std::cout << supplier << std::endl;
std::cout << "Description: " << supplier.getDescription() << std::endl;
}
// Display all suppliers in the database
void displayAllSuppliers() {
// Get all suppliers from the database
std::vector<Supplier> suppliers = supplierManager.getAllSuppliers();
// Check to see if vector is empty; if so then stop execution early
if (suppliers.size() == 0) {
std::cout << "No suppliers to display!" << std::endl;
return;
}
navigatePaginatedItems<Supplier>(suppliers, 5, "Supplier Menu List");
}
// ********** Functions for Product related operations **********
// Handles displaying and managing the product menu
void handleProductMenu() {
int choice;
do {
try {
// Display main menu and prompt input
std::cout << "Product Menu: " << std::endl;
std::cout << "1. Create Product" << std::endl;
std::cout << "2. Update Product" << std::endl;
std::cout << "3. Delete Product" << std::endl;
std::cout << "4. Get Product By ID" << std::endl;
std::cout << "5. Display all Products" << std::endl;
std::cout << "6. Exit Product Menu" << std::endl;
std::cout << "Please enter a number to continue: ";
std::cin >> choice;
if (std::cin.fail()) {
std::cout << "Invalid input. Please enter a number!" << std::endl;
std::cin.clear(); // Clear the error flag
std::cin.ignore(64, '\n'); // Clear the input buffer
continue; // Restart the loop
}
switch (choice) {
case 1:
handleCreateProduct();
break;
case 2:
handleUpdateProduct();
break;
case 3:
handleDeleteProduct();
break;
case 4:
handleGetProductByID();
break;
case 5:
displayAllProducts();
break;
case 6:
std::cout << "Exiting Product Menu..." << std::endl;
break;
default:
std::cout << "Product Menu: Invalid choice. Please enter a number between 1 and 6." << std::endl;
}
}
// Here you'll catch all of the errors thrown by the managers' methods
catch (const std::exception& ex) {
std::cerr << "Product Menu Error: " << ex.what() << std::endl;
}
} while (choice != 6);
}
// Prompts input for creating a new product
void handleCreateProduct() {
// Ignore to newline so our getlines work
std::cin.ignore();
// Prompt input for product information
std::string p_name, description;
int supplier_id, qty;
float price;
std::cout << "Enter product name: ";
std::getline(std::cin, p_name);
std::cout << "Enter description: ";
std::getline(std::cin, description);
// Prompt input for supplier_id, qty in stock, and price of the product
supplier_id = getValidNumericInput<int>("Enter the ID of the supplier selling this product: ");
qty = getValidNumericInput<int>("Enter the qty in stock: ");
price = getValidNumericInput<float>("Enter price of the product: ");
/*
- The reason we validate the input out here is because we want to do input checks before using the
resources to query our database. And since your productManager doesn't have a way to check the validity of the supplier_id, we'll lift the logic up to the RetailApp.
*/
productManager.validateProductName(p_name);
productManager.validateDescription(description);
productManager.validatePrice(price);
productManager.validateQty(qty);
// Ensure supplier_id links to an actual supplier
const bool isValid = supplierManager.isValidSupplierID(supplier_id);
if (!isValid) {
throw std::runtime_error("Supplier with supplier_id(" + std::to_string(supplier_id) + ") wasn't found!");
}
// Data is good so create product and print it out
Product product = productManager.createProduct(supplier_id, p_name, description, price, qty);
std::cout << "Success, new product: " << product << std::endl;
}
// Prompts input for updating an existing product
void handleUpdateProduct() {
// Fetch all products
std::vector<Product> products = productManager.getAllProducts();
if (products.size() == 0) {
std::cout << "No products available to update!" << std::endl;
return;
}
// Prompt user to pick product to update from a paginated menu
Product product = selectPaginatedItems<Product>(products, 5, "Product Menu List", "Enter list number for product we're updating");
// IF they didn't pick a product, stop function early
if (!product) {
return;
}
// extract supplier_id
const int product_id = product.getProductID();
// Prompt input for attribute to be updated
int choice;
do {
std::cout << "Selected Product: " << product << std::endl;
std::cout << "1. Product Name" << std::endl;
std::cout << "2. Description" << std::endl;
std::cout << "3. Price" << std::endl;
std::cout << "4. Quantity in stock" << std::endl;
std::cout << "Pick an attribute to update: ";
std::cin >> choice;
if (std::cin.fail()) {
std::cout << "Invalid choice, please enter a number!" << std::endl;
std::cin.clear(); // Clear the error flag
std::cin.ignore(64, '\n'); // Clear input buffer up to 64 characters or until newline is encountered
continue; // Restart the loop
}
} while (choice < 1 || choice > 4);
// clear new line so our getlines work in some of the switch statement's branches .
std::cin.ignore();
std::string p_name;
std::string description;
float price;
int qty;
switch (choice) {
case 1:
std::cout << "Enter new product name: ";
std::getline(std::cin, p_name);
productManager.updateName(product_id, p_name);
break;
case 2:
std::cout << "Enter new description: ";
std::getline(std::cin, description);
productManager.updateDescription(product_id, description);
break;
case 3:
price = getValidNumericInput<float>("Enter new price: ");
productManager.updatePrice(product_id, price);
break;
case 4:
qty = getValidNumericInput<int>("Enter new qty in stock: ");
productManager.updateQuantity(product_id, qty);
break;
}
std::cout << "Successfully, updated product!" << std::endl;
}
// Prompts input for deleting an existing product
void handleDeleteProduct() {
// Fetch all products
std::vector<Product> products = productManager.getAllProducts();
if (products.size() == 0) {
std::cout << "No products available to delete!" << std::endl;
return;
}
// Prompt user to pick product to update from a paginated menu
Product product = selectPaginatedItems<Product>(products, 5, "Product Menu List", "Enter list number for product we're deleting");
// IF they didn't pick a product, stop function early
if (!product) {
return;
}
// extract supplier_id
const int product_id = product.getProductID();
// Delete all cart items that reference the product being deleted
cartItemManager.deleteByProductID(product_id);
// Nullify all order items that reference the product_id
orderItemManager.nullifyProductID(product_id);
// Then delete the product
productManager.deleteProduct(product_id);
std::cout << "Deleted Product: " << product << std::endl;
}
// Handles prompting input for product_id and displaying detailed product information
void handleGetProductByID() {
// Prompt input on the ID of the customer that we will search for; ensure it's a valid integer.
int product_id = getValidNumericInput<int>("Enter the ID of the product you want to display: ");
// Fetch the customer and print them
Product product = productManager.getProductByID(product_id);
std::cout << "Found Product: " << std::endl;
std::cout << product << std::endl;
std::cout << "Description: " << product.getDescription() << std::endl;
}
// Handles displaying all products in the database
void displayAllProducts() {
std::vector<Product> products = productManager.getAllProducts();
// Check to see if vector is empty; if so then stop execution early
if (products.size() == 0) {
std::cout << "No products to display!" << std::endl;
return;
}
// There are suppliers, so display them.
navigatePaginatedItems<Product>(products, 5, "Product Menu List");
}
// ********** Functions for Shopping-Cart related operations **********
/*
+ Handles displaying choices for the cart menu
NOTE: currentCustomerID has to be defined and reference a currently existing customer before proceeding any further into the cart menu. This is because all of the operations are going to be related to a given customer's shopping cart.
*/
void handleCartMenu() {
int choice;
// Verify that currentCustomerID is defined and references a customer in the database
if (!currentCustomerID) {
std::cout << "Please select a customer first, before managing cart items!" << std::endl;
return;
}
currentCustomer = customerManager.getCustomerByID(currentCustomerID);
do {
try {
// Display main menu and prompt input
std::cout << "Cart Menu: " << std::endl;
std::cout << "Current Customer: " << currentCustomer << std::endl;
std::cout << "1. Add product to cart" << std::endl;
std::cout << "2. Remove product from cart" << std::endl;
std::cout << "3. View all cart items" << std::endl;
std::cout << "4. Update cart item quantity" << std::endl;
std::cout << "5. Check out current cart" << std::endl;
std::cout << "6. Exit Cart Menu" << std::endl;
std::cout << "Please enter a number to continue: ";
std::cin >> choice;
if (std::cin.fail()) {
std::cout << "Invalid input. Please enter a number!" << std::endl;
std::cin.clear(); // Clear the error flag
std::cin.ignore(64, '\n'); // Clear the input buffer
continue; // Restart the loop
}
switch (choice) {
case 1:
handleAddToCart();
break;
case 2:
handleRemoveFromCart();
break;
case 3:
displayCustomerCart();
break;
case 4:
handleUpdateCartItem();
break;
case 5:
handleCheckout();
break;
case 6:
std::cout << "Exiting Cart Menu..." << std::endl;
break;
default:
std::cout << "Cart Menu: Invalid choice. Please enter a number between 1 and 6." << std::endl;
}
}
catch (const std::exception& ex) {
std::cerr << "Cart Menu Error: " << ex.what() << std::endl;
}
} while (choice != 6);
}
// Handles input for adding a new product to cart
void handleAddToCart() {
// Only fetch products are in stock and available;
// If there are no items available to be put in the cart, return early
std::vector<Product> products = productManager.getAvailableProducts();
if (products.size() == 0) {
std::cout << "No available items to add to cart!" << std::endl;
return;
}
// prompt user to potentially pick a product to add to cart
Product product = selectPaginatedItems<Product>(products, 5, "Product Menu", "Enter list number of product you're adding");
// If user didn't pick a product to be added to cart
if (!product) {
return;
}
/*
- Get the number in stock
- At this point we can determine that the product's quantity in stock is greater than 0 (at least 1).
As well, we'll set a rule that the maximum of a particular product you can add to cart will be 10.
However if the number in stock (productQty) is less than 10, then the maximum will be set to the
amount remaining in stock.
- For integer quantity within min and max
*/
int qty = handleInputCartQty(product);
/*
- Good values so attempt to create a new cart item. Now this will also check if the user already has
the product in their cart, and if so an error will be thrown.
NOTE: createCartItem() is different from other create functions in other managers as it doesn't
return anything. This is because if we really wanted to return a CartItem object, we'd have to
do another database query. Instead of doing that, we can construct the CartItem object here
in this function since we have enough information to do that.
*/
cartItemManager.createCartItem(currentCustomerID, product.getProductID(), qty);
CartItem cartItem(currentCustomerID, product.getProductID(), qty, product.getName(), product.getPrice());
std::cout << "Added to Cart: " << cartItem << std::endl;
}
// Handles input for removing a product from the cart
void handleRemoveFromCart() {
// Get a customer's cart items
std::vector<CartItem> cartItems = cartItemManager.getCustomerCartItems(currentCustomerID);
if (cartItems.size() == 0) {
std::cout << "Nothing to remove, since no items are in cart!" << std::endl;
return;
}
// prompt user to potentially pick a product to remove from cart
CartItem cartItem = selectPaginatedItems<CartItem>(cartItems, 5, "Cart Item Menu", "Enter list number for cart item you're removing");
// If user didn't pick a cartItem to be removed, then stop function execution early.
if (!cartItem) {
return;
}
// Cart item was picked for removal, so remove it from database; then print out the cartItem that the user removed.
cartItemManager.deleteCartItem(currentCustomerID, cartItem.getProductID());
std::cout << "Removed Cart Item!" << std::endl;
}
// Handles input for updating an item's quantity when in cart
void handleUpdateCartItem() {
// Get a customer's cart items
std::vector<CartItem> cartItems = cartItemManager.getCustomerCartItems(currentCustomerID);
if (cartItems.size() == 0) {
std::cout << "Nothing to update, since no items are in cart!" << std::endl;
return;
}
// prompt user to potentially pick a product to remove from cart
CartItem cartItem = selectPaginatedItems<CartItem>(cartItems, 5, "Cart Item Menu", "Enter list number for cart item you're updating");
// If user didn't pick a cartItem to be removed, then stop function execution early.
if (!cartItem) {
return;
}
/*
- Fetch product associated with cart item
- Check that the product is still in stock. If it not, this just means the user has something in their cart, it was still in stock when they added it to their cart, but now it's out of stock. Of course this will be addressed at checkout.
- Now do the same thing and prompt input for the quantity of the product they want. We'll use the we used for adding
*/
Product product = productManager.getProductByID(cartItem.getProductID());
const int productQty = product.getQuantity();
if (productQty == 0) {
std::cout << "Product is actually out of stock!" << std::endl;
return;
}
int qty = handleInputCartQty(product);
// Now update the quantity for the cart item and display success message
cartItemManager.updateCartItem(currentCustomerID, cartItem.getProductID(), qty);
std::cout << "Updated Cart Item!" << std::endl;
}
/*
- Helper function for handling prompting input for the qty of the product that the customer is going to put in the cart.
Here we decide a user at maximum can only have 10 of one product in their cart, but if the actual quantity in stock for
that product is less than 10, the max is the remaining product
*/
int handleInputCartQty(Product product) {
int productQty = product.getQuantity();
int min = 1;
int max = 10;
if (productQty < max) {
max = productQty;
}
int qty = getValidRangeInput<int>("Enter quantity of product you're adding to cart: ", min, max);
return qty;
}
// Handles displaying paginated list of all cart items
void displayCustomerCart() {
std::vector<CartItem> cartItems = cartItemManager.getCustomerCartItems(currentCustomerID);
if (cartItems.size() == 0) {
std::cout << "No Items in Cart!" << std::endl;
return;
}
navigatePaginatedItems<CartItem>(cartItems, 5, "Customer Cart");
}
// Handles checking out the cart
void handleCheckout() {
// Fetch cart items for the customer
std::vector<CartItem> cartItems = cartItemManager.getCustomerCartItems(currentCustomerID);
if (cartItems.size() == 0) {
std::cout << "Cannot checkout since no items in Cart!" << std::endl;
return;
}
// Get vector of product ID values for products in the customer's cart.
std::vector<int> productIDs;
for (size_t i = 0; i < cartItems.size(); i++) {
int product_id = cartItems[i].getProductID();
productIDs.push_back(product_id);
}
/*
- productQuantityMap: Get a map of key product_id and value quantity in stock for that product
- newProductQuantities: Vector of tuples in form (product_id, qty), where qty is going to be the updated number in stock for the product (num in stock - num in cart)
*/
std::map<int, int> productQuantityMap = productManager.getProductQuantities(productIDs);
std::vector<std::tuple<int, int>> newProductQuantities;
// Total in your cart.
float total = 0;
// Check if each cart item's qty (num in cart) doesn't exceed the product's qty (num in stock)
for (size_t i = 0; i < cartItems.size(); i++) {
int product_id = cartItems[i].getProductID();
int numInCart = cartItems[i].getQty();
int numInStock = productQuantityMap[product_id];
if (numInCart > numInStock) {
std::cout << "Product '" << cartItems[i].getProductName() << "' has a quantity (" << numInCart
<< ") in your cart that exceeds the available stock (" << numInStock << ")" << std::endl;
return;
}
// Calculate the new product qty