-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_4.rb
125 lines (114 loc) · 3.32 KB
/
exercise_4.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require 'date'
class ShoppingCart
def initialize
@pricelist = {}
@items = {}
end
def add_item_to_cart(item)
if !@items[item]
@items[item] = [@pricelist[item].to_i, 1, @pricelist[item].to_i, @pricelist[item].to_f]
else
@items[item][1] += 1
@items[item][2] += @pricelist[item].to_i
@items[item][3] += @pricelist[item].to_f
end
end
def show
index = 1
puts "Number | Amount | Item \t| Price Each | Price Total | With discount"
@items.each do |key, value|
puts "#{index} \t| #{value[1]} \t | #{key}: \t| #{value[0]}$ \t\t| #{value[2]}$ \t\t| #{value[3]}$"
index += 1
end
end
def cost
total_cost = @items.reduce(0) {|sum, value| sum += value[1][3]}
puts "Total: #{total_cost}"
end
def load_prices(date_of_today)
priceli = File.readlines('prices')
priceli.each do |item|
if item.split(' ')[0] == "watermelon"
if date_of_today.sunday
@pricelist[item.split(' ')[0].to_sym] = item.split(' ')[1].split('$')[0].to_i * 2
else
@pricelist[item.split(' ')[0].to_sym] = item.split(' ')[1].split('$')[0].to_i
end
else
@pricelist[item.split(' ')[0].to_sym] = item.split(' ')[date_of_today.season].split('$')[0]
end
end
end
def discounts
#buy 2 apples and pay just 1
if @items[:apples][1] >= 2
num_apples = @items[:apples][1] / 2 + @items[:apples][1] % 2
@items[:apples][3] = @pricelist[:apples].to_f * num_apples
puts "Apples: buy 2 and pay 1"
end
#by 3 oranges and pay just 2
if @items[:oranges][1] >= 3
num_oranges = @items[:oranges][1] / 3 * 2 + @items[:oranges][1] % 3
@items[:oranges][3] = @pricelist[:oranges].to_f * num_oranges
puts "Oranges: buy 3 and pay 2"
end
if @items[:grapes][1] >= 4
num_bananas = @items[:grapes][1] / 4
if @items[:bananas]
if @items[:bananas][3] > @pricelist[:bananas].to_f * num_bananas
@items[:bananas][3] -= @pricelist[:bananas].to_f * num_bananas
else
@items[:bananas][3] = 0.0
end
puts "Grapes: buy 4 and get 1 bannana"
end
end
end
end
class Season
def initialize
@today_date = Date.today
end
def season
year_day = @today_date.yday().to_i
if year_day >= 355 or year_day < 81
4 #winter
elsif year_day >= 81 and year_day < 173
1 #spring
elsif year_day >= 173 and year_day < 266
2 #summer
elsif year_day >= 266 and year_day < 355
3 #autumn
end
end
def sunday
week_day = @today_date.wday.to_i
week_day == 0 ? true : false #is sunday?
end
end
cart = ShoppingCart.new
cart.load_prices(Season.new)
cart.add_item_to_cart :apples
cart.add_item_to_cart :apples
cart.add_item_to_cart :apples
cart.add_item_to_cart :apples
cart.add_item_to_cart :apples
cart.add_item_to_cart :bananas
cart.add_item_to_cart :grapes
cart.add_item_to_cart :grapes
cart.add_item_to_cart :grapes
cart.add_item_to_cart :grapes
cart.add_item_to_cart :grapes
cart.add_item_to_cart :grapes
cart.add_item_to_cart :grapes
cart.add_item_to_cart :grapes
cart.add_item_to_cart :oranges
cart.add_item_to_cart :oranges
cart.add_item_to_cart :oranges
cart.add_item_to_cart :oranges
cart.add_item_to_cart :oranges
cart.add_item_to_cart :oranges
cart.add_item_to_cart :watermelon
cart.discounts
cart.show
cart.cost