diff options
| author | mo khan <mo@mokhan.ca> | 2014-05-26 16:30:30 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-05-26 16:30:30 -0600 |
| commit | 951723841cb3b8cb40e6e7a7c2f4835f1f06c8b8 (patch) | |
| tree | 6f61f00300d10418b5df850a052e34fdacdc31a6 | |
initial commit.
| -rw-r--r-- | Gemfile | 3 | ||||
| -rw-r--r-- | Gemfile.lock | 18 | ||||
| -rw-r--r-- | lib/refactoring.rb | 66 |
3 files changed, 87 insertions, 0 deletions
@@ -0,0 +1,3 @@ +source "https://rubygems.org" + +gem "rspec" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..b3a6705 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,18 @@ +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.2.5) + rspec (2.14.1) + rspec-core (~> 2.14.0) + rspec-expectations (~> 2.14.0) + rspec-mocks (~> 2.14.0) + rspec-core (2.14.8) + rspec-expectations (2.14.5) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.14.6) + +PLATFORMS + ruby + +DEPENDENCIES + rspec diff --git a/lib/refactoring.rb b/lib/refactoring.rb new file mode 100644 index 0000000..52bfafb --- /dev/null +++ b/lib/refactoring.rb @@ -0,0 +1,66 @@ +class Movie + REGULAR = 0 + NEW_RELEASE = 1 + CHILDRENS = 2 + + attr_reader :title + attr_accessor :price_code + + def initialize(title, price_code) + @title, @price_code = title, price_code + end +end + +class Rental + attr_reader :movie, :days_rented + + def initialize(movie, days_rented) + @movie, @days_rented = movie, days_rented + end +end + +class Customer + attr_reader :name + + def initialize(name) + @name = name + @rentals = [] + end + + def add_rental(arg) + @rentals << arg + end + + def statement + total_amount, frequent_renter_points = 0, 0 + result = "Rental Record for #{@name}\n" + @rentals.each do |element| + this_amount = 0 + + # determine amounts for each line + case element.movie.price_code + when Movie::REGULAR + this_amount += 2 + this_amount += (element.days_rented - 2) * 1.5 if element.days_rented > 2 + when Movie::NEW_RELEASE + this_amount += element.days_rented * 3 + when Movie::CHILDRENS + this_amount += 1.5 + this_amount += (element.days_rented - 3) * 1.5 if element.days_rented > 3 + end + + # add frequent renter points + frequent_renter_points += 1 + # add bonus for a two day new release rental + if element.movie.price_code == Movie.NEW_RELEASE && element.days_rented > 1 + frequent_renter_points += 1 + end + # show figures for this rental + result += "\t" + element.movie.title + "\t" + this_amount.to_s + "\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
\ No newline at end of file |
