diff --git a/store.rb b/store.rb index 3cb712e..1b2602f 100644 --- a/store.rb +++ b/store.rb @@ -1 +1,109 @@ # Yout little input goes here! + +require 'json' + +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 + searchable_items = searchable_items.select{|item| item.available?} + else + searchable_items = searchable_items.select{|item| item[kw_key].to_s.downcase == kw_value.to_s.downcase} + end + end + return searchable_items + 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 +end + + +class Store::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 + end + + 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] + 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 1b3beb8..bbdb6b0 100644 --- a/test_store.rb +++ b/test_store.rb @@ -1,5 +1,4 @@ require 'rubygems' -require 'json' require_relative 'store' @@ -38,7 +37,7 @@ def test_categories_list 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 @@ -50,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 @@ -58,14 +57,14 @@ 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 @@ -106,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 \ No newline at end of file +end +