From 949451a1f155346c2e2db31176da8827d6646976 Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 15 Nov 2022 22:11:46 +0300 Subject: [PATCH] remote undo --- command/remote/app.d | 2 +- command/remote/remotecontrol.d | 8 ++- command/remoteundo/app.d | 52 ++++++++++++++++++ command/remoteundo/ceilingfan.d | 48 +++++++++++++++++ command/remoteundo/ceilingfanoffcommand.d | 24 +++++++++ command/remoteundo/ceilingfanoncommand.d | 24 +++++++++ command/remoteundo/command.d | 7 +++ command/remoteundo/garagedoor.d | 38 +++++++++++++ command/remoteundo/garagedoordowncommand.d | 24 +++++++++ command/remoteundo/garagedoorupcommand.d | 24 +++++++++ command/remoteundo/light.d | 23 ++++++++ command/remoteundo/lightoffcommand.d | 24 +++++++++ command/remoteundo/lightoncommand.d | 24 +++++++++ command/remoteundo/nocommand.d | 9 ++++ command/remoteundo/remotecontrol.d | 63 ++++++++++++++++++++++ command/remoteundo/stereo.d | 44 +++++++++++++++ command/remoteundo/stereooffcommand.d | 26 +++++++++ command/remoteundo/stereoonwithcdcommand.d | 26 +++++++++ 18 files changed, 487 insertions(+), 3 deletions(-) create mode 100644 command/remoteundo/app.d create mode 100644 command/remoteundo/ceilingfan.d create mode 100644 command/remoteundo/ceilingfanoffcommand.d create mode 100644 command/remoteundo/ceilingfanoncommand.d create mode 100644 command/remoteundo/command.d create mode 100644 command/remoteundo/garagedoor.d create mode 100644 command/remoteundo/garagedoordowncommand.d create mode 100644 command/remoteundo/garagedoorupcommand.d create mode 100644 command/remoteundo/light.d create mode 100644 command/remoteundo/lightoffcommand.d create mode 100644 command/remoteundo/lightoncommand.d create mode 100644 command/remoteundo/nocommand.d create mode 100644 command/remoteundo/remotecontrol.d create mode 100644 command/remoteundo/stereo.d create mode 100644 command/remoteundo/stereooffcommand.d create mode 100644 command/remoteundo/stereoonwithcdcommand.d diff --git a/command/remote/app.d b/command/remote/app.d index 5133a5d..4b2632f 100644 --- a/command/remote/app.d +++ b/command/remote/app.d @@ -39,7 +39,7 @@ void main() remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff); remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff); - remoteControl.setCommand(2, stereoOnWithCD, stereoOff); + remoteControl.setCommand(3, stereoOnWithCD, stereoOff); remoteControl.setCommand(4, garageDoorUp, garageDoorDown); writeln(remoteControl); diff --git a/command/remote/remotecontrol.d b/command/remote/remotecontrol.d index 797965e..b842b2c 100644 --- a/command/remote/remotecontrol.d +++ b/command/remote/remotecontrol.d @@ -5,6 +5,8 @@ import command.remote.nocommand; import std.conv : to; import std.stdio : writeln; import std.algorithm.mutation : fill; +import std.array : split, back; +import std.format; class RemoteControl { @@ -41,8 +43,10 @@ class RemoteControl string s = "\n------ Remote Control -------\n"; for (int i = 0; i < 7; ++i) { - s ~= "[slot " ~ i.to!string ~ "] " ~ typeof(onCommands[i]).stringof - ~ " " ~ typeof(offCommands[i]).stringof ~ "\n"; + s ~= "[slot " ~ i.to!string ~ "] " + ~ format("%22s", (cast(Object)onCommands[i]).classinfo.name.split(".").back()) + ~ format("%23s", (cast(Object)offCommands[i]).classinfo.name.split(".").back()) + ~ "\n"; } return s; diff --git a/command/remoteundo/app.d b/command/remoteundo/app.d new file mode 100644 index 0000000..fed0b16 --- /dev/null +++ b/command/remoteundo/app.d @@ -0,0 +1,52 @@ +module command.remoteundo.app; + +import command.remoteundo.remotecontrol; +import command.remoteundo.lightoncommand; +import command.remoteundo.lightoffcommand; +import command.remoteundo.light; +import command.remoteundo.ceilingfan; +import command.remoteundo.ceilingfanoffcommand; +import command.remoteundo.ceilingfanoncommand; +import command.remoteundo.garagedoorupcommand; +import command.remoteundo.garagedoordowncommand; +import command.remoteundo.garagedoor; +import command.remoteundo.stereoonwithcdcommand; +import command.remoteundo.stereo; +import command.remoteundo.stereooffcommand; +import std.stdio : writeln; + +void main() +{ + auto remoteControl = new RemoteControl(); + + auto livingRoomLight = new Light("Living Room"); + // auto kitchenLight = new Light("Kitchen"); + // auto ceilingFan = new CeilingFan("Living Room"); + // auto garageDoor = new GarageDoor("Garage"); + // auto stereo = new Stereo("Living Room"); + + remoteControl.setCommand(0, new LightOnCommand(livingRoomLight), new LightOffCommand(livingRoomLight)); + // remoteControl.setCommand(1, new LightOnCommand(kitchenLight), new LightOffCommand(kitchenLight)); + // remoteControl.setCommand(2, new CeilingFanOnCommand(ceilingFan), new CeilingFanOffCommand(ceilingFan)); + // remoteControl.setCommand(3, new StereoOnWithCDCommand(stereo), new StereoOffCommand(stereo)); + // remoteControl.setCommand(4, new GarageDoorUpCommand(garageDoor), new GarageDoorDownCommand(garageDoor)); + + // writeln(remoteControl); + + remoteControl.onButtonWasPushed(0); + remoteControl.offButtonWasPushed(0); + writeln(remoteControl); + remoteControl.undoButtonWasPushed(); + remoteControl.offButtonWasPushed(0); + remoteControl.onButtonWasPushed(0); + writeln(remoteControl); + remoteControl.undoButtonWasPushed(); + // remoteControl.onButtonWasPushed(1); + // remoteControl.offButtonWasPushed(1); + // remoteControl.onButtonWasPushed(2); + // remoteControl.offButtonWasPushed(2); + // remoteControl.onButtonWasPushed(3); + // remoteControl.offButtonWasPushed(3); + // remoteControl.onButtonWasPushed(4); + // remoteControl.offButtonWasPushed(4); +} diff --git a/command/remoteundo/ceilingfan.d b/command/remoteundo/ceilingfan.d new file mode 100644 index 0000000..99c5607 --- /dev/null +++ b/command/remoteundo/ceilingfan.d @@ -0,0 +1,48 @@ +module command.remoteundo.ceilingfan; + +import std.stdio : writeln; + +class CeilingFan +{ + private string location; + private int level; + + static int HIGH = 2; + static int MEDIUM = 1; + static int LOW = 0; + + this(string location) + { + this.location = location; + } + + void high() + { + level = HIGH; + writeln(location ~ " ceiling fan is on high"); + + } + + void medium() + { + level = MEDIUM; + writeln(location ~ " ceiling fan is on medium"); + } + + void low() + { + level = LOW; + writeln(location ~ " ceiling fan is on low"); + } + + void off() + { + level = 0; + writeln(location ~ " ceiling fan is off"); + } + + int getSpeed() + { + return level; + } +} diff --git a/command/remoteundo/ceilingfanoffcommand.d b/command/remoteundo/ceilingfanoffcommand.d new file mode 100644 index 0000000..dad5174 --- /dev/null +++ b/command/remoteundo/ceilingfanoffcommand.d @@ -0,0 +1,24 @@ +module command.remoteundo.ceilingfanoffcommand; + +import command.remoteundo.command; +import command.remoteundo.ceilingfan; + +class CeilingFanOffCommand : Command +{ + CeilingFan ceilingFan; + + this(CeilingFan ceilingFan) + { + this.ceilingFan = ceilingFan; + } + + void execute() + { + ceilingFan.off(); + } + + void undo() + { + ceilingFan.high(); + } +} diff --git a/command/remoteundo/ceilingfanoncommand.d b/command/remoteundo/ceilingfanoncommand.d new file mode 100644 index 0000000..8f2b683 --- /dev/null +++ b/command/remoteundo/ceilingfanoncommand.d @@ -0,0 +1,24 @@ +module command.remoteundo.ceilingfanoncommand; + +import command.remoteundo.command; +import command.remoteundo.ceilingfan; + +class CeilingFanOnCommand : Command +{ + CeilingFan ceilingFan; + + this(CeilingFan ceilingFan) + { + this.ceilingFan = ceilingFan; + } + + void execute() + { + ceilingFan.high(); + } + + void undo() + { + ceilingFan.off(); + } +} diff --git a/command/remoteundo/command.d b/command/remoteundo/command.d new file mode 100644 index 0000000..fe1bba1 --- /dev/null +++ b/command/remoteundo/command.d @@ -0,0 +1,7 @@ +module command.remoteundo.command; + +interface Command +{ + void execute(); + void undo(); +} diff --git a/command/remoteundo/garagedoor.d b/command/remoteundo/garagedoor.d new file mode 100644 index 0000000..720f224 --- /dev/null +++ b/command/remoteundo/garagedoor.d @@ -0,0 +1,38 @@ +module command.remoteundo.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/remoteundo/garagedoordowncommand.d b/command/remoteundo/garagedoordowncommand.d new file mode 100644 index 0000000..d9d3c24 --- /dev/null +++ b/command/remoteundo/garagedoordowncommand.d @@ -0,0 +1,24 @@ +module command.remoteundo.garagedoordowncommand; + +import command.remoteundo.command; +import command.remoteundo.garagedoor; + +class GarageDoorDownCommand : Command +{ + GarageDoor garageDoor; + + this(GarageDoor garageDoor) + { + this.garageDoor = garageDoor; + } + + void execute() + { + garageDoor.down(); + } + + void undo() + { + garageDoor.up(); + } +} diff --git a/command/remoteundo/garagedoorupcommand.d b/command/remoteundo/garagedoorupcommand.d new file mode 100644 index 0000000..70cdb0d --- /dev/null +++ b/command/remoteundo/garagedoorupcommand.d @@ -0,0 +1,24 @@ +module command.remoteundo.garagedoorupcommand; + +import command.remoteundo.command; +import command.remoteundo.garagedoor; + +class GarageDoorUpCommand : Command +{ + GarageDoor garageDoor; + + this(GarageDoor garageDoor) + { + this.garageDoor = garageDoor; + } + + void execute() + { + garageDoor.up(); + } + + void undo() + { + garageDoor.down(); + } +} diff --git a/command/remoteundo/light.d b/command/remoteundo/light.d new file mode 100644 index 0000000..ad6f09b --- /dev/null +++ b/command/remoteundo/light.d @@ -0,0 +1,23 @@ +module command.remoteundo.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/remoteundo/lightoffcommand.d b/command/remoteundo/lightoffcommand.d new file mode 100644 index 0000000..638a7e8 --- /dev/null +++ b/command/remoteundo/lightoffcommand.d @@ -0,0 +1,24 @@ +module command.remoteundo.lightoffcommand; + +import command.remoteundo.command; +import command.remoteundo.light; + +class LightOffCommand : Command +{ + Light light; + + this(Light light) + { + this.light = light; + } + + void execute() + { + light.off(); + } + + void undo() + { + light.on(); + } +} diff --git a/command/remoteundo/lightoncommand.d b/command/remoteundo/lightoncommand.d new file mode 100644 index 0000000..1a94203 --- /dev/null +++ b/command/remoteundo/lightoncommand.d @@ -0,0 +1,24 @@ +module command.remoteundo.lightoncommand; + +import command.remoteundo.command; +import command.remoteundo.light; + +class LightOnCommand : Command +{ + Light light; + + this(Light light) + { + this.light = light; + } + + void execute() + { + light.on(); + } + + void undo() + { + light.off(); + } +} diff --git a/command/remoteundo/nocommand.d b/command/remoteundo/nocommand.d new file mode 100644 index 0000000..e81c3fd --- /dev/null +++ b/command/remoteundo/nocommand.d @@ -0,0 +1,9 @@ +module command.remoteundo.nocommand; + +import command.remoteundo.command; + +class NoCommand : Command +{ + void execute() {} + void undo() {} +} diff --git a/command/remoteundo/remotecontrol.d b/command/remoteundo/remotecontrol.d new file mode 100644 index 0000000..7cba07a --- /dev/null +++ b/command/remoteundo/remotecontrol.d @@ -0,0 +1,63 @@ +module command.remoteundo.remotecontrol; + +import command.remoteundo.command; +import command.remoteundo.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("%22s", (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/remoteundo/stereo.d b/command/remoteundo/stereo.d new file mode 100644 index 0000000..167efb9 --- /dev/null +++ b/command/remoteundo/stereo.d @@ -0,0 +1,44 @@ +module command.remoteundo.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/remoteundo/stereooffcommand.d b/command/remoteundo/stereooffcommand.d new file mode 100644 index 0000000..5f79e61 --- /dev/null +++ b/command/remoteundo/stereooffcommand.d @@ -0,0 +1,26 @@ +module command.remoteundo.stereooffcommand; + +import command.remoteundo.command; +import command.remoteundo.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/remoteundo/stereoonwithcdcommand.d b/command/remoteundo/stereoonwithcdcommand.d new file mode 100644 index 0000000..222899c --- /dev/null +++ b/command/remoteundo/stereoonwithcdcommand.d @@ -0,0 +1,26 @@ +module command.remoteundo.stereoonwithcdcommand; + +import command.remoteundo.command; +import command.remoteundo.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(); + } +}