Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions item.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion items.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}
]
]
87 changes: 86 additions & 1 deletion store.rb
Original file line number Diff line number Diff line change
@@ -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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@found võiks siin pigem olla lokaalne muutuja ja selle algväärtus selguse mõttes pigem tühi massiiv.

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

23 changes: 14 additions & 9 deletions test_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -53,36 +57,36 @@ 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) }
assert_equal n, @cart.total_items
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) }
@cart.add_item(@store.search(:name => 'Hammer').first)
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)
Expand All @@ -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
Expand All @@ -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
end