0

Decorator Pattern アプローチを使用してこの問題を解決しようとしています。これこれを参考にしました。作成したアイテムに適用される税を合計しようとしていますが、ラップされていない最後のオブジェクトに適用される税しか取得できません。

module SalesTaxDeco

  class Item

    attr_accessor :price

    def initialize(price)
      @price = price
    end

    def sales_tax
    end

  end

  class SalesTax

    def price
      @component.price
    end

    def initialize(component)
      @component = component
    end

    def sales_tax
      ((@component.price * 0.10)*(1/0.05).ceil)/(1/0.05)
    end
  end

  class ImportDuty

    def price
      @component.price
    end

    def initialize(component)
      @component = component
    end

    def sales_tax
      ((@component.price * 0.5)*(1/0.05).ceil)/(1/0.05)
    end
  end
end

としてアイテムを作成しました。

def test_imported_perfume_is_taxed
    item = Item.new 47.50
    assert_equal 7.15, SalesTax.new(ImportDuty.new(item)).sales_tax
end

しかし、私は答えとして4.75しか得ていません。何を与える?どこが間違っていますか?

ありがとう。

4

1 に答える 1