-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecommerce.cpp
More file actions
233 lines (203 loc) · 7.67 KB
/
ecommerce.cpp
File metadata and controls
233 lines (203 loc) · 7.67 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
#include <iostream>
#include <vector>
#include <unordered_map>
#include <iomanip> // For setting precision
using namespace std;
// Product Class
class Product {
public:
string id, name, category;
double price;
Product(string id, string name, double price, string category)
: id(id), name(name), price(price), category(category) {}
};
// CartItem Class to hold products and their quantities in the cart
class CartItem {
public:
Product product;
int quantity;
CartItem(Product product, int quantity)
: product(product), quantity(quantity) {}
// Marking the getTotalCost method as const
double getTotalCost() const {
return product.price * quantity;
}
};
// Cart Class to handle cart operations
class Cart {
private:
vector<CartItem> items;
public:
void addToCart(Product product, int quantity) {
for (auto &item : items) {
if (item.product.id == product.id) {
item.quantity += quantity;
return;
}
}
items.push_back(CartItem(product, quantity));
}
void removeFromCart(string productId, int quantity) {
for (auto it = items.begin(); it != items.end(); ++it) {
if (it->product.id == productId) {
if (quantity >= it->quantity) {
cout << "Removed all " << it->product.name << "(s) from the cart.\n";
items.erase(it);
} else {
it->quantity -= quantity;
cout << "Reduced quantity of " << it->product.name << " to " << it->quantity << ".\n";
}
return;
}
}
cout << "Product not found in cart.\n";
}
void viewCart() const {
if (items.empty()) {
cout << "Your cart is empty.\n";
return;
}
cout << "Your Cart:\n";
double totalCost = 0;
for (const auto &item : items) {
cout << item.product.name << " - Quantity: " << item.quantity
<< ", Price: " << item.product.price << " USD"
<< ", Total: " << item.getTotalCost() << " USD\n";
totalCost += item.getTotalCost();
}
cout << "Total (before discounts): " << totalCost << " USD\n";
}
double getTotalCost() const {
double totalCost = 0;
for (const auto &item : items) {
totalCost += item.getTotalCost();
}
return totalCost;
}
vector<CartItem> &getItems() {
return items;
}
bool isEmpty() const {
return items.empty();
}
};
// Discount System
class DiscountSystem {
public:
double applyDiscounts(Cart &cart) {
double totalDiscount = 0;
for (auto &item : cart.getItems()) {
if (item.product.category == "Fashion" && item.quantity >= 2) {
// Buy 1 Get 1 Free for Fashion items
int freeItems = item.quantity / 2;
totalDiscount += freeItems * item.product.price;
cout << "Buy 1 Get 1 Free on " << item.product.name << " applied.\n";
} else if (item.product.category == "Electronics") {
// 10% Off for Electronics
totalDiscount += 0.10 * item.getTotalCost();
cout << "10% Off on " << item.product.name << " applied.\n";
}
}
cout << "Total Discount: " << totalDiscount << " USD\n";
return totalDiscount;
}
void listDiscounts() {
cout << "Available Discounts:\n";
cout << "1. Buy 1 Get 1 Free on Fashion items\n";
cout << "2. 10% Off on Electronics\n";
}
};
// Currency Conversion
class CurrencyConverter {
unordered_map<string, double> conversionRates;
public:
CurrencyConverter() {
conversionRates["EUR"] = 0.85;
conversionRates["GBP"] = 0.75;
}
double convert(double amount, string currency) {
return amount * getConversionRate(currency);
}
void showAvailableCurrencies() {
cout << "Available Currencies: EUR, GBP\n";
}
double getConversionRate(string currency) {
if (conversionRates.find(currency) != conversionRates.end()) {
return conversionRates[currency];
}
return 1; // No conversion if currency is not found
}
};
// Pre-populate product catalog
vector<Product> productCatalog = {
Product("P001", "Laptop", 1000.00, "Electronics"),
Product("P002", "Phone", 500.00, "Electronics"),
Product("P003", "T-Shirt", 20.00, "Fashion")
};
// Function to find product by ID
Product* findProductById(string productId) {
for (auto &product : productCatalog) {
if (product.id == productId) {
return &product; // Return pointer to found product
}
}
return nullptr; // Return null pointer if product is not found
}
int main() {
Cart cart;
DiscountSystem discountSystem;
CurrencyConverter currencyConverter;
string command, productId, currency;
int quantity;
while (true) {
cout << "\nEnter a command (add_to_cart, remove_from_cart, view_cart, list_discounts, checkout, quit): ";
cin >> command;
if (command == "add_to_cart") {
cin >> productId >> quantity;
Product* product = findProductById(productId);
if (product) {
cart.addToCart(*product, quantity);
cout << "Added " << quantity << " " << product->name << "(s) to the cart.\n";
} else {
cout << "Product with ID " << productId << " not found.\n";
}
} else if (command == "remove_from_cart") {
cin >> productId >> quantity;
cart.removeFromCart(productId, quantity);
} else if (command == "view_cart") {
cart.viewCart();
} else if (command == "list_discounts") {
discountSystem.listDiscounts();
} else if (command == "checkout") {
if (cart.isEmpty()) {
cout << "Your cart is empty. Add items to proceed with checkout.\n";
continue;
}
cout << "Applying discounts...\n";
double discount = discountSystem.applyDiscounts(cart);
double finalTotal = cart.getTotalCost() - discount;
cout << fixed << setprecision(2) << "Final Total in USD: " << finalTotal << " USD\n";
cout << "Would you like to view it in a different currency? (yes/no): ";
string response;
cin >> response;
if (response == "yes") {
currencyConverter.showAvailableCurrencies();
cout << "Enter currency: ";
cin >> currency;
if (currencyConverter.getConversionRate(currency) != 1) {
double convertedTotal = currencyConverter.convert(finalTotal, currency);
double rate = currencyConverter.getConversionRate(currency);
cout << fixed << setprecision(2) << "Final Total in " << currency << ": " << convertedTotal << " " << currency << " (Conversion rate: " << rate << ")\n";
} else {
cout << "Unsupported currency.\n";
}
}
} else if (command == "quit") {
cout << "Thank you for shopping! Exiting...\n";
break;
} else {
cout << "Invalid command. Available commands: add_to_cart, remove_from_cart, view_cart, list_discounts, checkout, quit.\n";
}
}
return 0;
}