From e5707839357afa7e955c1ec330ce54971706c4db Mon Sep 17 00:00:00 2001 From: David Underwood Date: Tue, 21 Jul 2026 09:48:32 -0400 Subject: [PATCH] Add Shop class to keep track of available products --- shop.rb | 22 ++++++++++++++++++++++ test/shop_test.rb | 25 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 shop.rb create mode 100644 test/shop_test.rb diff --git a/shop.rb b/shop.rb new file mode 100644 index 0000000..4d29ff9 --- /dev/null +++ b/shop.rb @@ -0,0 +1,22 @@ +class Shop + class DuplicateProductIdError < StandardError; end + def initialize + @products = [] + end + + def products + @products + end + + def add_product(product) + raise DuplicateProductIdError if duplicate_id?(product.id) + + @products.push(product) + end + + private + + def duplicate_id?(id) + @products.map(&:id).include?(id) + end +end diff --git a/test/shop_test.rb b/test/shop_test.rb new file mode 100644 index 0000000..e9e3dc3 --- /dev/null +++ b/test/shop_test.rb @@ -0,0 +1,25 @@ +require "minitest/autorun" + +require "shop.rb" +require "product.rb" + +class ShopTest < Minitest::Test + def setup + @shop = Shop.new + end + + def test_adding_a_product + pico = Product.new(id: 'PICO2', price: 4.00) + @shop.add_product(pico) + + assert_equal [pico], @shop.products + end + + def test_duplicate_product_id_raises_an_error + @shop.add_product(Product.new(id: 'PICO2')) + + assert_raises Shop::DuplicateProductIdError do + @shop.add_product(Product.new(id: 'PICO2')) + end + end +end