Skip to content

Instantly share code, notes, and snippets.

@JeffCohen
Created May 24, 2012 15:22
Show Gist options
  • Save JeffCohen/2782207 to your computer and use it in GitHub Desktop.
Save JeffCohen/2782207 to your computer and use it in GitHub Desktop.
Vending Machine Fun
# Try adding the ability to keep track of capacity per brand
# And push the buttons until the entire machine is empty
class VendingMachine
# attr_accessor :money
def initialize(number_of_cans)
@cans = []
number_of_cans.times do
@cans << 'Coke'
end
# => ['Coke', 'Coke', 'Coke', 'Coke']
@money = 0
end
def empty?
@cans.empty?
# @capacity == 0
end
def insert_coins(amount)
@money = @money + amount
end
def push_button(brand)
# @capacity = @capacity - 1
@cans.shift
puts brand
end
def remove_money
orig_amount = @money
@money = 0
return orig_amount
end
end
machine = VendingMachine.new(5)
until machine.empty?
machine.insert_coins(50)
machine.push_button 'Coke'
end
puts machine.remove_money # => 250
puts machine.remove_money # => 0
# Coke
# Coke
# Coke
# Coke
# Coke
# Extra credit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment