Logic for adding items and validating them
This commit is contained in:
@@ -0,0 +1 @@
|
||||
ruby 4.0.6
|
||||
@@ -1,5 +1,18 @@
|
||||
# Ruby Technical Test
|
||||
|
||||
## Dependencies
|
||||
|
||||
* Ruby 4.0.6
|
||||
* Minitest 6.0.6
|
||||
|
||||
## Instructions
|
||||
|
||||
Checkout class is in `checkout.rb`
|
||||
|
||||
Run tests with `ruby -Ilib:test tests.rb`
|
||||
|
||||
`main.rb` is a scratchpad
|
||||
|
||||
## Initial Thoughts
|
||||
|
||||
* Cart can be stored in an array of strings
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
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 items
|
||||
@items
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_item(item)
|
||||
return AVAILABLE_ITEMS.keys.include?(item)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,9 @@
|
||||
require "./checkout.rb"
|
||||
|
||||
checkout = Checkout.new()
|
||||
|
||||
checkout.add(:PI5)
|
||||
checkout.add(:ZERO2W)
|
||||
|
||||
checkout.items # ["PI5", "ZERO2W"]
|
||||
checkout.total # 75.00
|
||||
@@ -0,0 +1,22 @@
|
||||
require "minitest/autorun"
|
||||
|
||||
require "./checkout.rb"
|
||||
|
||||
class TestCheckout < Minitest::Test
|
||||
def setup
|
||||
@checkout = Checkout.new
|
||||
end
|
||||
|
||||
def test_that_unknown_items_cannot_be_added
|
||||
assert_raises Checkout::InvalidItem do
|
||||
@checkout.add(:foo)
|
||||
end
|
||||
end
|
||||
|
||||
def test_that_allowed_items_are_added
|
||||
@checkout.add(:PI5)
|
||||
@checkout.add(:ZERO2W)
|
||||
|
||||
assert_equal [:PI5, :ZERO2W].sort, @checkout.items.sort
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user