diff --git a/src/edu/kit/informatik/Main.java b/src/edu/kit/informatik/Main.java index d2f2098..4e63f0c 100644 --- a/src/edu/kit/informatik/Main.java +++ b/src/edu/kit/informatik/Main.java @@ -2,7 +2,17 @@ package edu.kit.informatik; import edu.kit.informatik.ui.CommandLine; +/** + * The main class. + * + * @author Arne Keller + * @version 1.0 + */ public class Main { + /** + * Program entry point. + * @param args command-line arguments + */ public static void main(String[] args) { CommandLine.startInteractive(); } diff --git a/src/edu/kit/informatik/model/ModelRailwaySimulation.java b/src/edu/kit/informatik/model/ModelRailwaySimulation.java index 21e5109..29c2995 100644 --- a/src/edu/kit/informatik/model/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/model/ModelRailwaySimulation.java @@ -376,26 +376,23 @@ public class ModelRailwaySimulation { public boolean putTrain(final int trainId, final Vector2D position, final Vector2D direction) throws InvalidInputException { Train train = trains.get(trainId); - if (train == null || !train.isProperTrain() || train.isPlaced()) { - // can only place valid trains that are not already placed - return false; + if (train == null) { + throw new InvalidInputException("train not found"); + } else if (!train.isProperTrain()) { + throw new InvalidInputException("train is not valid"); + } else if (train.isPlaced()) { + throw new InvalidInputException("train is already placed"); + } else if (direction.getX() != 0 && direction.getY() != 0) { + throw new InvalidInputException("invalid train direction"); } - // attempt to place train boolean placed = train.placeOn(railNetwork, position, direction); - - if (placed) { - // now, check for collisions - List> collisions = getStaticCollisions(); - if (!collisions.isEmpty()) { - // remove if colliding - train.removeFromRails(); - return false; - } else { - return true; - } - } else { + // check for collisions + if (placed && !getStaticCollisions().isEmpty()) { + train.removeFromRails(); return false; + } else { + return placed; } } diff --git a/src/edu/kit/informatik/ui/InvalidInputException.java b/src/edu/kit/informatik/ui/InvalidInputException.java index 057ac85..ee11fe1 100644 --- a/src/edu/kit/informatik/ui/InvalidInputException.java +++ b/src/edu/kit/informatik/ui/InvalidInputException.java @@ -8,7 +8,7 @@ package edu.kit.informatik.ui; */ public class InvalidInputException extends Exception { /** - * Construct a new invalid input expection. + * Construct a new invalid input exception with a specific message. * @param message the error message */ public InvalidInputException(final String message) { diff --git a/src/edu/kit/informatik/ui/command/CommandFactory.java b/src/edu/kit/informatik/ui/command/CommandFactory.java index fb0ed1f..39bb93f 100644 --- a/src/edu/kit/informatik/ui/command/CommandFactory.java +++ b/src/edu/kit/informatik/ui/command/CommandFactory.java @@ -49,7 +49,7 @@ public class CommandFactory { private static final String SHOW_TRAIN = "show train"; private static final String PUT_TRAIN = "put train"; private static final Pattern PUT_TRAIN_ARGUMENTS - = Pattern.compile(" \\+?(\\d+) at \\(([+-]?\\d+,[+-]?\\d+)\\) in direction ([+-]?\\d+),([+-]?\\d+)"); + = Pattern.compile(" \\+?(\\d+) at \\(([+-]?\\d+,[+-]?\\d+)\\) in direction ([+-]?\\d+,[+-]?\\d+)"); private static final String STEP = "step"; private static final String POSITIVE_NUMBER = " \\+?\\d+"; @@ -68,11 +68,7 @@ public class CommandFactory { } Vector2D start = Vector2D.parse(matcher.group(1)); Vector2D end = Vector2D.parse(matcher.group(2)); - if (start.getX() == end.getX() || start.getY() == end.getY()) { - return new AddTrack(start, end); - } else { - throw new InvalidInputException("invalid track segment: not a straight line"); - } + return new AddTrack(start, end); } else if (command.startsWith(ADD_SWITCH)) { String arguments = command.substring(ADD_SWITCH.length()); Matcher matcher = ADD_SWITCH_ARGUMENTS.matcher(arguments); @@ -205,12 +201,8 @@ public class CommandFactory { } int id = Integer.parseInt(matcher.group(1)); Vector2D point = Vector2D.parse(matcher.group(2)); - int x = Integer.parseInt(matcher.group(3)); - int y = Integer.parseInt(matcher.group(4)); - if (x != 0 && y != 0) { - throw new InvalidInputException("invalid train direction"); - } - return new PutTrain(id, point, x, y); + Vector2D direction = Vector2D.parse(matcher.group(3)); + return new PutTrain(id, point, direction); } else if (command.startsWith(STEP)) { String argument = command.substring(STEP.length()); if (!argument.matches(" [+-]?\\d+")) { diff --git a/src/edu/kit/informatik/ui/command/DeleteTrain.java b/src/edu/kit/informatik/ui/command/DeleteTrain.java index 21150f9..9f04422 100644 --- a/src/edu/kit/informatik/ui/command/DeleteTrain.java +++ b/src/edu/kit/informatik/ui/command/DeleteTrain.java @@ -11,7 +11,7 @@ import edu.kit.informatik.Terminal; */ public class DeleteTrain extends Command { /** - * Identifer of the train to delete. + * Identifier of the train to delete. */ private final int id; diff --git a/src/edu/kit/informatik/ui/command/PutTrain.java b/src/edu/kit/informatik/ui/command/PutTrain.java index 085d455..8a26b79 100644 --- a/src/edu/kit/informatik/ui/command/PutTrain.java +++ b/src/edu/kit/informatik/ui/command/PutTrain.java @@ -12,28 +12,34 @@ import edu.kit.informatik.ui.InvalidInputException; * @version 1.0 */ public class PutTrain extends Command { + /** + * Identifier of the train to place. + */ private final int id; + /** + * Where to place the train. + */ private final Vector2D point; - private final int x; - private final int y; + /** + * Initial direction of the train. + */ + private final Vector2D direction; /** * Construct a new 'put train' command. * @param id identifier of the train * @param point where to put the train - * @param x initial x direction - * @param y initial y direction + * @param direction initial direction */ - public PutTrain(final int id, final Vector2D point, final int x, final int y) { + public PutTrain(final int id, final Vector2D point, final Vector2D direction) { this.id = id; this.point = point; - this.x = x; - this.y = y; + this.direction = direction; } @Override public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { - if (simulation.putTrain(id, point, new Vector2D(x, y))) { + if (simulation.putTrain(id, point, direction)) { Terminal.printLine("OK"); } else { Terminal.printError("could not place train"); diff --git a/src/edu/kit/informatik/ui/command/SetSwitch.java b/src/edu/kit/informatik/ui/command/SetSwitch.java index d0126d1..e3a78bf 100644 --- a/src/edu/kit/informatik/ui/command/SetSwitch.java +++ b/src/edu/kit/informatik/ui/command/SetSwitch.java @@ -11,7 +11,13 @@ import edu.kit.informatik.Terminal; * @version 1.0 */ public class SetSwitch extends Command { + /** + * Identifier of the switch to configure. + */ private final int id; + /** + * Position to set the switch to. + */ private final Vector2D point; /** @@ -21,7 +27,8 @@ public class SetSwitch extends Command { */ public SetSwitch(final int id, final Vector2D point) { this.id = id; - this.point = point; + // make sure caller can not modify internal state + this.point = new Vector2D(point); } @Override