-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
194 lines (159 loc) · 6.77 KB
/
main.cpp
File metadata and controls
194 lines (159 loc) · 6.77 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
// Tyeon Ford
// 10/30/25
#include <iostream>
#include <vector>
#include <iomanip>
#include "Product.h"
#include "Keyboard.h"
#include "Monitor.h"
#include "Mouse.h"
#include "SSD.h"
#include "GPU.h"
#include "USBHub.h"
#include "ShoppingCart.h"
using namespace std;
int getValidInteger(int min, int max) {
int value;
cin >> value;
cin.ignore(1000, '\n');
return value;
}
void displayMainMenu() {
cout << "\n====== MAIN MENU ======\n";
cout << "1. Browse All Products\n";
cout << "2. View Product Details\n";
cout << "3. Add Item to Cart\n";
cout << "4. Remove Item from Cart\n";
cout << "5. Checkout\n";
cout << "6. Exit\n";
cout << "=======================\n";
cout << "Enter your choice (1-6): ";
}
void displayAllProducts(const vector<Product*>& products) {
cout << "\n========== ALL PRODUCTS ==========\n\n";
for (size_t i = 0; i < products.size(); i++) {
cout << "[" << i + 1 << "] " << products[i]->getName(); // Displays the product name
cout << " - $" << fixed << setprecision(2) << products[i]->getPrice(); // and the price
if (!products[i]->isInStock()) { // Out of stock
cout << " [OUT OF STOCK]";
} else {
cout << " (" << products[i]->getStock() << " in stock)"; // The product is in stock
}
cout << "\n";
}
cout << "\n==================================\n";
}
void viewProductDetails(const vector<Product*>& products) {
cout << "\nEnter product number (1-" << products.size() << "): ";
int choice = getValidInteger(1, products.size());
products[choice - 1]->displayDetails();
}
void addToCart(const vector<Product*>& products, ShoppingCart& cart) {
displayAllProducts(products);
cout << "\nEnter product number to add (1-" << products.size() << "): ";
int productChoice = getValidInteger(1, products.size()); // chooses the product
Product* selectedProduct = products[productChoice - 1]; // adds to cart
if (!selectedProduct->isInStock()) { // Edgecase in the situation the product is out of stock
cout << "\nOut of stock!\n";
return;
}
cout << "Enter quantity: ";
int quantity = getValidInteger(1, selectedProduct->getStock());
cart.addItem(selectedProduct, quantity);
}
void removeFromCart(ShoppingCart& cart) {
if (cart.isEmpty()) { // Cart is already empty
cout << "\nYour cart is empty!\n";
return;
}
cout << "\n===== YOUR CART =====\n";
const auto& items = cart.getItems(); // Get cart items
vector<Product*> cartProducts;
int index = 1;
for (const auto& pair : items) {
cout << "[" << index << "] " << pair.first->getName() << " (Qty: " << pair.second << ")\n";
cartProducts.push_back(pair.first);
index++;
}
cout << "=====================\n";
cout << "\nEnter item number to remove (1-" << cartProducts.size() << "): ";
int choice = getValidInteger(1, cartProducts.size());
Product* selectedProduct = cartProducts[choice - 1];
int currentQty = items.at(selectedProduct);
cout << "Enter quantity to remove (1-" << currentQty << "): ";
int quantity = getValidInteger(1, currentQty);
cart.removeItem(selectedProduct, quantity);
}
void checkout(ShoppingCart& cart) {
if (cart.isEmpty()) { // In case cart is empty
cout << "\nYour cart is empty! Add items before checking out.\n";
return;
}
printReceipt(cart); // Using a friend's function
cart.clearCart();
cout << "Your order has been placed successfully!\n";
}
int main() {
// Initializing the products & variables
vector<Product*> products;
ShoppingCart cart;
int choice;
// Keyboards
products.push_back(new Keyboard("Corsair K95 RGB", 199.99, 5, "Premium mechanical gaming keyboard", "Cherry MX Red", true, "Full-Size (104-key)"));
products.push_back(new Keyboard("Keychron K2", 89.99, 12, "Compact wireless mechanical keyboard", "Gateron Brown", true, "75% (84-key)"));
products.push_back(new Keyboard("Anne Pro 2", 79.99, 0, "Ultra-portable 60% mechanical keyboard", "Kailh Box White", true, "60% (61-key)"));
// Monitors
products.push_back(new Monitor("Dell S2721DGF", 349.99, 8, "27-inch gaming monitor with excellent response time", 27, "2560x1440", 165, "IPS"));
products.push_back(new Monitor("LG 27GN950-B", 799.99, 3, "4K gaming monitor with Nano IPS technology", 27, "3840x2160", 144, "Nano IPS"));
// Mice
products.push_back(new Mouse("Logitech G Pro X", 149.99, 15, "Professional wireless gaming mouse", 25600, true, 5, "HERO 25K"));
products.push_back(new Mouse("Razer DeathAdder V2", 69.99, 20, "Ergonomic wired gaming mouse", 20000, false, 8, "Focus+ Optical"));
// SSDs
products.push_back(new SSD("Samsung 980 PRO", 129.99, 25, "High-performance NVMe SSD for gaming and content creation", 1000, 7000, 5000, "M.2 NVMe"));
products.push_back(new SSD("Crucial MX500", 54.99, 30, "Reliable SATA SSD for everyday computing", 500, 560, 510, "2.5-inch SATA"));
// GPUs
products.push_back(new GPU("NVIDIA RTX 4070", 599.99, 6, "Powerful graphics card for 1440p gaming", 12, "AD104", 2475, "NVIDIA"));
products.push_back(new GPU("AMD RX 7800 XT", 499.99, 7, "Excellent performance for high-refresh gaming", 16, "Navi 32", 2430, "AMD"));
// USB Hubs
products.push_back(new USBHub("Anker 7-in-1 USB-C Hub", 45.99, 18, "Compact hub with multiple ports and power delivery", 7, true, 100, "USB 3.0"));
products.push_back(new USBHub("CalDigit TS3 Plus", 279.99, 5, "Professional Thunderbolt 3 dock with 15 ports", 15, true, 87, "Thunderbolt 3"));
cout << "\n======================================\n";
cout << " WELCOME TO BYTEMART TECH SHOP! \n";
cout << "======================================\n";
while (true) {
// opening
displayMainMenu();
choice = getValidInteger(1, 6);
switch (choice) {
// display all products
case 1:
displayAllProducts(products);
break;
// view a product's details
case 2:
viewProductDetails(products);
break;
// add to cart
case 3:
addToCart(products, cart);
break;
// remove from cart
case 4:
removeFromCart(cart);
break;
// checkout section
case 5:
checkout(cart);
break;
// exit shop
case 6:
cout << "\nThank you for visiting ByteMart!\n\n";
// Clean up
for (Product* p : products) {
delete p;
}
return 0;
}
}
return 0;
}