-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-12.rb
497 lines (382 loc) · 7.13 KB
/
06-12.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# Command Interface
class Command
def execute
raise NotImplementedError
end
def undo
raise NotImplementedError
end
end
class Light
attr_reader :location
def initialize(location)
@location = location
end
def on
puts "#{@location} light is on"
end
def off
puts "#{@location} light is off"
end
end
class LightOnCommand < Command
attr_reader :light
def initialize(light)
@light = light
end
def execute
@light.on
end
def undo
@light.off
end
end
class LightOffCommand < Command
attr_reader :light
def initialize(light)
@light = light
end
def execute
@light.off
end
def undo
@light.on
end
end
class Stereo
attr_reader :location
def initialize(location)
@location = location
end
def on
puts "#{@location} stereo is on"
end
def off
puts "#{@location} stereo is off"
end
def set_cd
puts "#{@location} stereo is set for CD input"
end
def set_dvd
puts "#{@location} stereo is set for DVD input"
end
def set_radio
puts "#{@location} stereo is set for Radio"
end
def set_volume(volume)
puts "#{@location} stereo volume set to #{volume}"
end
end
class StereoOnWithCDCommand < Command
attr_reader :stereo
def initialize(stereo)
@stereo = stereo
end
def execute
@stereo.on
@stereo.set_cd
@stereo.set_volume(11)
end
end
class StereotOffCommand < Command
attr_reader :stereo
def initialize(stereo)
@stereo = stereo
end
def execute
@stereo.off
end
end
class CeilingFan
attr_reader :location
attr_reader :level
HIGH = 3
MEDIUM = 2
LOW = 1
OFF = 0
def initialize(location)
@level = HIGH
@location = location
end
def high
@level = HIGH
puts "#{@location} ceiling fan is on high"
end
def medium
@level = MEDIUM
puts "#{@location} ceiling fan is on medium"
end
def low
@level = LOW
puts "#{@location} ceiling fan is on low"
end
def off
puts "#{@location} ceiling fan is off"
end
def speed
@level
end
end
class CeilingFanOnCommand < Command
attr_reader :ceiling_fan
def initialize(ceiling_fan)
@ceiling_fan = ceiling_fan
end
def execute
@ceiling_fan.high
end
end
class CeilingFanOffCommand < Command
attr_reader :ceiling_fan
attr_reader :prevSpeed
def initialize(ceiling_fan)
@ceiling_fan = ceiling_fan
end
def execute
@prevSpeed = @ceiling_fan.speed
@ceiling_fan.off
end
def undo
case @prevSpeed
when CeilingFan::HIGH
@ceiling_fan.high
when CeilingFan::MEDIUM
@ceiling_fan.medium
when CeilingFan::LOW
@ceiling_fan.low
when CeilingFan::OFF
@ceiling_fan.off
end
end
end
class CeilingFanHighCommand < Command
attr_reader :ceiling_fan
attr_reader :prevSpeed
def initialize(ceiling_fan)
@ceiling_fan = ceiling_fan
end
def execute
@prevSpeed = @ceiling_fan.speed
@ceiling_fan.high
end
def undo
case @prevSpeed
when CeilingFan::HIGH
@ceiling_fan.high
when CeilingFan::MEDIUM
@ceiling_fan.medium
when CeilingFan::LOW
@ceiling_fan.low
when CeilingFan::OFF
@ceiling_fan.off
end
end
end
class RemoteControl
attr_reader :on_commands
attr_reader :off_commands
attr_reader :undo_command
def initialize
@on_commands = Array.new
@off_commands = Array.new
end
def set_command(on_command, off_command)
@on_commands << on_command
@off_commands << off_command
end
def on_button_was_pushed(index)
@on_commands[index].execute
@undo_command = @on_commands[index]
end
def off_button_was_pushed(index)
@off_commands[index].execute
@undo_command = @off_commands[index]
end
def undo_button_was_pushed
@undo_command.undo
end
end
class TV
attr_reader :location
attr_reader :channel
def initialize(location)
@location = location
end
def on
puts "#{@location} TV is on"
end
def off
puts "#{@location} TV is off"
end
def set_input_channel
@channel = 3
put "#{@location} TV channel is set for DVD"
end
end
class TVOnCommand < Command
attr_reader :tv
def initialize(tv)
@tv = tv
end
def execute
@tv.on
end
def undo
@tv.off
end
end
class TVOffCommand < Command
attr_reader :tv
def initialize(tv)
@tv = tv
end
def execute
@tv.off
end
def undo
@tv.on
end
end
class MacroCommand < Command
attr_reader :commands
def initialize(commands)
@commands = commands
end
def execute
@commands.each do |command|
command.execute
end
end
def undo
@commands.each do |command|
command.undo
end
end
end
class Hottub
attr_reader :on
attr_reader :temperature
def initialize
@temperature = Random.new.rand(0..100)
end
def on
@on = true
end
def off
@on = false
end
def circulate
puts "Hottub is bubbling!"
end
def jets_on
puts "Hottub jets are on"
end
def jets_off
puts "Hottub jets are off"
end
def set_temperature(temperature)
if temperature > @temperature
puts "Hottub is heating to a steaming #{temperature} degrees"
else
puts "Hottub is cooling to #{temperature} degrees"
end
@temperature = temperature
end
end
class HottubOnCommand < Command
attr_reader :hottub
def initialize(hottub)
@hottub = hottub
end
def execute
@hottub.on
@hottub.set_temperature(104)
@hottub.circulate
end
def undo
@hottub.off
end
end
class HottubOffCommand < Command
attr_reader :hottub
def initialize(hottub)
@hottub = hottub
end
def execute
@hottub.set_temperature(98)
@hottub.off
end
def undo
@hottub.on
end
end
class Timer
attr_reader :time
def on
puts "Timer is on"
end
def off
puts "Timer is off"
end
def set_time(time)
@time = time
puts "Timer is set to #{@time}"
end
end
class TimerOnCommand < Command
attr_reader :timer
def initialize(timer)
@timer = timer
end
def execute
@timer.on
@timer.set_time("00:00")
end
def undo
@timer.off
end
end
class TimerOffCommand < Command
attr_reader :timer
def initialize(timer)
@timer = timer
end
def execute
@timer.off
end
def undo
@timer.on
@timer.set_time("00:00")
end
end
class TimerOnWithMorningCallCommand < Command
attr_reader :timer
def initialize(timer)
@timer = timer
end
def execute
@timer.on
@timer.set_time("07:00")
end
def undo
@timer.off
end
end
timer = Timer.new
stereo = Stereo.new("bedroom")
timer_on = TimerOnWithMorningCallCommand.new(timer)
timer_off = TimerOffCommand.new(timer)
stereo_on = StereoOnWithCDCommand.new(stereo)
stereo_off = StereotOffCommand.new(stereo)
morning_call_on = Array.new
morning_call_off = Array.new
morning_call_on << timer_on
morning_call_on << stereo_on
morning_call_off << timer_off
morning_call_off << stereo_off
morning_call_on_macro = MacroCommand.new(morning_call_on)
morning_call_off_macro = MacroCommand.new(morning_call_off)
remote = RemoteControl.new
remote.set_command(morning_call_on_macro, morning_call_off_macro)
remote.on_button_was_pushed(0)
remote.off_button_was_pushed(0)