Add BulkDiscount

This commit is contained in:
David Underwood
2026-07-21 12:32:51 -04:00
parent 554ce58cbf
commit 60d132a821
4 changed files with 34 additions and 26 deletions
+10
View File
@@ -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
+1 -1
View File
@@ -5,6 +5,6 @@ class BuyXGetOneFreeDiscount
end end
def discount_for_quantity(quantity) def discount_for_quantity(quantity)
return (quantity / @trigger_quantity).floor * @unit_price (quantity / @trigger_quantity).floor * @unit_price
end end
end end
+15
View File
@@ -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
+8 -25
View File
@@ -2,6 +2,7 @@ require "minitest/autorun"
require "checkout.rb" require "checkout.rb"
require "buy_x_get_one_free_discount.rb" require "buy_x_get_one_free_discount.rb"
require "bulk_discount.rb"
class CheckoutTest < Minitest::Test class CheckoutTest < Minitest::Test
def setup def setup
@@ -22,7 +23,8 @@ class CheckoutTest < Minitest::Test
@shop.add_product( @shop.add_product(
Product.new( Product.new(
id: 'PICO2', id: 'PICO2',
price: 4.00 price: 4.00,
discount: BulkDiscount.new(unit_discount: 0.50, trigger_quantity: 3)
) )
) )
@checkout = Checkout.new(shop: @shop) @checkout = Checkout.new(shop: @shop)
@@ -98,45 +100,26 @@ class CheckoutTest < Minitest::Test
end end
def test_that_price_is_reduced_for_3_picos def test_that_price_is_reduced_for_3_picos
skip @checkout.add(product_id: 'PICO2', quantity: 3) # 3.50 * 3 = 10.50
3.times do
@checkout.add(:PICO2) # 3.50 * 3 = 10.50
end
assert_equal 10.50, @checkout.total assert_equal 10.50, @checkout.total
end end
def test_that_price_is_reduced_for_more_than_3_picos def test_that_price_is_reduced_for_more_than_3_picos
skip @checkout.add(product_id: 'PICO2', quantity: 4) # 3.50 * 4 = 14
4.times do
@checkout.add(:PICO2) # 3.50 * 4 = 14
end
assert_equal 14.00, @checkout.total assert_equal 14.00, @checkout.total
end end
def test_that_price_is_not_reduced_for_less_than_3_picos def test_that_price_is_not_reduced_for_less_than_3_picos
skip @checkout.add(product_id: 'PICO2', quantity: 2) # 4.00 * 2 = 8
2.times do
@checkout.add(:PICO2) # 4.00 * 2 = 8
end
assert_equal 8.00, @checkout.total assert_equal 8.00, @checkout.total
end end
def test_that_both_discounts_apply_at_the_same_time def test_that_both_discounts_apply_at_the_same_time
skip @checkout.add(product_id: 'ZERO2W', quantity: 4) # 30.00 with BOGOF
@checkout.add(product_id: 'PICO2', quantity: 3) # 10.50 with bulk discount
4.times do
@checkout.add(:ZERO2W) # 30.00 with BOGOF
end
3.times do
@checkout.add(:PICO2) # 10.50 with bulk discount
end
assert_equal (30.00 + 10.50), @checkout.total assert_equal (30.00 + 10.50), @checkout.total
end end