-
Notifications
You must be signed in to change notification settings - Fork 0
/
refactoring_15.py
147 lines (105 loc) · 3.6 KB
/
refactoring_15.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class Movie:
CHILDRENS = 2
REGULAR = 0
NEW_RELEASE = 1
_title = None
_price = None
def __init__(self, title, priceCode):
self._title = title
self.setPriceCode(priceCode)
def getPriceCode(self):
return self._price.getPriceCode()
def setPriceCode(self, arg):
if arg == Movie.REGULAR:
self._price = RegularPrice()
elif arg == Movie.NEW_RELEASE:
self._price = NewReleasePrice()
elif arg == Movie.CHILDRENS:
self._price = ChildrensPrice()
def getTitle(self):
return self._title
def getCharge(self, daysRented):
return self._price.getCharge(daysRented)
def getFrequentPoints(self, daysRented):
if (self.getPriceCode() == Movie.NEW_RELEASE) & (daysRented > 1):
return 2
else:
return 1
class Price:
def getPriceCode(self):
pass
def getCharge(self, daysRented):
result = 0
priceCode = self.getPriceCode()
if priceCode == Movie.REGULAR:
result += 2
if daysRented > 2:
result += (daysRented - 2) * 1.5
elif priceCode == Movie.NEW_RELEASE:
result += daysRented * 3
elif priceCode == Movie.CHILDRENS:
result += 1.5
if daysRented > 3:
result += (daysRented - 3) * 1.5
return result
class ChildrensPrice(Price):
def getPriceCode(self):
return Movie.CHILDRENS
class NewReleasePrice(Price):
def getPriceCode(self):
return Movie.NEW_RELEASE
class RegularPrice(Price):
def getPriceCode(self):
return Movie.REGULAR
class Rental:
_movie = None
_daysRented = None
def __init__(self, movie, daysRented):
self._movie = movie
self._daysRented = daysRented
def getDaysRented(self):
return self._daysRented
def getMovie(self):
return self._movie
def getCharge(self):
return self._movie.getCharge(self._daysRented)
def getFrequentPoints(self):
return self._movie.getFrequentPoints()
class Customer:
_name = None
_rentals = []
def __init__(self, name):
self._name = name
def addRental(self, arg):
self._rentals.append(arg)
def getName(self):
return self._name
def getTotalCharge(self):
result = 0
for each in self._rentals:
result += each.getCharge()
return result
def getTotalFrequentRenterPoints(self):
result=0
for each in self._rentals:
result += each.getFrequentPoints()
return result
def statement(self):
result = "Rental Record for %s\n" % self.getName()
# determine amounts for each line
for each in self._rentals:
# show figures for this rental
result += "\t%s\t%s\n" % (each.getMovie().getTitle(), each.getCharge())
# add footer lines
result += "Amount owed is %s\n" % self.getTotalCharge()
result += "You earned %s frequent renter points" % self.getTotalFrequentRenterPoints()
return result
def htmlStatement(self):
result = "<h1>Rentals for <em>%s</em></h1><p>\n" % self.getName()
for each in self._rentals:
# show figures for this rental
result += "%s: %s</br>\n" % (each.getMovie().getTitle(), each.getCharge())
# add footer lines
result += "<p>You owe <em>%s</em></p>\n" % self.getTotalCharge()
result += "On this rental you earned <em>%s</em> frequent renter points</p>" % self.getTotalFrequentRenterPoints()
return result