1- from datetime import datetime
21from enum import Enum
32
43
@@ -10,32 +9,22 @@ class Condition(Enum):
109
1110
1211class Bike (object ):
13-
14- def __init__ (self , cost , make , model , year , condition ):
15- self .cost = cost
16- self .make = make
17- self .model = model
18- self .year = year
12+ def __init__ (self , description , condition , sale_price , cost = 0 ):
13+ self .description = description
1914 self .condition = condition
15+ self .sale_price = sale_price
16+ self .cost = cost
2017
21- self .sale_price = self .update_sale_price ()
2218 self .sold = False
23-
2419 print (f'New bike: { self } ' )
2520
2621 def __del__ (self ):
2722 print (f'Deleting bike: { self } ' )
2823
29- def update_sale_price (self ):
30- """
31- Set the current sale price based on the make, model, age, and condition
32- """
33- original_value = lookup_msrp_value (self .make , self .model )
34- current_year = datetime .now ().year
35- current_value = original_value * (1 - (current_year - self .year ) * 0.015 )
36- current_value = current_value * self .condition .value
37- self .sale_price = current_value
38- return self .sale_price
24+ def update_sale_price (self , sale_price ):
25+ if self .sold :
26+ return Exception ('Action not allowed. Bike has already been sold' )
27+ self .sale_price = sale_price
3928
4029 def sell (self ):
4130 """
@@ -45,36 +34,33 @@ def sell(self):
4534 profit = self .sale_price - self .cost
4635 return profit
4736
48- def service (self , cost , new_condition ):
37+ def service (self , spent , sale_price = None , condition = None ):
4938 """
50- Service the bike and update sale price
39+ Service the bike and update attributes
5140 """
52- self .cost += cost
53- self .condition = new_condition
54- self .update_sale_price ()
55- return self .sale_price
41+ self .cost += spent
42+ if sale_price :
43+ self .update_sale_price (sale_price )
44+ if self .condition :
45+ self .condition = condition
5646
5747 def __repr__ (self ):
58- return f'Bike: { self .year } { self .make } { self .model } ({ "sold" if self .sold else self .sale_price } )'
48+ sold_or_price = "sold" if self .sold else f"${ self .sale_price } "
49+ return f'Bike: { self .description } ({ sold_or_price } )'
5950
6051 def __str__ (self ):
61- return f'{ self .year } { self .make } { self .model } '
62-
63-
64- def lookup_msrp_value (make , model ):
65- """
66- Determine original sale price of a bike when new
67- """
68- return 1000
52+ return self .description
6953
7054
7155if __name__ == '__main__' :
72- bike = Bike (100 , 'Univega' , ' Alpina' , 1999 , Condition .OKAY ) # __init__ called
56+ bike = Bike ('Univega Alpina, orange' , Condition .OKAY , sale_price = 500 , cost = 100 )
7357
74- print (bike ) # __str__ called
58+ print (bike ) # __str__ called
59+ print (str (bike )) # __str__ called
7560
76- print ([bike ]) # __repr__ called
61+ print ([bike ]) # __repr__ called
62+ print (repr (bike )) # __repr__ called
7763
78- del bike # __del__ called
64+ del bike # __del__ called
7965
80- Bike (0 , 'Raleigh' , ' Talus 2' , 2013 , Condition .BAD ) # __init__ and __del__ called
66+ bike = Bike ('Raleigh Talus 2' , Condition .BAD , sale_price = 20 ) # __init__ and __del__ called
0 commit comments