Class: Hardware::CashRegister

Inherits:
Component show all
Defined in:
lib/hardware/cash_register.rb

Instance Method Summary collapse

Methods inherited from Component

#monitor_changes

Constructor Details

#initialize(bin, sensor_collection, actuator_collection) ⇒ CashRegister

Returns a new instance of CashRegister.



10
11
12
13
14
15
# File 'lib/hardware/cash_register.rb', line 10

def initialize(bin, sensor_collection, actuator_collection)
  super(sensor_collection, actuator_collection)
  @bin = bin
  @stocks = {}
  @stocks.default = 0
end

Instance Method Details

#configureObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hardware/cash_register.rb', line 53

def configure
  create_sensor_for(:cash_insert_200)
  create_sensor_for(:cash_insert_100)
  create_sensor_for(:cash_insert_50)
  create_sensor_for(:cash_fill_200)
  create_sensor_for(:cash_fill_100)
  create_sensor_for(:cash_fill_50)
  create_actuator_for(:cash_drop_200) do
    drop_coin(Coin.two_euro) 
  end
  create_actuator_for(:cash_drop_100) do
    drop_coin(Coin.one_euro) 
  end
  create_actuator_for(:cash_drop_50) do
    drop_coin(Coin.fifty_cents) 
  end
end

#drop_coin(coin, delay = 1) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/hardware/cash_register.rb', line 17

def drop_coin(coin, delay = 1)
  if @stocks[coin] > 0
    sleep(delay)
    @bin.receive(coin) 
    @stocks[coin] -= 1
  end
end

#empty?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/hardware/cash_register.rb', line 71

def empty?
  @stocks.empty?
end

#fill(coin_type, amount, delay = 1) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/hardware/cash_register.rb', line 37

def fill(coin_type, amount, delay = 1)
  amount.times do 
    sleep(delay)
    @stocks[coin_type] += 1
    @sensor_collection.fire(fill_event_for(coin_type))
  end
end

#fill_event_for(coin_type) ⇒ Object



45
46
47
48
49
# File 'lib/hardware/cash_register.rb', line 45

def fill_event_for(coin_type)
  {Coin.two_euro => :cash_fill_200, 
   Coin.one_euro => :cash_fill_100, 
   Coin.fifty_cents => :cash_fill_50 }[coin_type]
end

#insert_coin(coin_type) ⇒ Object



25
26
27
28
# File 'lib/hardware/cash_register.rb', line 25

def insert_coin(coin_type)
  @stocks[coin_type] += 1
  @sensor_collection.fire(insert_event_for(coin_type))
end

#resetObject



75
76
77
# File 'lib/hardware/cash_register.rb', line 75

def reset
  @stocks.clear
end