Code in C
Code in C
Code in C
#include<bits/stdc++.h>
using namespace std;
class Product {
private:
string name;
string description;
double price;
int stockLevel;
public:
// constructor
Product(string name, string description, double price, int stockLevel) {
this->name = name;
this->description = description;
this->price = price;
this->stockLevel = stockLevel;
}
// operator overloading
bool operator==(const Product& p) const {
return (getName() == p.getName() && getDescription() == p.getDescription() &&
getPrice() == p.getPrice() && getStockLevel() == p.getStockLevel());
}
};
namespace std {
template<>
struct hash<Product> {
size_t operator()(const Product& p) const {
size_t h1 = hash<string>()(p.getName());
size_t h2 = hash<string>()(p.getDescription());
size_t h3 = hash<double>()(p.getPrice());
size_t h4 = hash<int>()(p.getStockLevel());
return h1 ^ h2 ^ h3 ^ h4;
}
};
template<>
struct equal_to<Product> {
bool operator()(const Product& p1, const Product& p2) const {
return p1 == p2;
}
};
}
class ShoppingCart {
private:
std::vector<Product> products;
public:
// constructor
ShoppingCart() {}
class Customer {
private:
std::string name;
std::string address;
std::string email;
public:
Customer() : name(""), address(""), email("") { }
Customer(std::string name, std::string address, std::string email) :
name(name), address(address), email(email) { }
class Inventory {
private:
std::unordered_map<Product, int> products;
public:
// constructor
Inventory(){};
// method to add a product to the inventory
void addProduct(Product product, int stock) {
products[product] = stock;
}
// method to update the stock level of a product
void updateStock(Product product, int stock) {
products[product] = stock;
}
// method to check if a product is in stock
bool isInStock(Product product) {
return products[product] > 0;
}
// method to restock a product
void restock(Product product, int quantity) {
products[product] += quantity;
}
};
class Payment {
private:
double amount;
string paymentMethod;
public:
// constructor
Payment() : paymentMethod(""), amount(0) { }
Payment(double amount, string paymentMethod) {
this->amount = amount;
this->paymentMethod = paymentMethod;
}
class Shipping {
private:
string shippingMethod;
double shippingCost;
public:
// constructor
Shipping() : shippingMethod(""), shippingCost(0.0) { }
Shipping(string shippingMethod, double shippingCost) {
this->shippingMethod = shippingMethod;
this->shippingCost = shippingCost;
}
class Order {
private:
Customer customer;
vector<Product> products;
double totalCost;
Payment payment;
Shipping shipping;
public:
// constructor
Order(Customer customer, vector<Product> products, double totalCost, Payment payment,
Shipping shipping) {
this->customer = customer;
this->products = products;
this->totalCost = totalCost;
this->payment = payment;
this->shipping = shipping;
}
class Admin {
private:
Inventory inventory;
vector<Product> products;
vector<Customer> customers;
vector<Order> orders;
public:
// constructor
Admin() {}
// methods for adding and updating products
void addProduct(Product product) { products.push_back(product); }
void updateProduct(Product product) {
for (int i = 0; i < products.size(); i++) {
if (products[i] == product) {
products[i] = product;
break;
}
}
}