Created
June 25, 2014 15:09
-
-
Save kevin-cantwell/ba63e5cb3bd0c37857f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BulkPrice | |
| attr_accessor :quantity, :price | |
| def initialize(quantity, price) | |
| @quantity = quantity | |
| @price = price | |
| end | |
| def price_per_item | |
| price / quantity | |
| end | |
| end | |
| class Pricing | |
| def initialize(price, bulk_price=nil) | |
| @price = price | |
| @bulk_price = bulk_price | |
| end | |
| def total(quantity) | |
| if @bulk_price | |
| remainder = (quantity % @bulk_price.quantity) | |
| (@bulk_price.price_per_item * (quantity - remainder) + (@price * remainder)) | |
| else | |
| @price * quantity | |
| end | |
| end | |
| end | |
| class Terminal | |
| PRODUCT_PRICINGS = { | |
| "A" => Pricing.new(2.00, BulkPrice.new(4, 7.00)), | |
| "B" => Pricing.new(12.00), | |
| "C" => Pricing.new(1.25, BulkPrice.new(6, 6.00)), | |
| "D" => Pricing.new(0.15) | |
| } | |
| def set_pricing(product_pricings) | |
| @product_pricings = product_pricings | |
| end | |
| def scan(product_code) | |
| quantities[product_code] = (quantities[product_code] || 0 ) + 1 | |
| end | |
| def total | |
| total = 0 | |
| quantities.each do |product_code, quantity| | |
| pricing = @product_pricings[product_code] | |
| total += pricing.total(quantity) | |
| end | |
| total | |
| end | |
| private | |
| def quantities | |
| @quantities ||= {} | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment