Skip to content

Commit 80cd752

Browse files
committed
Page 13: Decomposing and Redistributing the Statement Method (moving the Customer.amount_for(rental) method to Rental.charge)
1 parent 4c18818 commit 80cd752

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

chapter_1.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ class Rental
1717
def initialize(movie, days_rented)
1818
@movie, @days_rented = movie, days_rented
1919
end
20+
21+
def charge
22+
result = 0
23+
case movie.price_code
24+
when Movie::REGULAR
25+
result += 2
26+
result += (days_rented - 2) * 1.5 if days_rented > 2
27+
when Movie::NEW_RELEASE
28+
result += days_rented * 3
29+
when Movie::CHILDRENS
30+
result += 1.5
31+
result += (days_rented - 3) * 1.5 if days_rented > 3
32+
end
33+
result
34+
end
2035
end
2136

2237

@@ -55,18 +70,7 @@ def statement
5570
end
5671

5772
def amount_for(rental)
58-
result = 0
59-
case rental.movie.price_code
60-
when Movie::REGULAR
61-
result += 2
62-
result += (rental.days_rented - 2) * 1.5 if rental.days_rented > 2
63-
when Movie::NEW_RELEASE
64-
result += rental.days_rented * 3
65-
when Movie::CHILDRENS
66-
result += 1.5
67-
result += (rental.days_rented - 3) * 1.5 if rental.days_rented > 3
68-
end
69-
result
73+
rental.charge
7074
end
7175
end
7276

0 commit comments

Comments
 (0)