Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.8 kB
2
Indexable
Never
class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = price

    def get_details(self):
        return f"{self.name} - ${self.price:.2f}"


class DailyNecessity(Product):
    def __init__(self, name, price, category):
        super().__init__(name, price)
        self.category = category

    def get_details(self):
        return f"{self.name} - ${self.price:.2f} (Category: {self.category})"


class Book(Product):
    def __init__(self, name, price, author, genre):
        super().__init__(name, price)
        self.author = author
        self.genre = genre

    def get_details(self):
        return f"{self.name} - ${self.price:.2f} (Author: {self.author}, Genre: {self.genre})"


class ElectronicProduct(Product):
    def __init__(self, name, price, brand):
        super().__init__(name, price)
        self.brand = brand

    def get_details(self):
        return f"{self.name} - ${self.price:.2f} (Brand: {self.brand})"


class ShoppingCart:
    def __init__(self):
        self.items = {}

    def add_item(self, product, quantity=1):
        if product in self.items:
            self.items[product] += quantity
        else:
            self.items[product] = quantity

    def remove_item(self, product, quantity=1):
        if product in self.items:
            self.items[product] -= quantity
            if self.items[product] <= 0:
                del self.items[product]

    def get_total_price(self):
        total = 0
        for product, quantity in self.items.items():
            total += product.price * quantity
        return total


class User:
    def __init__(self, name, email):
        self.name = name
        self.email = email
        self.shopping_cart = ShoppingCart()

    def add_to_cart(self, product, quantity=1):
        self.shopping_cart.add_item(product, quantity)

    def remove_from_cart(self, product, quantity=1):
        self.shopping_cart.remove_item(product, quantity)

    def get_cart_total(self):
        return self.shopping_cart.get_total_price()


# Example usage
if __name__ == "__main__":
    # Create some products
    milk = DailyNecessity("Milk", 2.5, "Grocery")
    python_book = Book("Python Crash Course", 25.0,
                       "Eric Matthes", "Programming")
    phone = ElectronicProduct("Smartphone", 400.0, "Samsung")

    # Create a user
    user = User("John Doe", "john@example.com")

    # Add products to the user's shopping cart
    user.add_to_cart(milk, 2)
    user.add_to_cart(python_book)
    user.add_to_cart(phone)

    # Get the total price of the user's shopping cart
    cart_total = user.get_cart_total()
    print(f"Total price in the shopping cart: ${cart_total:.2f}")