From d21373035e9afe73c95ad2fce4f4a208ef13542f Mon Sep 17 00:00:00 2001 From: David Underwood Date: Wed, 15 Jul 2026 10:57:09 -0400 Subject: [PATCH] BOGOF discount for ZERO2W --- checkout.rb | 20 +++++++++++++++++--- main.rb | 35 +++++++++++++++++++++++++++++++++++ tests.rb | 18 ++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/checkout.rb b/checkout.rb index dd0116d..64008f8 100644 --- a/checkout.rb +++ b/checkout.rb @@ -21,9 +21,7 @@ class Checkout end def total - @items.reduce(0.00) do |running_total, item| - running_total + AVAILABLE_ITEMS[item] - end + pre_discount_total - current_discount end private @@ -31,4 +29,20 @@ class Checkout def valid_item?(item) return AVAILABLE_ITEMS.keys.include?(item) 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 diff --git a/main.rb b/main.rb index 9129c44..e5111d9 100644 --- a/main.rb +++ b/main.rb @@ -7,3 +7,38 @@ checkout.add(:ZERO2W) puts checkout.items # ["PI5", "ZERO2W"] 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 diff --git a/tests.rb b/tests.rb index a52ddf7..ec09b55 100644 --- a/tests.rb +++ b/tests.rb @@ -34,4 +34,22 @@ class TestCheckout < Minitest::Test def test_that_an_empty_checkout_total_is_zero assert_equal 0.00, @checkout.total 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