Skip to content

Instantly share code, notes, and snippets.

@scottweisman
Forked from JeffCohen/gist:2782207
Created May 24, 2012 15:23
Show Gist options
  • Select an option

  • Save scottweisman/2782213 to your computer and use it in GitHub Desktop.

Select an option

Save scottweisman/2782213 to your computer and use it in GitHub Desktop.

Revisions

  1. @JeffCohen JeffCohen created this gist May 24, 2012.
    52 changes: 52 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    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