|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from enum import Enum |
| 3 | + |
| 4 | + |
| 5 | +class Condition(Enum): |
| 6 | + NEW = 0 |
| 7 | + GOOD = 1 |
| 8 | + OKAY = 2 |
| 9 | + BAD = 3 |
| 10 | + |
| 11 | + |
| 12 | +class Bike: |
| 13 | + def __init__( |
| 14 | + self, |
| 15 | + description: str = "Really Beautiful", |
| 16 | + condition: str = "Perfect Condition", |
| 17 | + sale_price: float = 0, |
| 18 | + cost: float = 0, |
| 19 | + year: int = 2015, |
| 20 | + ): |
| 21 | + """_summary_ |
| 22 | +
|
| 23 | + :param description: _description_, defaults to "Really Beautiful" |
| 24 | + :type description: str, optional |
| 25 | + :param condition: _description_, defaults to "Perfect Condition" |
| 26 | + :type condition: str, optional |
| 27 | + :param sale_price: _description_, defaults to 0 |
| 28 | + :type sale_price: float, optional |
| 29 | + :param cost: _description_, defaults to 0 |
| 30 | + :type cost: float, optional |
| 31 | + :param year: _description_, defaults to 2015 |
| 32 | + :type year: int, optional |
| 33 | + """ |
| 34 | + self.description = description |
| 35 | + self.condition = condition |
| 36 | + self.sale_price = sale_price |
| 37 | + self.cost = cost |
| 38 | + |
| 39 | + self.age = 2023 - year |
| 40 | + self.sold = False |
| 41 | + self.premium = None |
| 42 | + |
| 43 | + def update_sale_price(self, sale_price: float): |
| 44 | + if self.sold: |
| 45 | + raise AttributeError("Cannot update sale price of a sold bike.") |
| 46 | + if sale_price <= 0: |
| 47 | + raise ValueError("Sale price must be greater than zero.") |
| 48 | + self.sale_price = sale_price |
| 49 | + |
| 50 | + def is_premium(self): |
| 51 | + self.premium = "Yes" |
| 52 | + |
| 53 | + def sell(self) -> float: |
| 54 | + self.sold = True |
| 55 | + profit = self.sale_price - self.cost |
| 56 | + print(f"Sold for a profit: {profit}") |
| 57 | + return profit |
| 58 | + |
| 59 | + def service(self, cost, new_sale_price=None, new_condition=None): |
| 60 | + self.cost += cost |
| 61 | + if new_sale_price is not None: |
| 62 | + self.update_sale_price(new_sale_price) |
| 63 | + if new_condition is not None: |
| 64 | + self.condition = new_condition |
| 65 | + |
| 66 | + @staticmethod |
| 67 | + def sing_the_bike_song(): |
| 68 | + print("Singing the Bike Song") |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + my_bike = Bike( |
| 73 | + description="Black and Beautiful", |
| 74 | + condition=Condition.BAD, |
| 75 | + sale_price=1000, |
| 76 | + cost=100, |
| 77 | + year=1965, |
| 78 | + ) |
| 79 | + print(my_bike) |
| 80 | + print(type(my_bike)) |
| 81 | + print(my_bike.condition) |
| 82 | + my_bike.service(cost=100, new_condition=Condition.OKAY, new_sale_price=1200) |
| 83 | + print(my_bike.condition) |
| 84 | + my_bike.sell() |
| 85 | + my_bike.sing_the_bike_song() |
0 commit comments