From 2598af8d3acb580f0933994f176cae2b310eac5d Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Sun, 13 Oct 2024 18:23:07 +0200 Subject: [PATCH] added lab4 ex1 --- src/lab4_command/ex1/Car.java | 16 ++++++++ src/lab4_command/ex1/CarCommand.java | 10 +++++ src/lab4_command/ex1/Game.java | 38 +++++++++++++++++++ src/lab4_command/ex1/MobilePhone.java | 25 ++++++++++++ .../ex1/commands/BackwardCommand.java | 20 ++++++++++ .../ex1/commands/ForwardCommand.java | 20 ++++++++++ .../ex1/commands/LeftCommand.java | 20 ++++++++++ .../ex1/commands/RightCommand.java | 20 ++++++++++ 8 files changed, 169 insertions(+) create mode 100644 src/lab4_command/ex1/Car.java create mode 100644 src/lab4_command/ex1/CarCommand.java create mode 100644 src/lab4_command/ex1/Game.java create mode 100644 src/lab4_command/ex1/MobilePhone.java create mode 100644 src/lab4_command/ex1/commands/BackwardCommand.java create mode 100644 src/lab4_command/ex1/commands/ForwardCommand.java create mode 100644 src/lab4_command/ex1/commands/LeftCommand.java create mode 100644 src/lab4_command/ex1/commands/RightCommand.java diff --git a/src/lab4_command/ex1/Car.java b/src/lab4_command/ex1/Car.java new file mode 100644 index 0000000..ce8ad81 --- /dev/null +++ b/src/lab4_command/ex1/Car.java @@ -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"); + } +} diff --git a/src/lab4_command/ex1/CarCommand.java b/src/lab4_command/ex1/CarCommand.java new file mode 100644 index 0000000..105ab99 --- /dev/null +++ b/src/lab4_command/ex1/CarCommand.java @@ -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(); +} diff --git a/src/lab4_command/ex1/Game.java b/src/lab4_command/ex1/Game.java new file mode 100644 index 0000000..95d903a --- /dev/null +++ b/src/lab4_command/ex1/Game.java @@ -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 actions = new HashMap<>(); + private final Stack 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(); + } +} diff --git a/src/lab4_command/ex1/MobilePhone.java b/src/lab4_command/ex1/MobilePhone.java new file mode 100644 index 0000000..3d38515 --- /dev/null +++ b/src/lab4_command/ex1/MobilePhone.java @@ -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(); + } +} diff --git a/src/lab4_command/ex1/commands/BackwardCommand.java b/src/lab4_command/ex1/commands/BackwardCommand.java new file mode 100644 index 0000000..3e6b3e9 --- /dev/null +++ b/src/lab4_command/ex1/commands/BackwardCommand.java @@ -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(); + } +} diff --git a/src/lab4_command/ex1/commands/ForwardCommand.java b/src/lab4_command/ex1/commands/ForwardCommand.java new file mode 100644 index 0000000..22633d7 --- /dev/null +++ b/src/lab4_command/ex1/commands/ForwardCommand.java @@ -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(); + } +} diff --git a/src/lab4_command/ex1/commands/LeftCommand.java b/src/lab4_command/ex1/commands/LeftCommand.java new file mode 100644 index 0000000..9a0fe9f --- /dev/null +++ b/src/lab4_command/ex1/commands/LeftCommand.java @@ -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(); + } +} diff --git a/src/lab4_command/ex1/commands/RightCommand.java b/src/lab4_command/ex1/commands/RightCommand.java new file mode 100644 index 0000000..042329e --- /dev/null +++ b/src/lab4_command/ex1/commands/RightCommand.java @@ -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(); + } +}