63 lines
979 B
Ruby
63 lines
979 B
Ruby
class Checkout
|
|
class InvalidItem < StandardError; end
|
|
|
|
AVAILABLE_ITEMS = {
|
|
PI5: 60.00,
|
|
ZERO2W: 15.00,
|
|
PICO2: 4.00
|
|
}
|
|
|
|
def initialize
|
|
@items = []
|
|
end
|
|
|
|
def add(item)
|
|
raise InvalidItem unless valid_item?(item)
|
|
@items.push item
|
|
end
|
|
|
|
def remove(item)
|
|
if index = @items.index(item)
|
|
@items.delete_at(index)
|
|
end
|
|
|
|
@items
|
|
end
|
|
|
|
def items
|
|
@items
|
|
end
|
|
|
|
def total
|
|
pre_discount_total - current_discount
|
|
end
|
|
|
|
def clear
|
|
@items = []
|
|
end
|
|
|
|
private
|
|
|
|
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]
|
|
|
|
# Bulk PICO2
|
|
if(items.count(:PICO2) >= 3)
|
|
discount += items.count(:PICO2) * 0.50
|
|
end
|
|
|
|
return discount
|
|
end
|
|
|
|
def pre_discount_total
|
|
@items.reduce(0.00) do |running_total, item|
|
|
running_total + AVAILABLE_ITEMS[item]
|
|
end
|
|
end
|
|
end
|