Created
May 25, 2012 20:26
-
-
Save JeffCohen/2790389 to your computer and use it in GitHub Desktop.
Vending Machine Script
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
# Vending Project | |
# Task 1 - Write a vending machine script that will dispence cokes until inventory is 0. | |
# Task 2 - Try adding the ability to keep track of capacity per brand | |
class VendingMachine | |
def initialize(total_inventory) | |
@coke, @sprite, @root_beer = [total_inventory/3,total_inventory/3,total_inventory/3] | |
@money = 0 | |
@total_inventory = total_inventory * 50 | |
end | |
def inventory | |
@coke + @sprite + @root_beer | |
end | |
def empty? | |
@total_inventory == 0 | |
end | |
def insert_coins(coins) | |
@money = @money + coins | |
end | |
def push_button(soda) | |
if soda.downcase == 'coke' | |
@coke = @coke - 1 | |
puts "here is your #{soda}!" | |
elsif soda.downcase == 'sprite' | |
@sprite = @sprite - 1 | |
puts "here is your #{soda}!" | |
elsif soda.downcase == 'root beer' | |
@root_beer = @root_beer - 1 | |
puts "here is your #{soda}!" | |
else soda.downcase != 'coke' || 'sprite' || 'root beer' | |
puts "please pick coke, sprite, or root beer" | |
end | |
end | |
def remove_money | |
original = @money | |
@money = 0 | |
return original | |
end | |
end | |
#-------------------------------- | |
#---- Output | |
#-------------------------------- | |
machine = VendingMachine.new(15) | |
puts "Inventory is #{machine.inventory}" | |
machine.push_button 'sprite' | |
machine.push_button 'coke' | |
machine.push_button 'root beer' | |
machine.push_button 'orange' | |
machine.push_button 'root beer' | |
puts "Inventory is now #{machine.inventory}" | |
puts machine.remove_money # => 250 | |
puts machine.remove_money # => 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yay Ruby!!