Projects/product_class.py

26 lines
854 B
Python

class product:
def __init__(self, name: str, price: float, quantity = 0):
assert price >= 0, "Price must be non-negative"
assert quantity >= 0, "Quantity must be non-negative"
self.name = name
self.price = price
self.quantity = quantity
def calculate_total_price(self) -> float:
return self.price * self.quantity
def display_info(self):
return f"Product: {self.name}, Price: ${self.price:.2f}"
# Example usage:
product1 = product("Laptop", 1999.99, 1)
print(product1.display_info()) # Output: Item: Laptop, Price: $1999.99
print(product1.calculate_total_price()) # Output: 3999996.0001
product2 = product("Smartphone", 799.99, -3)
print(product2.display_info()) # Output: Item: Smartphone, Price: $799.99
print(product2.calculate_total_price()) # Output: 2399.97