Skip to content

Instantly share code, notes, and snippets.

@ecuageo
Created October 29, 2015 22:39
Show Gist options
  • Save ecuageo/a83c427d7eef52769d8a to your computer and use it in GitHub Desktop.
Save ecuageo/a83c427d7eef52769d8a to your computer and use it in GitHub Desktop.
class Movie
CHILDRENS = 2
REGULAR = 0
NEW_RELEASE = 1
attr_reader :title
attr_accessor :price_code
def initialize(title, price_code)
@title = title
@price_code = price_code
end
end
class Rental
attr_reader :movie, :days_rented
def initialize(movie, days_rented)
@movie = movie
@days_rented = days_rented
end
def self.for(movie, days_rented)
case movie.price_code
when Movie::REGULAR
self
when Movie::NEW_RELEASE
NewReleaseRental
when Movie::CHILDRENS
ChildrensRental
end.new(movie, days_rented)
end
def amount
2 + extended_amount
end
def extended_amount
if days_rented > 2
(days_rented - 2) * 1.5
else
0
end
end
def frequent_renter_points
1
end
end
class ChildrensRental < Rental
def amount
1.5 + extended_amount
end
def extended_amount
if (days_rented > 3)
(days_rented - 3) * 1.5
else
0
end
end
end
class NewReleaseRental < Rental
def amount
days_rented * 3
end
def extended_amount
0
end
def frequent_renter_points
if days_rented > 1
2
else
1
end
end
end
class Customer
attr_reader :name, :rentals
def initialize(name)
@name = name
@rentals = []
end
def add_rental(rental)
@rentals << rental
end
def statement(statement_formatter: TextStatement)
statement_formatter.print(self)
end
def total
rentals.map(&:amount).reduce(:+)
end
def frequent_renter_points
rentals.map(&:frequent_renter_points).reduce(:+)
end
end
class TextStatement
def self.print(customer)
result = "Rental Record for #{customer.name}\n"
customer.rentals.each do |rental|
result << "\t#{rental.movie.title}\t#{rental.amount}\n"
end
result << "Amount owed is #{customer.total}\n"
result << "You earned #{customer.frequent_renter_points} frequent renter points"
result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment