From 0a241e53fb50d1d3a42d4dbc3d22a21f629c9187 Mon Sep 17 00:00:00 2001 From: OliverSoop Date: Mon, 11 Mar 2013 17:23:51 +0200 Subject: [PATCH 1/4] Store implemented --- items.json | 2 +- store.rb | 129 +++++++++++++++++++++++++++++++++++++++++++++++++- test_store.rb | 24 ++++++---- 3 files changed, 144 insertions(+), 11 deletions(-) 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..764a719 100644 --- a/store.rb +++ b/store.rb @@ -1 +1,128 @@ -# Yout little input goes here! + +require 'rubygems' +require 'json' +require 'pp' +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(:name => item['name'], + :category => item['category'], + :color => item['color'], + :size => item['size'], + :price => item['price'], + :in_store => item['in_store'])) + 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 + if params.has_key?(:name) + @found = @found.select{|f| f.name == params[:name] || params[:name] && f.name.casecmp(params[:name]) == 0} + end + if params.has_key?(:category) + @found = @found.select{|f| f.category == params[:category] || + params[:category] && f.category.casecmp(params[:category]) == 0} + end + if params.has_key?(:color) + @found = @found.select{|f| f.color == params[:color] || + params[:color] && f.color.casecmp(params[:color]) == 0} + end + if params.has_key?(:size) + @found = @found.select{|f| f.size == params[:size] || + params[:size] && f.size.casecmp(params[:size]) == 0} + end + if params.has_key?(:price) + @found = @found.select{|f| f.price == params[:price] || + params[:price] && f.price.casecmp(params[:price]) == 0} + end + if params.has_key?(:in_store) + @found = @found.select{|f| f.in_store == params[:in_store]} + end + @found + end + + class Cart + attr_accessor :store + attr_accessor :total_cost + attr_accessor :items + def initialize(store) + @store = store + @items = Array.new + @total_cost = 0.00 + end + + def add_item(item) + + end + + def add_item(item, quantity = 1) + i = 0 + result = @store.items.select{|a| a.name == item.name && a.color == item.color && a.size == item.size && a.price == item.price} + if result.size > 0 + store_item = result[0] + current_quantity = store_item.in_store + while i < quantity + if current_quantity < 1 + break + end + @items.push(item) + @total_cost += item.price + @total_cost = @total_cost.round(2) + i+=1 + current_quantity -=1 + end + end + end + + def checkout! + @items.each do |item| + result = @store.items.select{|a| a.name == item.name && a.color == item.color && a.size == item.size && a.price == item.price} + result[0].in_store -= 1 + end + @store.total_sale += @total_cost + end + + def total_items + @items.size + end + + def unique_items + @items.uniq{|a| a.name && a.category && a.color && a.size && a.price} + end + end +end + +#r = Store.new +#r.import_items('items.json') + +class Array + def search + inject(0.0) { |result, el| result + el } + end +end + diff --git a/test_store.rb b/test_store.rb index 1b3beb8..48bf0b7 100644 --- a/test_store.rb +++ b/test_store.rb @@ -12,11 +12,12 @@ 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 +39,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 +58,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 +66,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 +80,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 +100,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 +112,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 From 9ff33b7c2239bd815d4c612918322769e74f1e13 Mon Sep 17 00:00:00 2001 From: OliverSoop Date: Mon, 11 Mar 2013 17:26:09 +0200 Subject: [PATCH 2/4] Minor fixes --- store.rb | 9 --------- 1 file changed, 9 deletions(-) diff --git a/store.rb b/store.rb index 764a719..89f368b 100644 --- a/store.rb +++ b/store.rb @@ -117,12 +117,3 @@ def unique_items end end -#r = Store.new -#r.import_items('items.json') - -class Array - def search - inject(0.0) { |result, el| result + el } - end -end - From b1189083de2f0740d33411d35fd4434c68518962 Mon Sep 17 00:00:00 2001 From: OliverSoop Date: Mon, 11 Mar 2013 20:54:35 +0200 Subject: [PATCH 3/4] Improvements to the code according to feedback --- store.rb | 73 ++++++++++++++------------------------------------- test_store.rb | 1 - 2 files changed, 20 insertions(+), 54 deletions(-) diff --git a/store.rb b/store.rb index 89f368b..d637f3e 100644 --- a/store.rb +++ b/store.rb @@ -1,7 +1,6 @@ require 'rubygems' require 'json' -require 'pp' require './item.rb' class Store @@ -17,12 +16,7 @@ def import_items(file_name) json = File.read(file_name) items_json = JSON.parse(json) items_json.each do |item| - @items.push(Item.new(:name => item['name'], - :category => item['category'], - :color => item['color'], - :size => item['size'], - :price => item['price'], - :in_store => item['in_store'])) + @items.push(Item.new(item)) end end @@ -41,70 +35,43 @@ def items_sorted_by(category_type, sort_type) def search(params = {}) @found = @items - if params.has_key?(:name) - @found = @found.select{|f| f.name == params[:name] || params[:name] && f.name.casecmp(params[:name]) == 0} - end - if params.has_key?(:category) - @found = @found.select{|f| f.category == params[:category] || - params[:category] && f.category.casecmp(params[:category]) == 0} - end - if params.has_key?(:color) - @found = @found.select{|f| f.color == params[:color] || - params[:color] && f.color.casecmp(params[:color]) == 0} - end - if params.has_key?(:size) - @found = @found.select{|f| f.size == params[:size] || - params[:size] && f.size.casecmp(params[:size]) == 0} - end - if params.has_key?(:price) - @found = @found.select{|f| f.price == params[:price] || - params[:price] && f.price.casecmp(params[:price]) == 0} - end - if params.has_key?(:in_store) - @found = @found.select{|f| f.in_store == params[:in_store]} + 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 :total_cost attr_accessor :items def initialize(store) @store = store - @items = Array.new - @total_cost = 0.00 + @items = [] end - def add_item(item) - + def add_item(item, qty = 1) + qty.times do + @items << item if @items.select{|a| a == item}.size < item.in_store + end end - def add_item(item, quantity = 1) - i = 0 - result = @store.items.select{|a| a.name == item.name && a.color == item.color && a.size == item.size && a.price == item.price} - if result.size > 0 - store_item = result[0] - current_quantity = store_item.in_store - while i < quantity - if current_quantity < 1 - break - end - @items.push(item) - @total_cost += item.price - @total_cost = @total_cost.round(2) - i+=1 - current_quantity -=1 - 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| - result = @store.items.select{|a| a.name == item.name && a.color == item.color && a.size == item.size && a.price == item.price} - result[0].in_store -= 1 + item.in_store -= 1 end - @store.total_sale += @total_cost + @store.total_sale += total_cost end def total_items @@ -112,7 +79,7 @@ def total_items end def unique_items - @items.uniq{|a| a.name && a.category && a.color && a.size && a.price} + @items.uniq end end end diff --git a/test_store.rb b/test_store.rb index 48bf0b7..029eece 100644 --- a/test_store.rb +++ b/test_store.rb @@ -13,7 +13,6 @@ def setup @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 From 173b687002cbc0223f0e303c987ecbbcc0ed90f2 Mon Sep 17 00:00:00 2001 From: OliverSoop Date: Sun, 24 Mar 2013 17:51:51 +0200 Subject: [PATCH 4/4] Added item.rb --- item.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 item.rb 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