Projects/MyProductClass.py

36 lines
1.4 KiB
Python

class Product:
pay_rate = 0.8 # Class variable for a standard discount of 20%
def __init__(self, name, price, quantity):
assert price >= 0, f"Product price {price} is not greater than or equal to zero!"
assert quantity >= 0, f"Product quantity {quantity} is not greater than or equal to zero!"
self.name = name
self.price = price
self.quantity = quantity
def calculate_total_price(self):
return self.price * self.quantity
def apply_discount(self):
return self.calculate_total_price() * (self.pay_rate)
# Example usage:
if __name__ == "__main__":
item1 = Product("Laptop", 1000, 2) # Item with standard discount
item2 = Product("Smartphone", 100, 3) # Item with special discount
print(f'Price of {item1.name}: ${item1.price:.2f}')
print(f'Price of {item1.name} after 20% discount: ${item1.price * Product.pay_rate:.2f}')
print(f"Total price for {item1.name}(s) after 20% discount: ${item1.apply_discount():.2f}") # Applying standard discount
print()
item2.pay_rate = 0.7 # Instance variable for a special discount of 30%
print(f'Price of {item2.name}: ${item2.price:.2f}')
print(f'Price of {item2.name}(s) after 30% discount: ${item2.price * item2.pay_rate:.2f}')
print(f"Total price for {item2.name}(s) after 30% discount: ${item2.apply_discount():.2f}") # Applying item-specific discount