Skip to content

Instantly share code, notes, and snippets.

@scottdomes
Last active March 5, 2016 00:41
Show Gist options
  • Save scottdomes/56b2f17e22c935da5d2c to your computer and use it in GitHub Desktop.
Save scottdomes/56b2f17e22c935da5d2c to your computer and use it in GitHub Desktop.
def determine_free_bottles (investment)
process_purchase(investment)
begin
find_bottles(@caps, @empty_bottles)
end while @empty_bottles > 1 || @caps > 3
print_result
end
def process_purchase (investment)
bottles_bought = investment / 2
puts "You bought #{bottles_bought} bottles!"
@bottles_consumed += bottles_bought
@caps += bottles_bought
@empty_bottles += bottles_bought
end
def print_result
puts "You've earned #{@bottles_from_caps} bottles from cap recycling!"
puts "You've earned #{@bottles_from_bottles} bottles from bottle recycling!"
puts "In total, you've received #{@bottles_consumed} bottles! Wee!"
puts "You have #{@empty_bottles} bottles and #{@caps} caps left over! Buy more, or start again by typing 'reset'!"
end
def find_bottles (caps, bottles)
@free_bottles = (caps / 4) + (bottles / 2)
@bottles_from_caps += (caps / 4)
@bottles_from_bottles += (bottles / 2)
@bottles_consumed += @free_bottles
@caps = (caps % 4) + @free_bottles
@empty_bottles = (bottles % 2) + @free_bottles
end
def initialize_values
@bottles_from_caps = 0
@bottles_from_bottles = 0
@bottles_consumed = 0
@caps = 0
@empty_bottles = 0
end
def ask_customer (new_session?)
if new_session? == true
initialize_values
end
print "How much do you want to spend? "
investment = gets.chomp
unless investment == "exit"
case investment
when "reset"
puts "All values reset!"
ask_customer(true)
else
determine_free_bottles(investment.to_i)
ask_customer(false)
end
end
end
ask_customer(true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment