diff --git a/bulk_discount.rb b/bulk_discount.rb new file mode 100644 index 0000000..e89d603 --- /dev/null +++ b/bulk_discount.rb @@ -0,0 +1,10 @@ +class BulkDiscount + def initialize(unit_discount:, trigger_quantity: 2) + @unit_discount = unit_discount + @trigger_quantity = trigger_quantity.to_f + end + + def discount_for_quantity(quantity) + quantity >= @trigger_quantity ? @unit_discount * quantity : 0.00 + end +end diff --git a/buy_x_get_one_free_discount.rb b/buy_x_get_one_free_discount.rb index 43cd065..b7deca0 100644 --- a/buy_x_get_one_free_discount.rb +++ b/buy_x_get_one_free_discount.rb @@ -5,6 +5,6 @@ class BuyXGetOneFreeDiscount end def discount_for_quantity(quantity) - return (quantity / @trigger_quantity).floor * @unit_price + (quantity / @trigger_quantity).floor * @unit_price end end diff --git a/test/bulk_discount_test.rb b/test/bulk_discount_test.rb new file mode 100644 index 0000000..3650db7 --- /dev/null +++ b/test/bulk_discount_test.rb @@ -0,0 +1,15 @@ +require "minitest/autorun" + +require 'bulk_discount.rb' + +class BulkDiscountTest < Minitest::Test + def test_appropriate_discount_is_applied + unit_discount = 0.75 + + discount = BulkDiscount.new(unit_discount: unit_discount, trigger_quantity: 4) + + assert_equal 0.00, discount.discount_for_quantity(3) + assert_equal unit_discount * 4, discount.discount_for_quantity(4) + assert_equal unit_discount * 5, discount.discount_for_quantity(5) + end +end diff --git a/test/checkout_test.rb b/test/checkout_test.rb index 398db7a..010b53c 100644 --- a/test/checkout_test.rb +++ b/test/checkout_test.rb @@ -2,6 +2,7 @@ require "minitest/autorun" require "checkout.rb" require "buy_x_get_one_free_discount.rb" +require "bulk_discount.rb" class CheckoutTest < Minitest::Test def setup @@ -22,7 +23,8 @@ class CheckoutTest < Minitest::Test @shop.add_product( Product.new( id: 'PICO2', - price: 4.00 + price: 4.00, + discount: BulkDiscount.new(unit_discount: 0.50, trigger_quantity: 3) ) ) @checkout = Checkout.new(shop: @shop) @@ -98,45 +100,26 @@ class CheckoutTest < Minitest::Test end def test_that_price_is_reduced_for_3_picos - skip - - 3.times do - @checkout.add(:PICO2) # 3.50 * 3 = 10.50 - end + @checkout.add(product_id: 'PICO2', quantity: 3) # 3.50 * 3 = 10.50 assert_equal 10.50, @checkout.total end def test_that_price_is_reduced_for_more_than_3_picos - skip - - 4.times do - @checkout.add(:PICO2) # 3.50 * 4 = 14 - end + @checkout.add(product_id: 'PICO2', quantity: 4) # 3.50 * 4 = 14 assert_equal 14.00, @checkout.total end def test_that_price_is_not_reduced_for_less_than_3_picos - skip - - 2.times do - @checkout.add(:PICO2) # 4.00 * 2 = 8 - end + @checkout.add(product_id: 'PICO2', quantity: 2) # 4.00 * 2 = 8 assert_equal 8.00, @checkout.total end def test_that_both_discounts_apply_at_the_same_time - skip - - 4.times do - @checkout.add(:ZERO2W) # 30.00 with BOGOF - end - - 3.times do - @checkout.add(:PICO2) # 10.50 with bulk discount - end + @checkout.add(product_id: 'ZERO2W', quantity: 4) # 30.00 with BOGOF + @checkout.add(product_id: 'PICO2', quantity: 3) # 10.50 with bulk discount assert_equal (30.00 + 10.50), @checkout.total end