-
Notifications
You must be signed in to change notification settings - Fork 0
/
task28-2.rb
87 lines (79 loc) · 2.08 KB
/
task28-2.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
class CofeeMachine
def initialize
@component = {"coffee" => 0, "tea" => 0, "chocolate" => 0, "cream" => 0, "sugar" => 0, "lemon" => 0}
@offers = {}
@Size = 23
@Recipes = {"Coffee" => {"coffee" => 1, "sugar" => 4},
"Tea" => {"tea" => 1, "sugar" => 4, "lemon" => 1},
"Chocolate" => {"chocolate" => 1, "sugar" => 1},
"Cream" => {"cream" => 1, "sugar" => 0}}
end
def order rec, changes
changes.each do |k, v|
if k == "sugar" and v > 9
changes["sugar"] = 9
elsif v.abs > 1 and k != "sugar"
changes[k] = v/v.abs
end
end
if @Recipes.key?(rec)
tmp = @Recipes[rec].clone
else
return nil
end
const = true
tmp.each_key do |key|
if changes.key?(key)
tmp[key] += changes[key]
if tmp[key] < 0 then tmp[key] = 0 end
end
end
["lemon", "sugar"].each do |x|
unless tmp.key?(x)
if changes[x]
if changes[x] > 0 then tmp[x] = changes[x] end
end
end
end
temp = @component.clone
tmp.each do |k, v|
if temp[k] - v > 0
temp[k] -= v
else
const = false
nil
end
end
if const
@component = temp.clone
@offers[rec] ? @offers[rec]+=1 : @offers[rec]=1
else
return nil
end
end
def load ()
@component.each_key { |key| @component[key] = @Size }
end
def status ()
@component
end
def stat ()
@offers
end
end
cf = CofeeMachine.new
cf.load
puts cf.status
cf.order("Coffee", {"lemon" => 4, "tea" => 3, "sugar" => 2, "coffee" =>2})
puts cf.status
cf.order("Coffee", {"lemon" => 4, "tea" => 3, "sugar" => 2, "coffee" =>2})
puts cf.status
cf.order("Tea", {"lemon" => 4, "tea" => 3, "sugar" => 2, "coffee" =>2})
puts cf.status
cf.order("Tea", {"lemon" => 4, "tea" => 3, "sugar" => 2, "coffee" =>2})
puts cf.status
cf.order("Tea", {"lemon" => 4, "tea" => 3, "sugar" => 2, "coffee" =>2})
cf.order("Cream", {"sugar" => 2})
cf.order("Cream", {"sugar" => -1})
puts cf.status
print cf.stat