-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedo_Candy_Vending.rb
More file actions
65 lines (56 loc) · 1.63 KB
/
Redo_Candy_Vending.rb
File metadata and controls
65 lines (56 loc) · 1.63 KB
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
# making a virtual vending Machine
# introduce the greeting to customer
# Ask customer how much money
# display all candy selections
# customer makes selection, is it valid
# upper and lower case selection
# decide if customer has enough money
# if yes give candy and change
# if no tell them
candy_options = Hash.new
candy_options = {
"A" => 0.65,
"B" => 0.50,
"C" => 0.75,
"D" => 0.65,
"E" => 0.55
}
# introduce the greeting to customer
# Ask customer how much money
puts "Welcome to Ada's Computer Candy Machine!
(All candy provided is virtual.)
How much money do ya got?"
# ask user for money
# validate input is number (http://stackoverflow.com/questions/31410694/how-to-check-if-user-input-is-a-number)
begin
user_money = Float(gets)
rescue ArgumentError
puts "Enter a number i.e. 1, 0.6"
retry
end
# display all user money and candy selections
puts "$#{user_money}, that's all?
Well, lemme tell ya what we got here.
A $0.65 Twix
B $0.50 Chips
C $0.75 Nutter Butter
D $0.65 Peanut Butter Cup
E $0.55 Juicy Fruit Gum
So, What'll ya have?"
# customer makes selection, is it valid
# upper and lower case selection
user_selection = gets.chomp.upcase
until candy_options.has_key?(user_selection) do
puts "enter a letter"
user_selection = gets.chomp.upcase
end
# decide if customer has enough money
# if yes give candy and change
# if no tell them
candy_price = candy_options[user_selection]
if user_money < candy_price
puts "Not enough money. take a hike"
else
user_change = user_money - candy_options[user_selection]
puts "\nThanks for purchasing candy through us. \nPlease take your candy, and your $#{user_change} change!"
end