-
Notifications
You must be signed in to change notification settings - Fork 0
/
soda_machine.rb
60 lines (52 loc) · 2.23 KB
/
soda_machine.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
machine = [
{:name => 'Coke', :amount_left => 12},
{:name => 'Diet Coke', :amount_left => 10},
{:name => 'cream soda', :amount_left => 9},
{:name => 'root beer', :amount_left => 11}
]
def sodas_left (soda_machine)
while 1 == 1
puts "Would you like to buy or reload a soda?"
action = gets.chomp.downcase
if action == "buy"
puts "What kind of soda would you like to buy? You can choose between Coke, Diet Coke, cream soda or root beer."
soda = gets.chomp.downcase
if soda == "coke"
soda_machine[0][:amount_left] -= 1
puts "There are #{soda_machine[0][:amount_left]} #{soda_machine[0][:name]}s left."
elsif soda == "diet coke"
soda_machine[1][:amount_left] -= 1
puts "There are #{soda_machine[1][:amount_left]} #{soda_machine[1][:name]}s left."
elsif soda == "cream soda"
soda_machine[2][:amount_left] -= 1
puts "There are #{soda_machine[2][:amount_left]} #{soda_machine[2][:name]}s left."
elsif soda == "root beer"
soda_machine[3][:amount_left] -= 1
puts "There are #{soda_machine[3][:amount_left]} #{soda_machine[3][:name]}s left."
else
puts "You're out of luck."
end #end if soda
elsif action == "reload"
puts "What kind of soda would you like to reload? You can choose between Coke, Diet Coke, cream soda or root beer."
reload = gets.chomp.downcase
if reload == "coke"
soda_machine[0][:amount_left] += 1
puts "There are #{soda_machine[0][:amount_left]} #{soda_machine[0][:name]}s left."
elsif reload == "diet coke"
soda_machine[1][:amount_left] += 1
puts "There are #{soda_machine[1][:amount_left]} #{soda_machine[1][:name]}s left."
elsif reload == "cream soda"
soda_machine[2][:amount_left] += 1
puts "There are #{soda_machine[1][:amount_left]} #{soda_machine[2][:name]}s left."
elsif reload == "root beer"
soda_machine[3][:amount_left] += 1
puts "There are #{soda_machine[2][:amount_left]} #{soda_machine[3][:name]}s left."
else
puts "What, can't you read??"
end # end if reload
else
puts "Sorry, this vending machine doesn't do THAT."
end #end if action
end #end while
end #end def
sodas_left(machine)