Untitled
unknown
plain_text
10 months ago
1.4 kB
21
Indexable
class Product:
next_number = 1
@classmethod
def get_next_number(cls):
val = cls.next_number
cls.next_number += 1
return val
def __init__(self, name: str, price: float):
self.name = name
self.price = price
self.serial_number = Product.get_next_number()
def advertise(self):
msg = f'Lovely {self.name} for only {self.price} $'
return msg
def apply_discount(self, percentage):
self.price -= self.price * (percentage / 100)
def __str__(self):
return f'{self.name} with {self.price} $'
def __eq__(self, other):
if not isinstance(other, Product):
return False
return self.name == other.name and self.price == other.price
cheese = Product('Cheese', 12.00)
cheese1 = Product('Cheese', 12.00)
print(cheese == cheese1)
print(cheese != cheese1)
print(cheese == "cheese")
wine = Product('Wine', 20.00)
chips = Product('Chips', 15.00)
print(cheese.price)
print(wine.price)
cheese.price = 9.00
print(cheese.price)
cheese.price = cheese.price * 2
print(cheese.price)
print(cheese.advertise())
wine.apply_discount(15)
print(wine.price)
print(wine.serial_number)
print(cheese.serial_number)
print(chips.serial_number)
print(Product.next_number)
print(wine)
print(chips)
print(cheese == cheese1)
Editor is loading...
Leave a Comment