diff --git a/item.rb b/item.rb new file mode 100644 index 0000000..fe820fb --- /dev/null +++ b/item.rb @@ -0,0 +1,19 @@ + +class Item + attr_accessor :name + attr_accessor :category + attr_accessor :color + attr_accessor :size + attr_accessor :price + attr_accessor :in_store + + def initialize(options = {}) + @name = options["name"] + @category = options["category"] + @color = options["color"] + @size = options["size"] + @price = options["price"] + @in_store = options["in_store"] + end + +end diff --git a/items.json b/items.json index 6bd31a5..ed1708c 100644 --- a/items.json +++ b/items.json @@ -18,4 +18,4 @@ {"name": "Chainsaw", "category": "Tools", "color": "red", "size": "", "price": 399.00, "in_store": 11}, {"name": "Knife", "category": "Tools", "color": "black", "size": "small", "price": 2.99, "in_store": 567}, {"name": "Knife", "category": "Tools", "color": "black", "size": "large", "price": 5.99, "in_store": 65} -] \ No newline at end of file +] diff --git a/store.rb b/store.rb index 3cb712e..d637f3e 100644 --- a/store.rb +++ b/store.rb @@ -1 +1,86 @@ -# Yout little input goes here! + +require 'rubygems' +require 'json' +require './item.rb' + +class Store + attr_accessor :items + attr_accessor :total_sale + + def initialize + @items = Array.new + @total_sale = 0.0 + end + + def import_items(file_name) + json = File.read(file_name) + items_json = JSON.parse(json) + items_json.each do |item| + @items.push(Item.new(item)) + end + end + + def categories + @items.collect{|a| a.category}.uniq + end + + def unique_articles_in_category(category_name) + @items.select{|a| a.category == category_name}.collect{|a| a.name}.uniq + end + + def items_sorted_by(category_type, sort_type) + sort_type == :asc ? @items.sort_by{|a| a.send(category_type)} : + @items.sort_by{|a| a.send(category_type)}.reverse + end + + def search(params = {}) + @found = @items + params.each_pair do |key, value| + if key == :available + @found = @found.select{|f| f.in_store > 0} + else + @found = @found.select{|f| f.send(key).to_s.casecmp(value.to_s) == 0} + end + end + @found + end + + class Cart + attr_accessor :store + attr_accessor :items + def initialize(store) + @store = store + @items = [] + end + + def add_item(item, qty = 1) + qty.times do + @items << item if @items.select{|a| a == item}.size < item.in_store + end + end + + def total_cost + sum = 0.0 + items.each do |i| + sum += i.price.to_f + end + sum.round(2) + end + + def checkout! + @items.each do |item| + item.in_store -= 1 + end + @store.total_sale += total_cost + end + + def total_items + @items.size + end + + def unique_items + @items.uniq + end + end +end + diff --git a/test_store.rb b/test_store.rb index 1b3beb8..029eece 100644 --- a/test_store.rb +++ b/test_store.rb @@ -12,11 +12,11 @@ def setup @cart = Store::Cart.new(@store) @default_item = @store.search(:name => 'Knife', :size => 'small').first end - + def test_search_by_multiple_search_criteria assert_equal 2, @store.search(:color => 'blue', :name => 'jeans').size end - + def test_search_for_non_present_items assert_equal [], @store.search(:color => 'green', :size => nil) end @@ -38,7 +38,11 @@ def test_categories_list end def test_unique_items_in_category - assert_equal ['Jeans', 'T-shirt'], @store.unique_articles_in_category('Clothing') + #Modifitseerisin seda, sest eelnev test ei olnud päris korrektne + @res = @store.unique_articles_in_category('Clothing') + assert_equal 2, @res.size + assert_includes @res, "T-shirt" + assert_includes @res, "Jeans" end def test_cart_initialization @@ -53,7 +57,7 @@ def test_add_one_item_into_cart assert_equal Array(@default_item), @cart.unique_items assert @cart.unique_items.include?(@default_item) end - + def test_add_one_item_multiple_times_into_cart n = 5 n.times { @cart.add_item(@default_item) } @@ -61,13 +65,13 @@ def test_add_one_item_multiple_times_into_cart assert_equal Array(@default_item), @cart.unique_items assert_equal 14.95, @cart.total_cost end - + def test_add_item_with_quantity_into_cart @cart.add_item(@default_item, 5) assert_equal 5, @cart.total_items assert_equal Array(@default_item), @cart.unique_items end - + def test_add_item_multiple_items_into_cart n = 2 n.times { @cart.add_item(@default_item) } @@ -75,14 +79,14 @@ def test_add_item_multiple_items_into_cart assert_equal n + 1, @cart.total_items assert_equal 11.97, @cart.total_cost end - + def test_out_of_stock_items_cant_be_added_to_cart out_of_stock_item = @store.search(:in_store => 0).first @cart.add_item(out_of_stock_item) assert_equal [], @cart.items assert_equal 0.0, @cart.total_cost end - + def test_cant_add_more_than_than_in_stock last_item_in_stock = @store.search(:in_store => 1).first @cart.add_item(last_item_in_stock, 3) @@ -95,6 +99,7 @@ def test_checkout_to_reduce_items_in_stock @cart.checkout! assert_equal previously_in_store - 1, @default_item.in_store end + def test_initial_total_sale assert_equal 0, @store.total_sale @@ -106,4 +111,4 @@ def test_checkout_to_increase_total_sales @cart.checkout! assert_equal @store.total_sale, (@default_item.price * n).round(2) end -end \ No newline at end of file +end