From 9e39cbfd5d722e230ea27655d1b4a090626f2124 Mon Sep 17 00:00:00 2001 From: irhete Date: Mon, 4 Mar 2013 17:06:37 +0200 Subject: [PATCH 1/4] Update test_store.rb remove json dependency --- test_store.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test_store.rb b/test_store.rb index 1b3beb8..53b337c 100644 --- a/test_store.rb +++ b/test_store.rb @@ -1,5 +1,4 @@ require 'rubygems' -require 'json' require_relative 'store' @@ -106,4 +105,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 0b8df7cc3738f7f59ed5a1202c125a70b80306b8 Mon Sep 17 00:00:00 2001 From: irhete Date: Tue, 12 Mar 2013 21:12:16 +0200 Subject: [PATCH 2/4] first 3 tests --- store.rb | 40 ++++++++++++++++++++++++++++++++++++++++ test_store.rb | 5 +++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/store.rb b/store.rb index 3cb712e..000aa89 100644 --- a/store.rb +++ b/store.rb @@ -1 +1,41 @@ # Yout little input goes here! + +require 'json' + +class Store +@@items + def import_items(file_name) + json = File.read(file_name) + @@items = JSON.parse(json, :symbolize_names => true) + end + def search(keywords) + searchable_items = @@items + keywords.each do |kw_key, kw_value| + if kw_key == :available && kw_value == true + searchable_items = searchable_items.select{|item| item[:in_store] > 0} + elsif kw_key == :available + searchable_items = searchable_items.select{|item| item[:in_store] <= 0} + else + searchable_items = searchable_items.select{|item| item[kw_key].downcase == kw_value.to_s.downcase} + end + end + return searchable_items + end +end + + + + +class Cart +@@store + def initialize(store) + @@store = store + end +end + + +class Hash + def method_missing(n) + self[n.to_s.to_sym] + end +end diff --git a/test_store.rb b/test_store.rb index 53b337c..c8464fb 100644 --- a/test_store.rb +++ b/test_store.rb @@ -19,11 +19,11 @@ def test_search_by_multiple_search_criteria def test_search_for_non_present_items assert_equal [], @store.search(:color => 'green', :size => nil) end - + def test_search_with_availability_flag assert_equal 'A lamp', @store.search(:color => 'green', :category => 'furniture', :available => true).first.name end - +=begin def test_sort_items_by_price_cheapest_first assert_equal 1.99, @store.items_sorted_by(:price, :asc).first.price end @@ -105,4 +105,5 @@ def test_checkout_to_increase_total_sales @cart.checkout! assert_equal @store.total_sale, (@default_item.price * n).round(2) end +=end end From 58d6fc401ba9e6bf1cb7ee62e425d854bcc1c54c Mon Sep 17 00:00:00 2001 From: irhete Date: Wed, 13 Mar 2013 19:57:04 +0200 Subject: [PATCH 3/4] all tests, but unique_items not correct format --- store.rb | 93 +++++++++++++++++++++++++++++++++++++++++++++------ test_store.rb | 20 +++++------ 2 files changed, 91 insertions(+), 22 deletions(-) diff --git a/store.rb b/store.rb index 000aa89..c45329a 100644 --- a/store.rb +++ b/store.rb @@ -4,38 +4,109 @@ class Store @@items +attr_accessor :total_sale + + def initialize + @total_sale = 0 + end + def import_items(file_name) json = File.read(file_name) @@items = JSON.parse(json, :symbolize_names => true) end + def search(keywords) searchable_items = @@items keywords.each do |kw_key, kw_value| - if kw_key == :available && kw_value == true - searchable_items = searchable_items.select{|item| item[:in_store] > 0} - elsif kw_key == :available - searchable_items = searchable_items.select{|item| item[:in_store] <= 0} + if kw_key == :available + searchable_items = searchable_items.select{|item| item.available?} else - searchable_items = searchable_items.select{|item| item[kw_key].downcase == kw_value.to_s.downcase} + searchable_items = searchable_items.select{|item| item[kw_key].to_s.downcase == kw_value.to_s.downcase} end end return searchable_items end -end + def items_sorted_by(attribute, order) + if order == :asc + return @@items.sort {|x,y| x[attribute] <=> y[attribute]} + else + return @@items.sort {|x,y| y[attribute] <=> x[attribute]} + end + end + + def categories + return @@items.map{ |item| item[:category] }.flatten.uniq + end + + def unique_articles_in_category(category) + articles = [] + @@items.select {|item| item[:category] == category}.map{ |item| item[:name] }.flatten.uniq.sort + end + + + class Cart + attr_accessor :store + attr_accessor :items + + def initialize(store) + @store = store + @items = [] + end + + def total_cost + if total_items == 0 + return 0.0 + else + return @items.map{|item| item[:price] }.flatten.inject{|sum,x| sum + x }*100.round / 100.0 + end + end + + def add_item(item, quantity = 1) + if quantity > item.in_store + quantity = item.in_store + end + quantity.times do + if item.available? + @items << item + end + end + end + def total_items + return @items.size + end + def unique_items + return @items.uniq.first.to_a + end -class Cart -@@store - def initialize(store) - @@store = store + def checkout! + @items.each do |item| + item[:in_store] -= 1 + end + @store.total_sale += total_cost + end end + end + + + + + class Hash def method_missing(n) - self[n.to_s.to_sym] + self[n.to_sym] + end + + def available? + if self.in_store > 0 + return true + else + return false + end end end diff --git a/test_store.rb b/test_store.rb index c8464fb..86f11ff 100644 --- a/test_store.rb +++ b/test_store.rb @@ -23,23 +23,23 @@ def test_search_for_non_present_items def test_search_with_availability_flag assert_equal 'A lamp', @store.search(:color => 'green', :category => 'furniture', :available => true).first.name end -=begin + def test_sort_items_by_price_cheapest_first assert_equal 1.99, @store.items_sorted_by(:price, :asc).first.price end - + def test_sort_items_by_in_stock_cheapest_last assert_equal 399.00, @store.items_sorted_by(:price, :desc).first.price end - + def test_categories_list assert_equal ['Clothing', 'Furniture', 'Tools'], @store.categories end - + def test_unique_items_in_category assert_equal ['Jeans', 'T-shirt'], @store.unique_articles_in_category('Clothing') end - + def test_cart_initialization assert_equal @store, @cart.store assert_equal [], @cart.items @@ -66,7 +66,7 @@ def test_add_item_with_quantity_into_cart 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) } @@ -74,7 +74,7 @@ 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) @@ -94,16 +94,14 @@ 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 - end - + end def test_checkout_to_increase_total_sales n = 5 @cart.add_item(@default_item, n) @cart.checkout! assert_equal @store.total_sale, (@default_item.price * n).round(2) end -=end end From 65ff5025450247ce984288c62311402ec41fbc15 Mon Sep 17 00:00:00 2001 From: irhete Date: Thu, 14 Mar 2013 12:37:43 +0200 Subject: [PATCH 4/4] added fixed tests --- store.rb | 67 ++++++++++++++++++++++++--------------------------- test_store.rb | 28 +++++++++++---------- 2 files changed, 47 insertions(+), 48 deletions(-) diff --git a/store.rb b/store.rb index c45329a..1b2602f 100644 --- a/store.rb +++ b/store.rb @@ -43,60 +43,57 @@ def unique_articles_in_category(category) articles = [] @@items.select {|item| item[:category] == category}.map{ |item| item[:name] }.flatten.uniq.sort end +end - class Cart - attr_accessor :store - attr_accessor :items +class Store::Cart + attr_accessor :store + attr_accessor :items - def initialize(store) - @store = store - @items = [] - end + def initialize(store) + @store = store + @items = [] + end - def total_cost - if total_items == 0 - return 0.0 - else - return @items.map{|item| item[:price] }.flatten.inject{|sum,x| sum + x }*100.round / 100.0 - end + def total_cost + if total_items == 0 + return 0.0 + else + return @items.map{|item| item[:price] }.flatten.inject{|sum,x| sum + x }*100.round / 100.0 end + end - def add_item(item, quantity = 1) - if quantity > item.in_store - quantity = item.in_store - end - quantity.times do - if item.available? - @items << item - end + def add_item(item, quantity = 1) + if quantity > item.in_store + quantity = item.in_store + end + quantity.times do + if item.available? + @items << item end end + end - def total_items - return @items.size - end + def total_items + return @items.size + end - def unique_items - return @items.uniq.first.to_a - end + def unique_items + return @items.uniq + end - def checkout! - @items.each do |item| - item[:in_store] -= 1 - end - @store.total_sale += total_cost + def checkout! + @items.each do |item| + item[:in_store] -= 1 end + @store.total_sale += total_cost end - end - - class Hash def method_missing(n) self[n.to_sym] diff --git a/test_store.rb b/test_store.rb index 86f11ff..bbdb6b0 100644 --- a/test_store.rb +++ b/test_store.rb @@ -19,7 +19,7 @@ def test_search_by_multiple_search_criteria def test_search_for_non_present_items assert_equal [], @store.search(:color => 'green', :size => nil) end - + def test_search_with_availability_flag assert_equal 'A lamp', @store.search(:color => 'green', :category => 'furniture', :available => true).first.name end @@ -27,19 +27,19 @@ def test_search_with_availability_flag def test_sort_items_by_price_cheapest_first assert_equal 1.99, @store.items_sorted_by(:price, :asc).first.price end - + def test_sort_items_by_in_stock_cheapest_last assert_equal 399.00, @store.items_sorted_by(:price, :desc).first.price end - + def test_categories_list assert_equal ['Clothing', 'Furniture', 'Tools'], @store.categories end - + def test_unique_items_in_category - assert_equal ['Jeans', 'T-shirt'], @store.unique_articles_in_category('Clothing') + assert_equal ['Jeans', 'T-shirt'].sort, @store.unique_articles_in_category('Clothing').sort end - + def test_cart_initialization assert_equal @store, @cart.store assert_equal [], @cart.items @@ -49,7 +49,7 @@ def test_cart_initialization def test_add_one_item_into_cart @cart.add_item(@default_item) assert_equal 1, @cart.total_items - assert_equal Array(@default_item), @cart.unique_items + assert_equal [@default_item], @cart.unique_items assert @cart.unique_items.include?(@default_item) end @@ -57,16 +57,16 @@ def test_add_one_item_multiple_times_into_cart n = 5 n.times { @cart.add_item(@default_item) } assert_equal n, @cart.total_items - assert_equal Array(@default_item), @cart.unique_items + assert_equal [@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 + assert_equal [@default_item], @cart.unique_items end - + def test_add_item_multiple_items_into_cart n = 2 n.times { @cart.add_item(@default_item) } @@ -74,7 +74,7 @@ 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) @@ -94,10 +94,11 @@ 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 - end + end + def test_checkout_to_increase_total_sales n = 5 @cart.add_item(@default_item, n) @@ -105,3 +106,4 @@ def test_checkout_to_increase_total_sales assert_equal @store.total_sale, (@default_item.price * n).round(2) end end +