-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemoteControl.java
More file actions
67 lines (51 loc) · 1.64 KB
/
RemoteControl.java
File metadata and controls
67 lines (51 loc) · 1.64 KB
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
package command;
import java.util.LinkedList;
/**
* @author Khalid Elshafie <abolkog@gmail.com>
* @created 14/03/2018.
*/
//Invoker
public class RemoteControl {
private Command[] onCommands;
private Command[] offCommands;
private LinkedList<Command> history;
private final int SLOTS = 5;
public RemoteControl() {
onCommands = new Command[SLOTS];
offCommands = new Command[SLOTS];
NoCommand noCommand = new NoCommand();
for (int i = 0; i < SLOTS; i++) {
onCommands[i] =noCommand;
offCommands[i] =noCommand;
}
history = new LinkedList<>();
}
protected void addCommand(int slot, Command onCommand, Command offCommand) {
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
public void onButtonPressed(int slot) {
onCommands[slot].execute();
history.push(onCommands[slot]);
}
public void offButtonPressed(int slot) {
offCommands[slot].execute();
history.push(offCommands[slot]);
}
public void undoButtonPressed() {
if (history.peek() != null) {
history.poll().undo();
}else {
System.out.println("No more history");
}
}
@Override
public String toString() {
System.out.println("---------- Remote Control ----------");
StringBuffer buffer = new StringBuffer();
for(int i =0; i < SLOTS; i++) {
buffer.append(String.format("[Slot %d] %s \t %s%n", i, onCommands[i].getClass().getSimpleName(), offCommands[i].getClass().getSimpleName()));
}
return buffer.toString();
}
}