Code:
class Product:
def __init__(self, product_name, price, quantity):
self.product_name = product_name
self.price = price
self.quantity = quantity
def add_stock(self, amount):
self.quantity += amount
print(f"{amount} units added. New stock for {self.product_name}:
{self.quantity} units.")
def sell(self, amount):
if amount <= self.quantity:
self.quantity -= amount
print(f"{amount} units of {self.product_name} sold. Remaining stock:
{self.quantity} units.")
else:
print(f"Insufficient stock to sell {amount} units of {self.product_name}. Only
{self.quantity} units available.")
def get_stock_value(self):
return self.price * self.quantity
def __str__(self):
return f"Product: {self.product_name}, Price: ${self.price}, Stock:
{self.quantity} units"
class CategoryProduct(Product):
def __init__(self, product_name, price, quantity, category):
super().__init__(product_name, price, quantity)
self.category = category
def __str__(self):
return f"Category: {self.category}, {super().__str__()}"
class DiscountProduct(Product):
def __init__(self, product_name, price, quantity, discount_percentage):
super().__init__(product_name, price, quantity)
self.discount_percentage = discount_percentage
def get_discounted_price(self):
return self.price * (1 - self.discount_percentage / 100)
def get_stock_value(self):
discounted_price = self.get_discounted_price()
return discounted_price * self.quantity
def __str__(self):
return f"{super().__str__()}, Discount: {self.discount_percentage}% off,
Discounted Price: ${self.get_discounted_price():.2f}"
product1 = Product("Laptop", 1000, 50)
product2 = CategoryProduct("Smartphone", 700, 30, "Electronics")
product3 = DiscountProduct("Headphones", 150, 100, 10)
print(product1)
product1.add_stock(20)
product1.sell(15)
print(f"Total stock value for {product1.product_name}:
${product1.get_stock_value()}")
print("\n")
print(product2)
product2.add_stock(10)
product2.sell(5)
print(f"Total stock value for {product2.product_name}:
${product2.get_stock_value()}")
print("\n")
print(product3)
product3.add_stock(50)
product3.sell(30)
print(f"Total stock value for {product3.product_name} after discount:
${product3.get_stock_value()}")
Output: