diff --git a/command/remoteundostatusmacro/app.d b/command/remoteundostatusmacro/app.d new file mode 100644 index 0000000..8940c5e --- /dev/null +++ b/command/remoteundostatusmacro/app.d @@ -0,0 +1,54 @@ +module command.remoteundostatusmacro.app; + +import command.remoteundostatusmacro.remotecontrol; +import command.remoteundostatusmacro.lightoncommand; +import command.remoteundostatusmacro.lightoffcommand; +import command.remoteundostatusmacro.light; +import command.remoteundostatusmacro.tv; +import command.remoteundostatusmacro.tvoffcommand; +import command.remoteundostatusmacro.tvoncommand; +import command.remoteundostatusmacro.hottub; +import command.remoteundostatusmacro.hottuboffcommand; +import command.remoteundostatusmacro.hottuboncommand; +import command.remoteundostatusmacro.stereoonwithcdcommand; +import command.remoteundostatusmacro.stereo; +import command.remoteundostatusmacro.stereooffcommand; +import command.remoteundostatusmacro.command; +import command.remoteundostatusmacro.macrocommand; +import std.stdio : writeln; + +void main() +{ + auto remoteControl = new RemoteControl(); + + auto light = new Light("Living Room"); + auto tv = new TV("Living Room"); + auto stereo = new Stereo("Living Room"); + auto hottub = new Hottub(); + + Command[] partyOn = cast(Command[])[ + new LightOnCommand(light), + new StereoOnWithCDCommand(stereo), + new TVOnCommand(tv), + new HottubOnCommand(hottub) + ]; + Command[] partyOff = cast(Command[])[ + new LightOffCommand(light), + new StereoOffCommand(stereo), + new TVOffCommand(tv), + new HottubOffCommand(hottub) + ]; + + remoteControl.setCommand(0, new MacroCommand(partyOn), new MacroCommand(partyOff)); + + writeln(remoteControl); + writeln("--- Pushing Macro On ---"); + remoteControl.onButtonWasPushed(0); + writeln("--- Pushing Macro Off ---"); + remoteControl.offButtonWasPushed(0); + writeln(remoteControl); + remoteControl.undoButtonWasPushed(); + writeln(remoteControl); + remoteControl.onButtonWasPushed(0); + writeln(remoteControl); +} \ No newline at end of file diff --git a/command/remoteundostatusmacro/command.d b/command/remoteundostatusmacro/command.d new file mode 100644 index 0000000..350ed3d --- /dev/null +++ b/command/remoteundostatusmacro/command.d @@ -0,0 +1,7 @@ +module command.remoteundostatusmacro.command; + +interface Command +{ + void execute(); + void undo(); +} diff --git a/command/remoteundostatusmacro/hottub.d b/command/remoteundostatusmacro/hottub.d new file mode 100644 index 0000000..833a90a --- /dev/null +++ b/command/remoteundostatusmacro/hottub.d @@ -0,0 +1,58 @@ +module command.remoteundostatusmacro.hottub; + +import std.stdio : writeln; +import std.conv : to; + +class Hottub +{ + private bool isOn; + private int temperature; + + void on() + { + isOn = true; + } + + void off() + { + isOn = false; + } + + void circulate() + { + if (isOn) + { + writeln("Hottub is bubbling!"); + } + } + + void jetsOn() + { + if (isOn) + { + writeln("Hottub jets are on"); + } + } + + void jetsOff() + { + if (isOn) + { + writeln("Hottub jets are off"); + } + } + + void setTemperature(int temperature) + { + if (temperature > this.temperature) + { + writeln("Hottub is heating to a steaming " ~ temperature.to!string ~ " degrees"); + } + else + { + writeln("Hottub is cooling to " ~ temperature.to!string ~ " degrees"); + } + + this.temperature = temperature; + } +} diff --git a/command/remoteundostatusmacro/hottuboffcommand.d b/command/remoteundostatusmacro/hottuboffcommand.d new file mode 100644 index 0000000..b146284 --- /dev/null +++ b/command/remoteundostatusmacro/hottuboffcommand.d @@ -0,0 +1,25 @@ +module command.remoteundostatusmacro.hottuboffcommand; + +import command.remoteundostatusmacro.command; +import command.remoteundostatusmacro.hottub; + +class HottubOffCommand : Command +{ + Hottub hottub; + + this(Hottub hottub) + { + this.hottub = hottub; + } + + void execute() + { + hottub.setTemperature(98); + hottub.off(); + } + + void undo() + { + hottub.on(); + } +} diff --git a/command/remoteundostatusmacro/hottuboncommand.d b/command/remoteundostatusmacro/hottuboncommand.d new file mode 100644 index 0000000..d50a2ec --- /dev/null +++ b/command/remoteundostatusmacro/hottuboncommand.d @@ -0,0 +1,26 @@ +module command.remoteundostatusmacro.hottuboncommand; + +import command.remoteundostatusmacro.command; +import command.remoteundostatusmacro.hottub; + +class HottubOnCommand : Command +{ + Hottub hottub; + + this(Hottub hottub) + { + this.hottub = hottub; + } + + void execute() + { + hottub.on(); + hottub.setTemperature(104); + hottub.circulate(); + } + + void undo() + { + hottub.off(); + } +} diff --git a/command/remoteundostatusmacro/light.d b/command/remoteundostatusmacro/light.d new file mode 100644 index 0000000..38d12c3 --- /dev/null +++ b/command/remoteundostatusmacro/light.d @@ -0,0 +1,23 @@ +module command.remoteundostatusmacro.light; + +import std.stdio : writeln; + +class Light +{ + private string location; + + this(string location) + { + this.location = location; + } + + void on() + { + writeln(location ~ " light is On"); + } + + void off() + { + writeln(location ~ " light is Off"); + } +} diff --git a/command/remoteundostatusmacro/lightoffcommand.d b/command/remoteundostatusmacro/lightoffcommand.d new file mode 100644 index 0000000..735dbf2 --- /dev/null +++ b/command/remoteundostatusmacro/lightoffcommand.d @@ -0,0 +1,24 @@ +module command.remoteundostatusmacro.lightoffcommand; + +import command.remoteundostatusmacro.command; +import command.remoteundostatusmacro.light; + +class LightOffCommand : Command +{ + Light light; + + this(Light light) + { + this.light = light; + } + + void execute() + { + light.off(); + } + + void undo() + { + light.on(); + } +} diff --git a/command/remoteundostatusmacro/lightoncommand.d b/command/remoteundostatusmacro/lightoncommand.d new file mode 100644 index 0000000..265dc71 --- /dev/null +++ b/command/remoteundostatusmacro/lightoncommand.d @@ -0,0 +1,24 @@ +module command.remoteundostatusmacro.lightoncommand; + +import command.remoteundostatusmacro.command; +import command.remoteundostatusmacro.light; + +class LightOnCommand : Command +{ + Light light; + + this(Light light) + { + this.light = light; + } + + void execute() + { + light.on(); + } + + void undo() + { + light.off(); + } +} diff --git a/command/remoteundostatusmacro/macrocommand.d b/command/remoteundostatusmacro/macrocommand.d new file mode 100644 index 0000000..b3b4315 --- /dev/null +++ b/command/remoteundostatusmacro/macrocommand.d @@ -0,0 +1,29 @@ +module command.remoteundostatusmacro.macrocommand; + +import command.remoteundostatusmacro.command; + +class MacroCommand : Command +{ + Command[] commands; + + this(Command[] commands) + { + this.commands = commands; + } + + void execute() + { + foreach (val; commands) + { + val.execute(); + } + } + + void undo() + { + foreach (val; commands) + { + val.undo(); + } + } +} diff --git a/command/remoteundostatusmacro/nocommand.d b/command/remoteundostatusmacro/nocommand.d new file mode 100644 index 0000000..54fc006 --- /dev/null +++ b/command/remoteundostatusmacro/nocommand.d @@ -0,0 +1,9 @@ +module command.remoteundostatusmacro.nocommand; + +import command.remoteundostatusmacro.command; + +class NoCommand : Command +{ + void execute() {} + void undo() {} +} diff --git a/command/remoteundostatusmacro/remotecontrol.d b/command/remoteundostatusmacro/remotecontrol.d new file mode 100644 index 0000000..d058881 --- /dev/null +++ b/command/remoteundostatusmacro/remotecontrol.d @@ -0,0 +1,63 @@ +module command.remoteundostatusmacro.remotecontrol; + +import command.remoteundostatusmacro.command; +import command.remoteundostatusmacro.nocommand; +import std.conv : to; +import std.stdio : writeln; +import std.algorithm.mutation : fill; +import std.array : split, back; +import std.format; + +class RemoteControl +{ + Command[] onCommands; + Command[] offCommands; + Command undoCommand; + + this() + { + onCommands = new Command[7]; + offCommands = new Command[7]; + Command noCommand = new NoCommand(); + fill(onCommands, noCommand); + fill(offCommands, noCommand); + undoCommand = noCommand; + } + + void setCommand(int slot, Command onCommand, Command offCommand) + { + onCommands[slot] = onCommand; + offCommands[slot] = offCommand; + } + + void onButtonWasPushed(int slot) + { + onCommands[slot].execute(); + undoCommand = onCommands[slot]; + } + + void offButtonWasPushed(int slot) + { + offCommands[slot].execute(); + undoCommand = offCommands[slot]; + } + + void undoButtonWasPushed() + { + undoCommand.undo(); + } + + override string toString() const + { + string s = "\n------ Remote Control -------\n"; + for (int i = 0; i < 7; ++i) + { + s ~= "[slot " ~ i.to!string ~ "] " + ~ format("%23s", (cast(Object)onCommands[i]).classinfo.name.split(".").back()) + ~ format("%23s", (cast(Object)offCommands[i]).classinfo.name.split(".").back()) + ~ "\n"; + } + + return s ~ "[undo] " ~ (cast(Object)undoCommand).classinfo.name.split(".").back() ~ "\n"; + } +} diff --git a/command/remoteundostatusmacro/stereo.d b/command/remoteundostatusmacro/stereo.d new file mode 100644 index 0000000..6493fdb --- /dev/null +++ b/command/remoteundostatusmacro/stereo.d @@ -0,0 +1,44 @@ +module command.remoteundostatusmacro.stereo; + +import std.stdio : writeln; +import std.conv : to; + +class Stereo +{ + string location; + + this(string location) + { + this.location = location; + } + + void on() + { + writeln(location ~ " stereo is on"); + } + + void off() + { + writeln(location ~ " stereo is off"); + } + + void setCD() + { + writeln(location ~ " stereo is set for CD input"); + } + + void setDVD() + { + writeln(location ~ " stereo is set for DVD input"); + } + + void setRadio() + { + writeln(location ~ " stereo is set for Radio"); + } + + void setVolume(int volume) + { + writeln(location ~ " stereo volume set to " ~ volume.to!string); + } +} diff --git a/command/remoteundostatusmacro/stereooffcommand.d b/command/remoteundostatusmacro/stereooffcommand.d new file mode 100644 index 0000000..cfeb708 --- /dev/null +++ b/command/remoteundostatusmacro/stereooffcommand.d @@ -0,0 +1,26 @@ +module command.remoteundostatusmacro.stereooffcommand; + +import command.remoteundostatusmacro.command; +import command.remoteundostatusmacro.stereo; + +class StereoOffCommand : Command +{ + Stereo stereo; + + this(Stereo stereo) + { + this.stereo = stereo; + } + + void execute() + { + stereo.off(); + } + + void undo() + { + stereo.on(); + stereo.setCD(); + stereo.setVolume(11); + } +} diff --git a/command/remoteundostatusmacro/stereoonwithcdcommand.d b/command/remoteundostatusmacro/stereoonwithcdcommand.d new file mode 100644 index 0000000..32171c9 --- /dev/null +++ b/command/remoteundostatusmacro/stereoonwithcdcommand.d @@ -0,0 +1,26 @@ +module command.remoteundostatusmacro.stereoonwithcdcommand; + +import command.remoteundostatusmacro.command; +import command.remoteundostatusmacro.stereo; + +class StereoOnWithCDCommand : Command +{ + Stereo stereo; + + this(Stereo stereo) + { + this.stereo = stereo; + } + + void execute() + { + stereo.on(); + stereo.setCD(); + stereo.setVolume(11); + } + + void undo() + { + stereo.off(); + } +} diff --git a/command/remoteundostatusmacro/tv.d b/command/remoteundostatusmacro/tv.d new file mode 100644 index 0000000..cee24af --- /dev/null +++ b/command/remoteundostatusmacro/tv.d @@ -0,0 +1,30 @@ +module command.remoteundostatusmacro.tv; + +import std.stdio : writeln; + +class TV +{ + private string location; + private int channel; + + this(string location) + { + this.location = location; + } + + void on() + { + writeln(location ~ " TV is on"); + } + + void off() + { + writeln(location ~ " TV is off"); + } + + void setInputChannel() + { + this.channel = 3; + writeln(location ~ " TV channel is set for DVD"); + } +} diff --git a/command/remoteundostatusmacro/tvoffcommand.d b/command/remoteundostatusmacro/tvoffcommand.d new file mode 100644 index 0000000..21e7170 --- /dev/null +++ b/command/remoteundostatusmacro/tvoffcommand.d @@ -0,0 +1,24 @@ +module command.remoteundostatusmacro.tvoffcommand; + +import command.remoteundostatusmacro.command; +import command.remoteundostatusmacro.tv; + +class TVOffCommand : Command +{ + TV tv; + + this(TV tv) + { + this.tv = tv; + } + + void execute() + { + tv.off(); + } + + void undo() + { + tv.on(); + } +} diff --git a/command/remoteundostatusmacro/tvoncommand.d b/command/remoteundostatusmacro/tvoncommand.d new file mode 100644 index 0000000..9bca6a4 --- /dev/null +++ b/command/remoteundostatusmacro/tvoncommand.d @@ -0,0 +1,25 @@ +module command.remoteundostatusmacro.tvoncommand; + +import command.remoteundostatusmacro.command; +import command.remoteundostatusmacro.tv; + +class TVOnCommand : Command +{ + TV tv; + + this(TV tv) + { + this.tv = tv; + } + + void execute() + { + tv.on(); + tv.setInputChannel(); + } + + void undo() + { + tv.off(); + } +}