Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. puts "Welcome to the Ada Developers Academy's Computer Candy Machine!"
  2. puts "(All candy provided is virtual.)"
  3.  
  4. # Ask the user how much money they have
  5. puts "How much money do you have?"
  6.  
  7. money = gets.chomp.to_f
  8. while money <= 0
  9. puts "Please enter a valid amount."
  10. money = gets.chomp.to_f
  11. end
  12.  
  13. #Display all the candy options and their costs (even if they cannot afford the candy
  14. puts "We have:
  15. A $0.65 Twix
  16. B $0.50 Chips
  17. C $0.75 Nutter Butter
  18. D $0.65 Peanut Butter Cup
  19. E $.55 Juicy Fruit Gum "
  20.  
  21. products = {
  22. 'A': 0.65,
  23. 'B': 0.50,
  24. 'C': 0.75,
  25. 'D': 0.65,
  26. 'E': 0.55
  27. }
  28.  
  29. # Decide whether the user can afford the candy or not
  30. puts "What would you like to have?"
  31.  
  32. # Handle when the buyer enters "C" or "c" so that it works as expected
  33. candy = gets.chomp.upcase.to_sym
  34.  
  35. until products.key? candy
  36. puts "Please enter a valid letter"
  37. candy = gets.chomp.upcase
  38. end
  39.  
  40. change = money - products[candy]
  41. if change > 0
  42. puts "Your change is $#{change}. Please take your candy."
  43. elsif change == 0
  44. puts "Please take your candy"
  45. elsif change < 0
  46. puts "You do not have enough money to buy the selected candy."
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement