Class: ChangeMachine

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

Constant Summary collapse

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

Class Method Summary collapse

Methods included from Executable

clean_input, run

Class Method Details

.make_cents(currency) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/coinstar/change_machine.rb', line 21

def self.make_cents(currency)
  begin
    currency.inject(0) do |cents, (k,v)|
      cents += (CURRENCY[k.to_s.singularize.to_sym] * v)
      cents
    end
  rescue NoMethodError
    raise 'Unknown currency type'
  end
end

.make_change(cents) ⇒ Object



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

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.to_s.pluralize.to_sym] = divisible
      cents -= (CURRENCY[k] * divisible)
    end
    change
  end
end