added lab4 ex1

This commit is contained in:
Louis Heredero 2024-10-13 18:23:07 +02:00
parent 3a2484a123
commit 2598af8d3a
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
8 changed files with 169 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package lab4_command.ex1;
public class Car {
public void forward() {
System.out.println("Car goes forward");
}
public void backward() {
System.out.println("Car goes backward");
}
public void left() {
System.out.println("Car goes left");
}
public void right() {
System.out.println("Car goes right");
}
}

View File

@ -0,0 +1,10 @@
package lab4_command.ex1;
public abstract class CarCommand {
protected Car car;
public CarCommand(Car car) {
this.car = car;
}
public abstract void execute();
public abstract void undo();
}

View File

@ -0,0 +1,38 @@
package lab4_command.ex1;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class Game {
private final Map<Integer, CarCommand> actions = new HashMap<>();
private final Stack<CarCommand> history = new Stack<>();
public Car car;
public Game() {
car = new Car();
}
public void bindKey(int key, CarCommand command) {
actions.put(key, command);
}
public void unbindKey(int key) {
actions.remove(key);
}
public void pushKey(int key) {
if (actions.containsKey(key)) {
CarCommand action = actions.get(key);
action.execute();
history.push(action);
}
}
public void undo() {
if (history.empty()) {
return;
}
history.pop().undo();
}
}

View File

@ -0,0 +1,25 @@
package lab4_command.ex1;
import lab4_command.ex1.commands.BackwardCommand;
import lab4_command.ex1.commands.ForwardCommand;
import lab4_command.ex1.commands.LeftCommand;
import lab4_command.ex1.commands.RightCommand;
public class MobilePhone {
public static void main(String[] args) {
Game game = new Game();
game.bindKey(0, new ForwardCommand(game.car));
game.bindKey(1, new BackwardCommand(game.car));
game.bindKey(2, new LeftCommand(game.car));
game.bindKey(3, new RightCommand(game.car));
game.pushKey(2);
game.pushKey(2);
game.pushKey(3);
game.pushKey(2);
game.pushKey(0);
game.pushKey(1);
game.undo();
}
}

View File

@ -0,0 +1,20 @@
package lab4_command.ex1.commands;
import lab4_command.ex1.Car;
import lab4_command.ex1.CarCommand;
public class BackwardCommand extends CarCommand {
public BackwardCommand(Car car) {
super(car);
}
@Override
public void execute() {
car.backward();
}
@Override
public void undo() {
car.forward();
}
}

View File

@ -0,0 +1,20 @@
package lab4_command.ex1.commands;
import lab4_command.ex1.Car;
import lab4_command.ex1.CarCommand;
public class ForwardCommand extends CarCommand {
public ForwardCommand(Car car) {
super(car);
}
@Override
public void execute() {
car.forward();
}
@Override
public void undo() {
car.backward();
}
}

View File

@ -0,0 +1,20 @@
package lab4_command.ex1.commands;
import lab4_command.ex1.Car;
import lab4_command.ex1.CarCommand;
public class LeftCommand extends CarCommand {
public LeftCommand(Car car) {
super(car);
}
@Override
public void execute() {
car.left();
}
@Override
public void undo() {
car.right();
}
}

View File

@ -0,0 +1,20 @@
package lab4_command.ex1.commands;
import lab4_command.ex1.Car;
import lab4_command.ex1.CarCommand;
public class RightCommand extends CarCommand {
public RightCommand(Car car) {
super(car);
}
@Override
public void execute() {
car.right();
}
@Override
public void undo() {
car.left();
}
}