diff --git a/command/remoteundostatus/app.d b/command/remoteundostatus/app.d new file mode 100644 index 0000000..3c9b6fa --- /dev/null +++ b/command/remoteundostatus/app.d @@ -0,0 +1,36 @@ +module command.remoteundostatus.app; + +import command.remoteundostatus.remotecontrol; +import command.remoteundostatus.lightoncommand; +import command.remoteundostatus.lightoffcommand; +import command.remoteundostatus.light; +import command.remoteundostatus.ceilingfan; +import command.remoteundostatus.ceilingfanoffcommand; +import command.remoteundostatus.ceilingfanhighcommand; +import command.remoteundostatus.ceilingfanmediumcommand; +import command.remoteundostatus.ceilingfanlowcommand; +import command.remoteundostatus.garagedoorupcommand; +import command.remoteundostatus.garagedoordowncommand; +import command.remoteundostatus.garagedoor; +import command.remoteundostatus.stereoonwithcdcommand; +import command.remoteundostatus.stereo; +import command.remoteundostatus.stereooffcommand; +import std.stdio : writeln; + +void main() +{ + auto remoteControl = new RemoteControl(); + + auto ceilingFan = new CeilingFan("Living Room"); + + remoteControl.setCommand(0, new CeilingFanMediumCommand(ceilingFan), new CeilingFanOffCommand(ceilingFan)); + remoteControl.setCommand(1, new CeilingFanHighCommand(ceilingFan), new CeilingFanOffCommand(ceilingFan)); + + remoteControl.onButtonWasPushed(0); + remoteControl.offButtonWasPushed(0); + writeln(remoteControl); + remoteControl.undoButtonWasPushed(); + remoteControl.onButtonWasPushed(1); + writeln(remoteControl); + remoteControl.undoButtonWasPushed(); +} \ No newline at end of file diff --git a/command/remoteundostatus/ceilingfan.d b/command/remoteundostatus/ceilingfan.d new file mode 100644 index 0000000..fc2e9b7 --- /dev/null +++ b/command/remoteundostatus/ceilingfan.d @@ -0,0 +1,49 @@ +module command.remoteundostatus.ceilingfan; + +import std.stdio : writeln; + +class CeilingFan +{ + private string location; + private int speed; + + static const int HIGH = 3; + static const int MEDIUM = 2; + static const int LOW = 1; + static const int OFF = 0; + + this(string location) + { + this.location = location; + } + + void high() + { + speed = HIGH; + writeln(location ~ " ceiling fan is on high"); + + } + + void medium() + { + speed = MEDIUM; + writeln(location ~ " ceiling fan is on medium"); + } + + void low() + { + speed = LOW; + writeln(location ~ " ceiling fan is on low"); + } + + void off() + { + speed = OFF; + writeln(location ~ " ceiling fan is off"); + } + + int getSpeed() + { + return speed; + } +} diff --git a/command/remoteundostatus/ceilingfanhighcommand.d b/command/remoteundostatus/ceilingfanhighcommand.d new file mode 100644 index 0000000..c591fe4 --- /dev/null +++ b/command/remoteundostatus/ceilingfanhighcommand.d @@ -0,0 +1,41 @@ +module command.remoteundostatus.ceilingfanhighcommand; + +import command.remoteundostatus.command; +import command.remoteundostatus.ceilingfan; + +class CeilingFanHighCommand : Command +{ + CeilingFan ceilingFan; + int prevSpeed; + + this(CeilingFan ceilingFan) + { + this.ceilingFan = ceilingFan; + } + + void execute() + { + prevSpeed = ceilingFan.getSpeed(); + ceilingFan.high(); + } + + void undo() + { + switch (prevSpeed) + { + case CeilingFan.HIGH: + ceilingFan.high(); + break; + case CeilingFan.MEDIUM: + ceilingFan.medium(); + break; + case CeilingFan.LOW: + ceilingFan.low(); + break; + case CeilingFan.OFF: + ceilingFan.off(); + break; + default: break; + } + } +} diff --git a/command/remoteundostatus/ceilingfanlowcommand.d b/command/remoteundostatus/ceilingfanlowcommand.d new file mode 100644 index 0000000..a815785 --- /dev/null +++ b/command/remoteundostatus/ceilingfanlowcommand.d @@ -0,0 +1,41 @@ +module command.remoteundostatus.ceilingfanlowcommand; + +import command.remoteundostatus.command; +import command.remoteundostatus.ceilingfan; + +class CeilingFanLowCommand : Command +{ + CeilingFan ceilingFan; + int prevSpeed; + + this(CeilingFan ceilingFan) + { + this.ceilingFan = ceilingFan; + } + + void execute() + { + prevSpeed = ceilingFan.getSpeed(); + ceilingFan.low(); + } + + void undo() + { + switch (prevSpeed) + { + case CeilingFan.HIGH: + ceilingFan.high(); + break; + case CeilingFan.MEDIUM: + ceilingFan.medium(); + break; + case CeilingFan.LOW: + ceilingFan.low(); + break; + case CeilingFan.OFF: + ceilingFan.off(); + break; + default: break; + } + } +} diff --git a/command/remoteundostatus/ceilingfanmediumcommand.d b/command/remoteundostatus/ceilingfanmediumcommand.d new file mode 100644 index 0000000..de974b0 --- /dev/null +++ b/command/remoteundostatus/ceilingfanmediumcommand.d @@ -0,0 +1,41 @@ +module command.remoteundostatus.ceilingfanmediumcommand; + +import command.remoteundostatus.command; +import command.remoteundostatus.ceilingfan; + +class CeilingFanMediumCommand : Command +{ + CeilingFan ceilingFan; + int prevSpeed; + + this(CeilingFan ceilingFan) + { + this.ceilingFan = ceilingFan; + } + + void execute() + { + prevSpeed = ceilingFan.getSpeed(); + ceilingFan.medium(); + } + + void undo() + { + switch (prevSpeed) + { + case CeilingFan.HIGH: + ceilingFan.high(); + break; + case CeilingFan.MEDIUM: + ceilingFan.medium(); + break; + case CeilingFan.LOW: + ceilingFan.low(); + break; + case CeilingFan.OFF: + ceilingFan.off(); + break; + default: break; + } + } +} diff --git a/command/remoteundostatus/ceilingfanoffcommand.d b/command/remoteundostatus/ceilingfanoffcommand.d new file mode 100644 index 0000000..3e5e174 --- /dev/null +++ b/command/remoteundostatus/ceilingfanoffcommand.d @@ -0,0 +1,41 @@ +module command.remoteundostatus.ceilingfanoffcommand; + +import command.remoteundostatus.command; +import command.remoteundostatus.ceilingfan; + +class CeilingFanOffCommand : Command +{ + CeilingFan ceilingFan; + int prevSpeed; + + this(CeilingFan ceilingFan) + { + this.ceilingFan = ceilingFan; + } + + void execute() + { + prevSpeed = ceilingFan.getSpeed(); + ceilingFan.off(); + } + + void undo() + { + switch (prevSpeed) + { + case CeilingFan.HIGH: + ceilingFan.high(); + break; + case CeilingFan.MEDIUM: + ceilingFan.medium(); + break; + case CeilingFan.LOW: + ceilingFan.low(); + break; + case CeilingFan.OFF: + ceilingFan.off(); + break; + default: break; + } + } +} diff --git a/command/remoteundostatus/command.d b/command/remoteundostatus/command.d new file mode 100644 index 0000000..a1945ae --- /dev/null +++ b/command/remoteundostatus/command.d @@ -0,0 +1,7 @@ +module command.remoteundostatus.command; + +interface Command +{ + void execute(); + void undo(); +} diff --git a/command/remoteundostatus/garagedoor.d b/command/remoteundostatus/garagedoor.d new file mode 100644 index 0000000..1bf9ac5 --- /dev/null +++ b/command/remoteundostatus/garagedoor.d @@ -0,0 +1,38 @@ +module command.remoteundostatus.garagedoor; + +import std.stdio : writeln; + +class GarageDoor +{ + private string location; + + this(string location) + { + this.location = location; + } + + void up() + { + writeln(location ~ " garage Door is Up"); + } + + void down() + { + writeln(location ~ " garage Door is Down"); + } + + void stop() + { + writeln(location ~ " garage Door is Stopped"); + } + + void lightOn() + { + writeln(location ~ " garage light is on"); + } + + void lightOff() + { + writeln(location ~ " garage light is off"); + } +} diff --git a/command/remoteundostatus/garagedoordowncommand.d b/command/remoteundostatus/garagedoordowncommand.d new file mode 100644 index 0000000..c47de33 --- /dev/null +++ b/command/remoteundostatus/garagedoordowncommand.d @@ -0,0 +1,24 @@ +module command.remoteundostatus.garagedoordowncommand; + +import command.remoteundostatus.command; +import command.remoteundostatus.garagedoor; + +class GarageDoorDownCommand : Command +{ + GarageDoor garageDoor; + + this(GarageDoor garageDoor) + { + this.garageDoor = garageDoor; + } + + void execute() + { + garageDoor.down(); + } + + void undo() + { + garageDoor.up(); + } +} diff --git a/command/remoteundostatus/garagedoorupcommand.d b/command/remoteundostatus/garagedoorupcommand.d new file mode 100644 index 0000000..5f865bb --- /dev/null +++ b/command/remoteundostatus/garagedoorupcommand.d @@ -0,0 +1,24 @@ +module command.remoteundostatus.garagedoorupcommand; + +import command.remoteundostatus.command; +import command.remoteundostatus.garagedoor; + +class GarageDoorUpCommand : Command +{ + GarageDoor garageDoor; + + this(GarageDoor garageDoor) + { + this.garageDoor = garageDoor; + } + + void execute() + { + garageDoor.up(); + } + + void undo() + { + garageDoor.down(); + } +} diff --git a/command/remoteundostatus/light.d b/command/remoteundostatus/light.d new file mode 100644 index 0000000..aa0cf4e --- /dev/null +++ b/command/remoteundostatus/light.d @@ -0,0 +1,23 @@ +module command.remoteundostatus.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/remoteundostatus/lightoffcommand.d b/command/remoteundostatus/lightoffcommand.d new file mode 100644 index 0000000..c66f98b --- /dev/null +++ b/command/remoteundostatus/lightoffcommand.d @@ -0,0 +1,24 @@ +module command.remoteundostatus.lightoffcommand; + +import command.remoteundostatus.command; +import command.remoteundostatus.light; + +class LightOffCommand : Command +{ + Light light; + + this(Light light) + { + this.light = light; + } + + void execute() + { + light.off(); + } + + void undo() + { + light.on(); + } +} diff --git a/command/remoteundostatus/lightoncommand.d b/command/remoteundostatus/lightoncommand.d new file mode 100644 index 0000000..24619ea --- /dev/null +++ b/command/remoteundostatus/lightoncommand.d @@ -0,0 +1,24 @@ +module command.remoteundostatus.lightoncommand; + +import command.remoteundostatus.command; +import command.remoteundostatus.light; + +class LightOnCommand : Command +{ + Light light; + + this(Light light) + { + this.light = light; + } + + void execute() + { + light.on(); + } + + void undo() + { + light.off(); + } +} diff --git a/command/remoteundostatus/nocommand.d b/command/remoteundostatus/nocommand.d new file mode 100644 index 0000000..8f81974 --- /dev/null +++ b/command/remoteundostatus/nocommand.d @@ -0,0 +1,9 @@ +module command.remoteundostatus.nocommand; + +import command.remoteundostatus.command; + +class NoCommand : Command +{ + void execute() {} + void undo() {} +} diff --git a/command/remoteundostatus/remotecontrol.d b/command/remoteundostatus/remotecontrol.d new file mode 100644 index 0000000..2366274 --- /dev/null +++ b/command/remoteundostatus/remotecontrol.d @@ -0,0 +1,63 @@ +module command.remoteundostatus.remotecontrol; + +import command.remoteundostatus.command; +import command.remoteundostatus.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/remoteundostatus/stereo.d b/command/remoteundostatus/stereo.d new file mode 100644 index 0000000..09d7cf9 --- /dev/null +++ b/command/remoteundostatus/stereo.d @@ -0,0 +1,44 @@ +module command.remoteundostatus.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/remoteundostatus/stereooffcommand.d b/command/remoteundostatus/stereooffcommand.d new file mode 100644 index 0000000..b1a5fed --- /dev/null +++ b/command/remoteundostatus/stereooffcommand.d @@ -0,0 +1,26 @@ +module command.remoteundostatus.stereooffcommand; + +import command.remoteundostatus.command; +import command.remoteundostatus.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/remoteundostatus/stereoonwithcdcommand.d b/command/remoteundostatus/stereoonwithcdcommand.d new file mode 100644 index 0000000..b69353b --- /dev/null +++ b/command/remoteundostatus/stereoonwithcdcommand.d @@ -0,0 +1,26 @@ +module command.remoteundostatus.stereoonwithcdcommand; + +import command.remoteundostatus.command; +import command.remoteundostatus.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(); + } +}