-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteControl.java
More file actions
52 lines (42 loc) · 1.47 KB
/
RemoteControl.java
File metadata and controls
52 lines (42 loc) · 1.47 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
package CommandPattern.BasicRemote;
public class RemoteControl {
Command[] onCommands;
Command[] offCommands;
Command undoCommand;
public RemoteControl(){
// create an command array with 7 slots
onCommands = new Command[7];
offCommands = new Command[7];
// Set all commands to No Command because the remote hasn't been programmed yet
Command noCommand = new NoCommand();
for (int i=0; i<7;i++){
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
undoCommand = noCommand;
}
public void setCommand(int slot, Command onCommand, Command offCommand){
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
public void onButtonWasPushed(int slot){
onCommands[slot].execute();
undoCommand = onCommands[slot];
}
public void offButtonWasPushed(int slot){
offCommands[slot].execute();
undoCommand = offCommands[slot];
}
public void undoButtonWasPushed(){
undoCommand.undo();
}
public String toString(){
StringBuffer stringBuff = new StringBuffer();
stringBuff.append("\n--------Remote Control ---------\n");
for (int i=0; i< onCommands.length; i++){
stringBuff.append("\nSlot " + i + ": " + onCommands[i] + " " + offCommands[i]);
}
stringBuff.append("\nUndo Command: " + undoCommand);
return stringBuff.toString();
}
}