package org.xander.behavioral.command;

public class RemoteControlWithUndo {
    Command[] onCommands;
    Command[] offCommands;
    Command undoCommand;

    public RemoteControlWithUndo() {
        onCommands = new Command[4];
        offCommands = new Command[4];

        Command noCommand = new NoCommand();
        for (int i = 0; i < onCommands.length; 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) {
        if (onCommands[slot] != null) {
            onCommands[slot].execute();
        }
        undoCommand = onCommands[slot];
    }

    public void offButtonWasPushed(int slot) {
        offCommands[slot].execute();
        undoCommand = offCommands[slot];
    }

    public void undoCommandWasPushed() {
        undoCommand.undo();
    }

    @Override
    public String toString() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("\n----- Remote control -----\n");

        for (int i = 0; i < onCommands.length; i++) {
            stringBuffer.append("[slot " + i + "] " + onCommands[i].getClass().getName()
                    + "    " + offCommands[i].getClass().getName() + "\n");
        }

        return stringBuffer.toString();
    }
}
@@ -1105,18 +1105,19 @@
 StringBu
-ff
+ild
 er strin
@@ -1119,18 +1119,19 @@
 stringBu
-ff
+ild
 er = new
@@ -1139,18 +1139,19 @@
 StringBu
-ff
+ild
 er();%0A  
@@ -1156,34 +1156,35 @@
         stringBu
-ff
+ild
 er.append(%22%5Cn---
@@ -1287,18 +1287,19 @@
 stringBu
-ff
+ild
 er.appen
@@ -1439,24 +1439,80 @@
 ;%0A        %7D%0A
+        stringBuilder.append(%22%5Bundo%5D   %22 + undoCommand);
 %0A        ret
@@ -1523,18 +1523,19 @@
 stringBu
-ff
+ild
 er.toStr
@@ -1548,8 +1548,9 @@
 %0A    %7D%0A%7D
+%0A

Update RemoteControlWithUndo.java