Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- puts "Welcome to the Ada Developers Academy's Computer Candy Machine!"
- puts "(All candy provided is virtual.)"
- # Ask the user how much money they have
- puts "How much money do you have?"
- money = gets.chomp.to_f
- while money <= 0
- puts "Please enter a valid amount."
- money = gets.chomp.to_f
- end
- #Display all the candy options and their costs (even if they cannot afford the candy
- puts "We have:
- A $0.65 Twix
- B $0.50 Chips
- C $0.75 Nutter Butter
- D $0.65 Peanut Butter Cup
- E $.55 Juicy Fruit Gum "
- products = {
- 'A': 0.65,
- 'B': 0.50,
- 'C': 0.75,
- 'D': 0.65,
- 'E': 0.55
- }
- # Decide whether the user can afford the candy or not
- puts "What would you like to have?"
- # Handle when the buyer enters "C" or "c" so that it works as expected
- candy = gets.chomp.upcase.to_sym
- until products.key? candy
- puts "Please enter a valid letter"
- candy = gets.chomp.upcase
- end
- change = money - products[candy]
- if change > 0
- puts "Your change is $#{change}. Please take your candy."
- elsif change == 0
- puts "Please take your candy"
- elsif change < 0
- puts "You do not have enough money to buy the selected candy."
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement