-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshop.rb
74 lines (56 loc) · 2.25 KB
/
shop.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
module Shop
# global
$products_count_hash = Hash.new
$products_price_hash = Hash.new
# create a hash of |product => count| pairs from products_to_find
def Shop.create_count_hash(products_to_find)
$products_count_hash = Hash[products_to_find.collect { |product| [product, 0] }]
end
# create a hash of |product => price| pairs from products_to_find
def Shop.create_price_hash(products_to_find)
$products_price_hash = Hash[products_to_find.collect { |product| [product, 0.0] }]
end
# for each requested product type finds total price from listed products
def Shop.find_prices(products_array)
products_array.each do |product|
if $products_count_hash.key?(product["product_type"])
# count product's occurrence
$products_count_hash[product["product_type"]] += 1
# add up product's price for all variants
variants_array = product["variants"]
variants_array.each { |variant|
$products_price_hash[product["product_type"]] += variant["price"].to_f
}
end
end
end
# check if reached end of json body
def Shop.is_page_end(products_array)
products_array.length == 0 ? true : false
end
# sum total price of all products requested
def Shop.get_total_price
total = 0.0;
$products_price_hash.each do |product, price|
total += price
end
return total.round(2)
end
# print result consisting of frequency and prices of requested products
def Shop.print_result
puts ""
puts "drum roll..."
puts ""
puts "This Shopify store contains: "
$products_count_hash.each do |product, count|
if not product.nil?
puts count.to_s + " products for type " + product +
" worth $" + $products_price_hash[product].round(2).to_s
end
end
puts "------------------------------------------"
print "Total price of these products: "
puts "$" + get_total_price.to_s
puts ""
end
end