Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. # Ask the user how much money they have, assume that the $ symbol is part of the prompt (the user doesn't have to enter it)
  2.  
  3. # [Optional] Handle when the buyer enters "C" or "c" so that it works as expected
  4. # [Optional] Do something appropriate when the buyer enters an invalid amount for the money and an invalid selection
  5.  
  6. puts "\n"
  7. puts "Hi kid! Welcome to my virtual candy machine. You give me virtual money, I give you virtual candy."
  8. puts "\n"
  9. print "How much money do you have? $"
  10. kid_money = gets.chomp.to_f
  11.  
  12. puts "\n"
  13.  
  14. # Since we're talking about money, format the response to include two decimal points
  15.  
  16. puts "$%0.2f? Sweet!" % [kid_money]
  17. puts "\n"
  18.  
  19. # Display all candy options and their costs (even if the user cannot afford the candy)
  20.  
  21. puts "Okay, here's what I've got:"
  22. puts "\n"
  23. puts "a. Laffy Taffy, $0.10"
  24. puts "b. Bit o' Honey, $0.75"
  25. puts "c. Jelly Bellys, $1.75"
  26. puts "d. Kinder egg, $2.50"
  27. puts "\n"
  28. puts "So, which one do you want? Choose a letter."
  29.  
  30. # Decide whether the user can afford the candy or not, if they can't, tell them so, if they can, calculate and display their change
  31.  
  32. candy_option = gets.chomp.downcase
  33. puts "\n"
  34.  
  35. # Use conditionals to define letter options
  36.  
  37. if candy_option == "a"
  38. price = 0.10
  39. candy_name = "Laffy Taffy"
  40. elsif candy_option == "b"
  41. price = 0.75
  42. candy_name = "Bit o' Honey"
  43. elsif candy_option == "c"
  44. price = 1.75
  45. candy_name = "Jelly Bellys"
  46. elsif candy_option == "d"
  47. price = 2.50
  48. candy_name = "Kinder egg"
  49. else
  50. puts "Sorry, I don't recognize that choice. Here's your money back."
  51. puts "\n"
  52. end
  53.  
  54. if kid_money < price
  55. puts "Get outta here, you don't have the money for that!"
  56. elsif kid_money == price
  57. puts "Okay, here's your #{candy_name}. Enjoy your virtual candy!"
  58. else kid_money > price
  59. change = kid_money - price
  60. puts "All righty, here's your #{candy_name} and your $%0.2f change. Enjoy!" % [change]
  61. end
  62.  
  63. puts "Thanks for using the virtual candy machine."
  64. puts "\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement