Untitled

 avatar
unknown
plain_text
2 years ago
2.5 kB
4
Indexable
#include <iostream>
#include <string>
#include <vector>

class Product {
public:
    Product(std::string name, double price) : name_(name), price_(price) {}
    std::string getName() const { return name_; }
    double getPrice() const { return price_; }
private:
    std::string name_;
    double price_;
};

class Grocery : public Product {
public:
    Grocery(std::string name, double price) : Product(name, price) {}
};

class Vegetable : public Product {
public:
    Vegetable(std::string name, double price) : Product(name, price) {}
};

class Snack : public Product {
public:
    Snack(std::string name, double price) : Product(name, price) {}
};

class Customer {
public:
    Customer(std::string name) : name_(name), totalBill_(0) {}
    void addToCart(Product product) {
        cart_.push_back(product);
        totalBill_ += product.getPrice();
    }
    double getTotalBill() const { return totalBill_; }
    std::string getName() const { return name_; }
private:
    std::string name_;
    std::vector<Product> cart_;
    double totalBill_;
};

int main() {
    Customer customer1("John");
    std::cout << "Customer: " << customer1.getName() << std::endl;
    std::cout << "Items Purchased:" << std::endl;
    Grocery rice("Rice", 1.00);
    customer1.addToCart(rice);
    std::cout << "Product: " << rice.getName() << " Price: $" << rice.getPrice() << std::endl;
    Snack chips("Chips", 2.00);
    customer1.addToCart(chips);
    std::cout << "Product: " << chips.getName() << " Price: $" << chips.getPrice() << std::endl;
    std::cout << "Total Bill: $" << customer1.getTotalBill() << std::endl;

    Customer customer2("Jane");
    std::cout << "Customer: " << customer2.getName() << std::endl;
    std::cout << "Items Purchased:" << std::endl;
    Vegetable carrot("Carrot", 1.99);
    customer2.addToCart(carrot);
    std::cout << "Product: " << carrot.getName() << " Price: $" << carrot.getPrice() << std::endl;
    chips = Snack("Chips", 2.99);
    customer2.addToCart(chips);
    std::cout << "Product: " << chips.getName() << " Price: $" << chips.getPrice() << std::endl;
    std::cout << "Total Bill: $" << customer2.getTotalBill() << std::endl;

    if (customer2.getTotalBill() > 5.0) {
        double discount = customer2.getTotalBill() * 0.1;
        std::cout << "Discount: $" << discount << std::endl;
        std::cout << "Total Bill after Discount: $" << customer2.getTotalBill() - discount << std::endl;
    }

    return 0;
}
Editor is loading...