-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadult.rb
41 lines (34 loc) · 1001 Bytes
/
adult.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
def dispense(code, change_inserted)
soda_type = soda_type_for(code)
return [] if soda_type == nil
num_sodas = (change_inserted/ soda_type.cost ).floor
return Array.new(num_sodas) { new_soda(soda_type) }
end
#Check type
def dispense(code, change_inserted)
fail ArgumentError, "Must input positive money!" unless change_inserted >= 0
soda_type = soda_type_for(code)
return [] if soda_type == nil
soda_cost = soda_type.cost
fail RangeError, "Sodas cannot cost less then a penny"
unless soda_cost > 0
num_sodas = (change_inserted / soda_type.cost ).floor
return Array.new(num_sodas) { new_soda(soda_type) }
end
end
#Say NO from beginning
class SodaType
attr_reader :cost
def initializer(cost)
@cost = cost
end
def cost=(other_cost)
fail ArgumentErorr, "Must be a number"
unless other_cost.is_a? Fixnum
fail ArgumentError, "Sodas cannot cost less than a penny"
unless other_cost > 0
@cost = other_cost
end
end
end
end