remote undo

This commit is contained in:
Alexander Zhirov 2022-11-15 22:11:46 +03:00
parent 1744b158c6
commit 949451a1f1
18 changed files with 487 additions and 3 deletions

View File

@ -39,7 +39,7 @@ void main()
remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff); remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);
remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff); remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff);
remoteControl.setCommand(2, stereoOnWithCD, stereoOff); remoteControl.setCommand(3, stereoOnWithCD, stereoOff);
remoteControl.setCommand(4, garageDoorUp, garageDoorDown); remoteControl.setCommand(4, garageDoorUp, garageDoorDown);
writeln(remoteControl); writeln(remoteControl);

View File

@ -5,6 +5,8 @@ import command.remote.nocommand;
import std.conv : to; import std.conv : to;
import std.stdio : writeln; import std.stdio : writeln;
import std.algorithm.mutation : fill; import std.algorithm.mutation : fill;
import std.array : split, back;
import std.format;
class RemoteControl class RemoteControl
{ {
@ -41,8 +43,10 @@ class RemoteControl
string s = "\n------ Remote Control -------\n"; string s = "\n------ Remote Control -------\n";
for (int i = 0; i < 7; ++i) for (int i = 0; i < 7; ++i)
{ {
s ~= "[slot " ~ i.to!string ~ "] " ~ typeof(onCommands[i]).stringof s ~= "[slot " ~ i.to!string ~ "] "
~ " " ~ typeof(offCommands[i]).stringof ~ "\n"; ~ format("%22s", (cast(Object)onCommands[i]).classinfo.name.split(".").back())
~ format("%23s", (cast(Object)offCommands[i]).classinfo.name.split(".").back())
~ "\n";
} }
return s; return s;

52
command/remoteundo/app.d Normal file
View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,7 @@
module command.remoteundo.command;
interface Command
{
void execute();
void undo();
}

View File

@ -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");
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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");
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,9 @@
module command.remoteundo.nocommand;
import command.remoteundo.command;
class NoCommand : Command
{
void execute() {}
void undo() {}
}

View File

@ -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";
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}