Created
February 4, 2015 15:57
-
-
Save Shinpeim/bae986401c10009096c1 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 Bento | |
attr_reader :price | |
attr_writer :seals | |
def initialize(price) | |
@price = price | |
@seals = [] | |
end | |
def discount_ammount | |
sell_price - @price | |
end | |
def sell_price | |
prices = @seals.map do |seal| | |
seal.call(@price) | |
end | |
prices.sort{|a, b| a <=> b}.first | |
end | |
def discount(seals) | |
bento = dup | |
bento.seals = seals | |
bento | |
end | |
def to_s | |
"原価: #{@price}, 値引き後: #{sell_price}, 値引額: #{discount_ammount}" | |
end | |
end | |
半額シール = ->(price){ price / 2 } | |
おつとめ品シール = ->(price) { 200 } # おつとめ品は一律200円 | |
def セール(bento_list, seal_list) | |
bento_list.map do |bento| | |
bento.discount(seal_list) | |
end | |
end | |
弁当リスト = [350, 400, 450, 500, 550].map {|price| Bento.new price} | |
puts "【半額品】" | |
puts セール 弁当リスト, [半額シール] | |
puts "【おつとめ品】" | |
puts セール 弁当リスト, [おつとめ品シール, 半額シール] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sell_price の計算の責務はBentoにあるべきだと思う
地の�文には「いちりつ200円」とあるが、見たところ「一律200円引き」というロジックになっている気がするので地の文に合わせた。地の文をわたしが読み違えているかもしれない。