Skip to content

Commit fde9ccc

Browse files
committed
Page 45: Replacing the Conditional Logic on Price Code with Polymorphism (added NewReleasePrice.charge and ChildrensPrice.charge and changed Movie.charge to simply delegate to these)
1 parent 99b3fda commit fde9ccc

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

chapter_1.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,7 @@ def price_code=(value)
2020
end
2121

2222
def charge(days_rented)
23-
result = 0
24-
case price_code
25-
when Movie::REGULAR
26-
return @price.charge(days_rented)
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
23+
@price.charge(days_rented)
3424
end
3525

3626
def frequent_renter_points(days_rented)
@@ -47,9 +37,17 @@ def charge(days_rented)
4737
end
4838

4939
class NewReleasePrice
40+
def charge(days_rented)
41+
days_rented * 3
42+
end
5043
end
5144

5245
class ChildrensPrice
46+
def charge(days_rented)
47+
result = 1.5
48+
result += (days_rented - 3) * 1.5 if days_rented > 3
49+
result
50+
end
5351
end
5452

5553
class Rental

0 commit comments

Comments
 (0)