Skip to content

Instantly share code, notes, and snippets.

@ecuageo
Created October 29, 2015 12:16
Show Gist options
  • Save ecuageo/6173d28bee1cd873d82a to your computer and use it in GitHub Desktop.
Save ecuageo/6173d28bee1cd873d82a 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
end
class Customer
attr_reader :name, :rentals
def initialize(name)
@name = name
@rentals = []
end
def add_rental(rental)
@rentals << rental
end
def statement
total_amount = 0
frequent_renter_points = 0
result = "Rental Record for #{name}\n"
rentals.each do |rental|
this_amount = 0
# determine amounts for each line
case rental.movie.price_code
when Movie::REGULAR
this_amount += 2;
if rental.days_rented > 2
this_amount += (rental.days_rented - 2) * 1.5
end
when Movie::NEW_RELEASE
this_amount += rental.days_rented * 3
when Movie::CHILDRENS
this_amount += 1.5
if (rental.days_rented > 3)
this_amount += (rental.days_rented - 3) * 1.5
end
end
# add frequent renter points
frequent_renter_points += 1
# add bonus for a two day new release rental
if rental.movie.price_code == Movie::NEW_RELEASE && rental.days_rented > 1
frequent_renter_points += 1
end
# show figures for this rental
result << "\t#{rental.movie.title}\t#{this_amount}\n"
total_amount += this_amount
end
# add footer lines
result << "Amount owed is #{total_amount}\n"
result << "You earned #{frequent_renter_points} frequent renter points"
result
end
end
movies = [["Jaws", Movie::REGULAR], ["Frozen", Movie::CHILDRENS], ["Spectre", Movie::NEW_RELEASE], ["Ghost Busters", Movie::REGULAR]].map do |title, price_code|
Movie.new(title, price_code)
end
customer = Customer.new("George")
customer.add_rental(Rental.new(movies[0], 3))
customer.add_rental(Rental.new(movies[1], 2))
customer.add_rental(Rental.new(movies[2], 5))
puts customer.statement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment