BOGOF discount for ZERO2W

This commit is contained in:
David Underwood
2026-07-15 10:57:09 -04:00
parent dadfeed768
commit d21373035e
3 changed files with 70 additions and 3 deletions
+17 -3
View File
@@ -21,9 +21,7 @@ class Checkout
end end
def total def total
@items.reduce(0.00) do |running_total, item| pre_discount_total - current_discount
running_total + AVAILABLE_ITEMS[item]
end
end end
private private
@@ -31,4 +29,20 @@ class Checkout
def valid_item?(item) def valid_item?(item)
return AVAILABLE_ITEMS.keys.include?(item) return AVAILABLE_ITEMS.keys.include?(item)
end end
def current_discount
# BOGOF ZERO2W
discount = (@items.count(:ZERO2W) / 2.0).floor * AVAILABLE_ITEMS[:ZERO2W]
# TODO: Bulk PICO2
return discount
end
def pre_discount_total
@items.reduce(0.00) do |running_total, item|
running_total + AVAILABLE_ITEMS[item]
end
end
end end
+35
View File
@@ -7,3 +7,38 @@ checkout.add(:ZERO2W)
puts checkout.items # ["PI5", "ZERO2W"] puts checkout.items # ["PI5", "ZERO2W"]
puts checkout.total # 75.00 puts checkout.total # 75.00
checkout = Checkout.new()
checkout.add(:ZERO2W)
checkout.add(:ZERO2W)
puts checkout.total # 15.00
checkout = Checkout.new()
checkout.add(:ZERO2W)
checkout.add(:ZERO2W)
checkout.add(:ZERO2W)
puts checkout.total # 30.00
checkout = Checkout.new()
checkout.add(:PICO2)
checkout.add(:PICO2)
checkout.add(:PICO2)
puts checkout.total # 10.50
checkout = Checkout.new()
checkout.add(:PI5)
checkout.add(:ZERO2W)
checkout.add(:ZERO2W)
checkout.add(:PICO2)
checkout.add(:PICO2)
checkout.add(:PICO2)
checkout.add(:PICO2)
puts checkout.total # 89.00
+18
View File
@@ -34,4 +34,22 @@ class TestCheckout < Minitest::Test
def test_that_an_empty_checkout_total_is_zero def test_that_an_empty_checkout_total_is_zero
assert_equal 0.00, @checkout.total assert_equal 0.00, @checkout.total
end end
# Discounts
def test_that_bogof_applied
4.times do
@checkout.add(:ZERO2W)
end
assert_equal 30.00, @checkout.total
end
def test_that_bogof_applied_with_odd_number
5.times do
@checkout.add(:ZERO2W)
end
assert_equal 45.00, @checkout.total
end
end end