Skip to content

Instantly share code, notes, and snippets.

@laurenachoo
Created May 4, 2015 01:37
Show Gist options
  • Save laurenachoo/69db69c7ae28d7bb35c4 to your computer and use it in GitHub Desktop.
Save laurenachoo/69db69c7ae28d7bb35c4 to your computer and use it in GitHub Desktop.
class BottleConsumer
attr_accessor :bottles, :caps, :dollars, :empty, :bottles_returned, :caps_returned
def initialize(dollars)
@bottles = 0
@caps = 0
@empty = 0
@dollars = dollars
@caps_returned = 0
@bottles_returned = 0
end
end
require_relative 'bottle_consumer'
class BottleReturn
def purchase_bottle
while @consumer.dollars >= 2
@consumer.bottles += 1
@consumer.dollars -= 2
end
end
def drink_bottles
while @consumer.bottles > 0
@consumer.bottles -= 1
@consumer.empty += 1
@consumer.caps += 1
end
end
def return_bottles
while @consumer.empty >= 2
@consumer.empty -= 2
@consumer.bottles += 1
@consumer.bottles_returned += 1
end
end
def return_caps
while @consumer.caps >= 4
@consumer.caps -= 4
@consumer.bottles += 1
@consumer.caps_returned += 1
end
end
def shop_cycle
while @consumer.bottles > 0
drink_bottles
return_bottles
return_caps
end
@consumer.bottles
end
def run_program
puts "Enter dollar amount you will spend: "
dollars = gets.chomp.to_i
@consumer = BottleConsumer.new(dollars)
purchase_bottle
shop_cycle
puts "If you make an investment of $#{dollars} you will get back #{@consumer.caps_returned} bottles from returning the caps, and #{@consumer.bottles_returned} bottles from returning the empties. "
puts "There will be #{@consumer.caps} caps remaining, and #{@consumer.empty} empty bottles left."
restart_program
end
def restart_program
restart = BottleReturn.new
restart.run_program
end
end
start = BottleReturn.new
start.run_program
@anlek
Copy link

anlek commented May 12, 2015

Love the fact that you did this in OOP!
You did a great job naming the methods as well.
Be careful with restart_program. You actually are creating a new program and running it, not restarting it. You want something like:

def restart_program
  run_program
end

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