Created
May 5, 2016 20:34
-
-
Save foucist/eb81f25551d084cb0cc66dd7fa87469f to your computer and use it in GitHub Desktop.
This file contains 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 Price | |
def pricer(quantity, item) | |
end | |
case | |
when setpoint | |
SetPrice | |
when bulkpoint | |
BulkPrice | |
end | |
end | |
class SetPrice | |
end | |
class BulkPrice | |
end | |
class Pricing | |
def initialize( | |
lookup | |
end | |
def to_a | |
.. | |
end | |
end | |
class Item | |
#price & quantity & set points/bulk points.. | |
# base_price : 1.50 | |
# 3 for 2 : 3 | |
# pricing | |
# Item | types | amount | bulk_price | |
# apples | set | 3 | 1 | |
# apples | set | 1 | 1.5 | |
# beef | bulk | 1 | 2 | |
# beef | bulk | 5 | 9 | |
end | |
class CheckoutItem < Item | |
def initialize(item, quantity) | |
@item = item | |
@quantiy = quantity | |
end | |
def price | |
pricing = Pricing.new(@item) #=> [ SetPrice.new(1, 1.5), SetPrice.new(3, 1) ] | |
result = 0 | |
pricing.sort.each do |pricing| | |
if quantity > pricing.quantity | |
result = quantity * pricing.price | |
quantity -= pricing.quantity | |
else | |
next | |
end | |
end | |
result | |
end | |
end | |
class Checkout | |
# takes item & quantity, returns price? | |
def initialize() | |
@items = {} | |
end | |
def add_item(item, quantity) | |
@items[item] += quantity | |
end | |
def final_price | |
@items.map do |item, quantity| | |
CheckoutItem.new(item,quantity).price | |
end.inject(&:+) | |
end | |
end | |
@checkout = Checkout.new | |
@checkout.add_item(apple: 1) | |
@checkout.final_price | |
def test_final_price_is_zero | |
@checkout = Checkout.new | |
assert 0, @checkout.final_price | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment