Last active
November 24, 2015 01:08
-
-
Save fuadsaud/088bf58454c8be3c1a80 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 CheckOut | |
def initialize(rules) | |
@rules = rules | |
@goods = [] | |
end | |
def scan(good) | |
goods << good unless good.nil? | |
end | |
def total | |
rules.map { |r| r.call(goods) }.reduce(0, &:+) | |
end | |
private | |
attr_reader :rules, :goods | |
end | |
class ModPrice | |
def initialize(sku:, regular_price:, quantity: 1, special_price: regular_price) | |
@sku = sku | |
@regular_price = regular_price | |
@quantity = quantity | |
@special_price = special_price | |
end | |
def call(goods) | |
count = goods.count { |g| g == sku } | |
count | |
.divmod(quantity) | |
.zip([special_price, regular_price]) | |
.map { |(q, p)| q * p } | |
.reduce(&:+) | |
end | |
private | |
attr_reader :sku, :quantity, :regular_price, :special_price | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment