Skip to content

Instantly share code, notes, and snippets.

@thiagofm
Created December 2, 2014 11:06
Show Gist options
  • Save thiagofm/9a028a15a64669810b4c to your computer and use it in GitHub Desktop.
Save thiagofm/9a028a15a64669810b4c to your computer and use it in GitHub Desktop.
class CheckOut
attr_accessor :total
def initialize(rules)
@rules = rules
@total = 0
@amount_of_units = Hash.new(0)
end
def scan(item)
update_amount_of_units item
@total += find_price item
end
private
def update_amount_of_units item
@amount_of_units[item] += 1
end
def find_price item
if has_special_price?(item) && fits_the_special_price_criteria?(item)
@rules[item][:special_price][:price] - (@rules[item][:special_price][:amount]-1) * @rules[item][:unit_price]
else
@rules[item][:unit_price]
end
end
def fits_the_special_price_criteria? item
(@amount_of_units[item] % @rules[item][:special_price][:amount] == 0) if has_special_price? item
end
def has_special_price? item
@rules[item].has_key? :special_price
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment