Class: ChangeMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/coinstar/change_machine.rb

Constant Summary collapse

CURRENCY =
{penny: 1, nickel: 5, dime: 10, quarter: 25}

Class Method Summary collapse

Class Method Details

.make_cents(currency) ⇒ Object



19
20
21
22
23
24
# File 'lib/coinstar/change_machine.rb', line 19

def self.make_cents(currency)
  currency.inject(0) do |cents, (k,v)|
    cents += (CURRENCY[singularize(k)] * v)
    cents
  end
end

.make_change(cents) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/coinstar/change_machine.rb', line 6

def self.make_change(cents)
  raise 'You\'re going to need a bigger change machine' if cents > 100
  calculated_change = sorted_currency.inject({}) do |change, (k,v)|
    unless cents < v
      divisible = cents / v
      change[k] = divisible
      cents = cents - (CURRENCY[k] * divisible)
    end
    change
  end
  pluralize(calculated_change)
end