Created
May 24, 2012 15:22
-
-
Save JeffCohen/2782207 to your computer and use it in GitHub Desktop.
Vending Machine Fun
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
# 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