Skip to content

Instantly share code, notes, and snippets.

@gbrl
Created April 27, 2016 21:37
Show Gist options
  • Save gbrl/fb8c695f850166af02dfa77a59bcea77 to your computer and use it in GitHub Desktop.
Save gbrl/fb8c695f850166af02dfa77a59bcea77 to your computer and use it in GitHub Desktop.
require 'pp'
# BUYING
# num_bottles = money/2
# REDEEMING
# num_bottles = caps * 0.25
# num_bottles = bottles * 0.5
def buy(investment=0)
#puts "Customer gives me #{investment} dollars."
num_bottles = 0
change = investment % 2
if investment <= 1
puts "A bottle costs $2!"
elsif investment.odd?
puts "A bottle costs $2."
puts "Customer's change is $#{change}." if change != 0
else
num_bottles = investment/2
puts "Thanks, you now have #{num_bottles} #{num_bottles > 1 ? 'bottles' : 'bottle'}."
end
redeem_potential = redeem(num_bottles,0)
puts "With these bottles you can redeem #{redeem(num_bottles,0)} bottles."
end
def redeem(bottles=0,caps=0)
bottles_from_caps = caps * 0.25
bottles_from_empties = bottles * 0.5
redemption = (bottles_from_empties + bottles_from_caps).to_i
end
def openshop
keep_going = true
while keep_going
puts "Hello, how much are you spending?"
money = gets.chomp.to_i
if money.class == Fixnum
buy(money)
else
puts "Sorry, I didn't understand that. Enter a number next time."
end
puts "Anything else? ('y' or 'n')"
stopping = gets.chomp
keep_going = false if stopping.downcase == "n"
end
end
def test
(2..20).each do |x|
buy(x)
puts "---------------------------------------------------"
end
end
#test()
openshop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment