BOGOF discount for ZERO2W
This commit is contained in:
+17
-3
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user