Make Command an abstract class

This commit is contained in:
Arne Keller 2020-02-17 12:33:26 +01:00
parent fbf80103be
commit efcd3a721b
19 changed files with 39 additions and 21 deletions

View File

@ -10,7 +10,7 @@ import edu.kit.informatik.Terminal;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class AddSwitch implements Command { public class AddSwitch extends Command {
/** /**
* Start position of the switch to add. * Start position of the switch to add.
*/ */
@ -37,6 +37,7 @@ public class AddSwitch implements Command {
this.end2 = new Vector2D(end2); this.end2 = new Vector2D(end2);
} }
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
int id = simulation.addSwitch(start, end1, end2); int id = simulation.addSwitch(start, end1, end2);
if (id == -1) { if (id == -1) {

View File

@ -10,7 +10,7 @@ import edu.kit.informatik.Terminal;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class AddTrack implements Command { public class AddTrack extends Command {
/** /**
* Start position of the new track. * Start position of the new track.
*/ */
@ -31,6 +31,7 @@ public class AddTrack implements Command {
this.end = new Vector2D(end); this.end = new Vector2D(end);
} }
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
if (start.equals(end)) { if (start.equals(end)) {
Terminal.printError("track has length 0"); Terminal.printError("track has length 0");

View File

@ -10,7 +10,7 @@ import edu.kit.informatik.Terminal;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class AddTrain implements Command { public class AddTrain extends Command {
/** /**
* Identifier of the train to add or modify. * Identifier of the train to add or modify.
*/ */
@ -30,6 +30,7 @@ public class AddTrain implements Command {
this.rollingStockId = rollingStockId; this.rollingStockId = rollingStockId;
} }
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
if (simulation.addTrain(trainId, rollingStockId)) { if (simulation.addTrain(trainId, rollingStockId)) {
RollingStock rollingStock = simulation.getRollingStock(rollingStockId); RollingStock rollingStock = simulation.getRollingStock(rollingStockId);

View File

@ -4,15 +4,15 @@ import edu.kit.informatik.model.ModelRailwaySimulation;
/** /**
* Command that can be applied to a simulation. * Command that can be applied to a simulation.
* Commands are implemented as separate classes to avoid a god enum :) * Commands are implemented as separate classes (to avoid a god enum).
* *
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public interface Command { public abstract class Command {
/** /**
* Apply this command to a model railway simulation. * Apply this command to a model railway simulation.
* @param simulation simulation to use * @param simulation simulation to use
*/ */
void apply(ModelRailwaySimulation simulation); public abstract void apply(ModelRailwaySimulation simulation);
} }

View File

@ -10,7 +10,7 @@ import edu.kit.informatik.Terminal;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class CreateCoach implements Command { public class CreateCoach extends Command {
/** /**
* Type of the new coach. * Type of the new coach.
*/ */
@ -46,6 +46,7 @@ public class CreateCoach implements Command {
this.couplingBack = couplingBack; this.couplingBack = couplingBack;
} }
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
int id = simulation.createCoach(type, length, couplingFront, couplingBack); int id = simulation.createCoach(type, length, couplingFront, couplingBack);
if (id == -1) { if (id == -1) {

View File

@ -14,7 +14,7 @@ import edu.kit.informatik.ui.EngineType;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class CreateEngine implements Command { public class CreateEngine extends Command {
/** /**
* Type of the new engine. * Type of the new engine.
*/ */
@ -62,6 +62,7 @@ public class CreateEngine implements Command {
this.couplingBack = couplingBack; this.couplingBack = couplingBack;
} }
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
Engine engine; Engine engine;
switch (type) { switch (type) {

View File

@ -10,7 +10,7 @@ import edu.kit.informatik.model.TrainSet;
* @author Arne Keller * @author Arne Keller
* @version Arne Keller * @version Arne Keller
*/ */
public class CreateTrainSet implements Command { public class CreateTrainSet extends Command {
/** /**
* Series (class) of the new train set. * Series (class) of the new train set.
*/ */
@ -49,6 +49,7 @@ public class CreateTrainSet implements Command {
this.couplingBack = couplingBack; this.couplingBack = couplingBack;
} }
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
TrainSet trainSet = new TrainSet(series, name, length, couplingFront, couplingBack); TrainSet trainSet = new TrainSet(series, name, length, couplingFront, couplingBack);
if (simulation.createTrainSet(trainSet)) { if (simulation.createTrainSet(trainSet)) {

View File

@ -9,7 +9,7 @@ import edu.kit.informatik.Terminal;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class DeleteRollingStock implements Command { public class DeleteRollingStock extends Command {
/** /**
* Identifier of the rolling stock to delete. * Identifier of the rolling stock to delete.
*/ */
@ -23,6 +23,7 @@ public class DeleteRollingStock implements Command {
this.id = id; this.id = id;
} }
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
if (simulation.deleteRollingStock(id)) { if (simulation.deleteRollingStock(id)) {
Terminal.printLine("OK"); Terminal.printLine("OK");

View File

@ -9,7 +9,7 @@ import edu.kit.informatik.Terminal;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class DeleteTrack implements Command { public class DeleteTrack extends Command {
/** /**
* Identifier of the rail to delete. * Identifier of the rail to delete.
*/ */
@ -23,6 +23,7 @@ public class DeleteTrack implements Command {
this.id = id; this.id = id;
} }
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
if (simulation.removeRail(id)) { if (simulation.removeRail(id)) {
Terminal.printLine("OK"); Terminal.printLine("OK");

View File

@ -9,7 +9,7 @@ import edu.kit.informatik.Terminal;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class DeleteTrain implements Command { public class DeleteTrain extends Command {
/** /**
* Identifer of the train to delete. * Identifer of the train to delete.
*/ */
@ -23,6 +23,7 @@ public class DeleteTrain implements Command {
this.id = id; this.id = id;
} }
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
if (simulation.deleteTrain(id)) { if (simulation.deleteTrain(id)) {
Terminal.printLine("OK"); Terminal.printLine("OK");

View File

@ -8,7 +8,8 @@ import edu.kit.informatik.model.ModelRailwaySimulation;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class ListCoaches implements Command { public class ListCoaches extends Command {
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
simulation.printCoaches(); simulation.printCoaches();
} }

View File

@ -8,7 +8,8 @@ import edu.kit.informatik.model.ModelRailwaySimulation;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class ListEngines implements Command { public class ListEngines extends Command {
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
simulation.printEngines(); simulation.printEngines();
} }

View File

@ -8,7 +8,8 @@ import edu.kit.informatik.model.ModelRailwaySimulation;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class ListTracks implements Command { public class ListTracks extends Command {
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
simulation.printTracks(); simulation.printTracks();
} }

View File

@ -8,7 +8,8 @@ import edu.kit.informatik.model.ModelRailwaySimulation;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class ListTrainSets implements Command { public class ListTrainSets extends Command {
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
simulation.printTrainSets(); simulation.printTrainSets();
} }

View File

@ -8,7 +8,8 @@ import edu.kit.informatik.model.ModelRailwaySimulation;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class ListTrains implements Command { public class ListTrains extends Command {
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
simulation.printTrains(); simulation.printTrains();
} }

View File

@ -10,7 +10,7 @@ import edu.kit.informatik.Terminal;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class PutTrain implements Command { public class PutTrain extends Command {
private final int id; private final int id;
private final Vector2D point; private final Vector2D point;
private final int x; private final int x;
@ -30,6 +30,7 @@ public class PutTrain implements Command {
this.y = y; this.y = y;
} }
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
if (simulation.putTrain(id, point, new Vector2D(x, y))) { if (simulation.putTrain(id, point, new Vector2D(x, y))) {
Terminal.printLine("OK"); Terminal.printLine("OK");

View File

@ -10,7 +10,7 @@ import edu.kit.informatik.Terminal;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class SetSwitch implements Command { public class SetSwitch extends Command {
private final int id; private final int id;
private final Vector2D point; private final Vector2D point;
@ -24,6 +24,7 @@ public class SetSwitch implements Command {
this.point = point; this.point = point;
} }
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
if (simulation.setSwitch(id, point)) { if (simulation.setSwitch(id, point)) {
Terminal.printLine("OK"); Terminal.printLine("OK");

View File

@ -8,7 +8,7 @@ import edu.kit.informatik.model.ModelRailwaySimulation;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class ShowTrain implements Command { public class ShowTrain extends Command {
private final int id; private final int id;
/** /**
@ -19,6 +19,7 @@ public class ShowTrain implements Command {
this.id = id; this.id = id;
} }
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
simulation.printTrain(id); simulation.printTrain(id);
} }

View File

@ -8,7 +8,7 @@ import edu.kit.informatik.model.ModelRailwaySimulation;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class Step implements Command { public class Step extends Command {
private final short speed; private final short speed;
/** /**
@ -19,6 +19,7 @@ public class Step implements Command {
this.speed = speed; this.speed = speed;
} }
@Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
simulation.step(speed); simulation.step(speed);
} }