Skip to content

Instantly share code, notes, and snippets.

@ChantalDemissie
Created January 11, 2019 08:23
Show Gist options
  • Save ChantalDemissie/389a8234a38efb45568090a129a84b22 to your computer and use it in GitHub Desktop.
Save ChantalDemissie/389a8234a38efb45568090a129a84b22 to your computer and use it in GitHub Desktop.
candy machine
#introduction to user of candy machine program
puts "Welcome to Chantals ~New Orleans Sweets Machine~ \n How much money do you have?"
money = gets.chomp.to_f
puts "\n"
puts "That $#{money} sounds about right in this economy, here are your options!"
puts "A: $5.00 Bananas Foster"
puts "B: $3.00 Beignet"
puts "C: $4.50 Bread Pudding"
puts "D: $2.50 Praline"
candy_choice =gets.chomp.upcase
if candy_choice == "A"
candy_choice = 5.00
elsif candy_choice == "B"
candy_choice = 3.00
elsif candy_choice == "C"
candy_choice = 4.50
elsif candy_choice == "D"
candy_choice = 2.50
end
change = money - candy_choice
puts "\n"
if change > 0
puts "Enjoy the sweets! and take your $#{change} change"
else
puts "No imported sweets for you! go buy some produce around the corner if you only have $#{money}! "
end
@shrutivanw
Copy link

Nicely done!
Here are some thoughts for further improvement:

  • Minor: you're missing a space after = in line 11.
  • How can you give the user a useful error message if they enters an invalid selection e.g. "z"?
  • How can you make your design scalable and maintainable? e.g. easily allow for changing the price or name of a candy, easily allow adding 4 more candy choices etc.
  • As you learn more in chapters to follow, consider if using a data structure like a hash would allow you to better organize your data and make your design more scalable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment