Logic for adding items and validating them

This commit is contained in:
David Underwood
2026-07-15 10:30:28 -04:00
parent 3eebf18fb8
commit 8e28112a8c
5 changed files with 73 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
ruby 4.0.6
+13
View File
@@ -1,5 +1,18 @@
# Ruby Technical Test # 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 ## Initial Thoughts
* Cart can be stored in an array of strings * Cart can be stored in an array of strings
+28
View File
@@ -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
+9
View File
@@ -0,0 +1,9 @@
require "./checkout.rb"
checkout = Checkout.new()
checkout.add(:PI5)
checkout.add(:ZERO2W)
checkout.items # ["PI5", "ZERO2W"]
checkout.total # 75.00
+22
View File
@@ -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