-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.rb
115 lines (88 loc) · 1.93 KB
/
order.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
require_relative 'coffee'
class Order
attr_reader :order
def initialize coffees = {}
@order = coffees
end
def self.import s
throw ArgumentError, "Invalid order export code" unless !!(s =~ (/^\{.*}$/))
return Order.new(Hash[eval(s).collect{|k,v| [Coffee.import(k), v]}])
end
def [] i
throw TypeError unless i.class == Fixnum
return @order.keys[i]
end
def search_by args={}
matches = []
c1 = Coffee.new
if args[:coffee]
c1 = args[:coffee]
else
c1.name = args[:name]
c1.id = args[:id].to_i
c1.intensity = args[:intensity].to_i
c1.price = args[:price].to_f
c1.url = args[:url]
end
@order.keys.each do |c2|
matches << c2 if c1.par_eql? c2
end
return matches
end
def is_empty?
@order.length < 1
end
def add coffee, amount=0
if coffee.kind_of? Coffee
@order.store coffee, amount
else
raise TypeError, "Argument is not a coffee"
end
end
def remove args, amount
if @order.length > 0
should_remove = nil
@order.keys.each do |k|
should_remove = k if k.par_eql? == Coffee.new(args)
end
raise ArgumentError, "No order for this ID found" unless should_remove
@order[should_remove] -= amount
@order.delete should_remove if @order[should_remove] <= 0
end
end
def total_capsules
amount = 0
if @order.length > 0
@order.values.each do |v|
amount += v * 10
end
end
return amount
end
def total_cost
price = 0
if @order.length > 0
@order.each do |k, v|
price += k.price * v
end
end
return price
end
def to_s
s = ""
if @order.length > 0
@order.each do |k, v|
s << "#{v} -- #{k.to_s}\n"
end
s << "\nTotal of #{total_capsules} capsules for #{total_cost} ILS."
else
s << "Empty order."
end
return s
end
def export
return Hash[@order.collect{|k,v| [k.export, v]}].to_s
end
def export! filename='default'
File::write "#{filename}.order", Hash[@order.collect{|k,v| [k.export, v]}].to_s
end