Last active
July 30, 2018 22:05
-
-
Save sheland/22be8da0d69be96ff5fa1592c1c10d2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Day 2 JSL | |
| #Requirements for candy machine | |
| #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) | |
| #Display all candy options and their costs (even if the user cannot afford the candy) | |
| #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 | |
| #[Optional] Handle when the buyer enters "C" or "c" so that it works as expected | |
| #[Optional] Do something appropriate when the buyer enters an invalid amount for the money and an invalid selection | |
| puts "Welcome to Ada's Computer Candy Machine!" | |
| #input user's $ amount | |
| puts "How much money do you have? $" | |
| user_money = gets.chomp.to_f | |
| puts "$ #{user_money}, awesome! Here's our candy collection: " | |
| #Display all candy options and their costs | |
| puts "A: $0.65 Twix" | |
| puts "B: $0.50 Chips" | |
| puts "C: $0.75 Nutter Butter" | |
| puts "D: $0.65 Peanut Butter Cup" | |
| puts "E: $0.55 Juicy Fruit Gum" | |
| #input user's candy choice | |
| puts "Which candy would you like? Please enter the letter coressponding to your candy choice: A,B,C,D or E? " | |
| user_candy = gets.chomp.upcase | |
| #Determine if user can purchase candy choice "A" | |
| if user_candy == "A" && user_money >= 0.65 | |
| user_change = "#{user_money.to_f - 0.65}" | |
| puts "Here's your Twix." | |
| puts "Your change is: $ #{user_change}" | |
| elsif user_candy == "A" && user_money < 0.65 | |
| puts "$ #{user_money} is not enough to purchase a Twix." | |
| end | |
| #Determine if user can purchase candy choice "B" | |
| if user_candy == "B" && user_money >= 0.50 | |
| user_change = "#{user_money.to_f - 0.50}" | |
| puts "Here's your Chips." | |
| puts "Your change is: $ #{user_change}" | |
| elsif user_candy == "B" && user_money < 0.50 | |
| puts "$ #{user_money} is not enough to purchase Chips." | |
| end | |
| #Determine if user can purchase candy choice "C" | |
| if user_candy == "C" && user_money >= 0.75 | |
| user_change = "#{user_money.to_f - 0.75}" | |
| puts "Here's your Nutter Butter." | |
| puts "Your change is: $ #{user_change}" | |
| elsif user_candy == "C" && user_money < 0.75 | |
| puts "$ #{user_money} is not enough to purchase a Nutter Butter." | |
| end | |
| #Determine if user can purchase candy choice "D" | |
| if user_candy == "D" && user_money >= 0.65 | |
| user_change = "#{user_money.to_f - 0.65}" | |
| puts "Here's your Peanut Butter Cup." | |
| puts "Your change is: $ #{user_change}" | |
| elsif user_candy == "D" && user_money < 0.65 | |
| puts "$ #{user_money} is not enough to purchase a Peanut Butter Cup." | |
| end | |
| #Determine if user can purchase candy choice "A" | |
| if user_candy == "E" && user_money >= 0.55 | |
| user_change = "#{user_money.to_f - 0.55}" | |
| puts "Here's your Juicy Fruit Gum." | |
| puts "Your change is: $ #{user_change}" | |
| elsif user_candy == "E" && user_money < 0.55 | |
| puts "$ #{user_money} is not enough to purchase a Juicy Fruit Gum." | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment