forked from ariannedee/oop-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_5_methods.py
More file actions
57 lines (41 loc) · 1.25 KB
/
Copy pathexample_5_methods.py
File metadata and controls
57 lines (41 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Bike class for use in a retail shop
"""
from enum import Enum
class Condition(Enum):
NEW = 0
GOOD = 1
OKAY = 2
BAD = 3
class MethodNotAllowed(Exception):
pass
class Bike:
def __init__(self, description, condition, sale_price, cost=0):
self.cost = cost
self.sale_price = sale_price
self.condition = condition
self.description = description
self.sold = False
def update_sale_price(self, new_sale_price):
if self.sold:
raise MethodNotAllowed("You can't update the sale price of a bike that's been sold")
self.sale_price = new_sale_price
def sell(self):
self.sold = True
profit = self.sale_price - self.cost
return profit
def service(self, cost, new_sale_price=None, new_condition=None):
self.cost += cost
if new_sale_price:
self.update_sale_price(new_sale_price)
if new_condition:
self.condition = new_condition
if __name__ == '__main__':
my_bike = Bike("Red Releigh cruiser", Condition.GOOD, 450, 50)
print(my_bike)
my_bike.update_sale_price(400)
profit = my_bike.sell()
print(profit)
my_bike.update_sale_price(1000)
my_bike.hello = 'world'
'world'.hi = 'hi'