Checkstyle

This commit is contained in:
Arne Keller 2020-02-18 19:18:04 +01:00
parent 2125588a0d
commit 137b8359c6
7 changed files with 51 additions and 39 deletions

View File

@ -2,7 +2,17 @@ package edu.kit.informatik;
import edu.kit.informatik.ui.CommandLine; import edu.kit.informatik.ui.CommandLine;
/**
* The main class.
*
* @author Arne Keller
* @version 1.0
*/
public class Main { public class Main {
/**
* Program entry point.
* @param args command-line arguments
*/
public static void main(String[] args) { public static void main(String[] args) {
CommandLine.startInteractive(); CommandLine.startInteractive();
} }

View File

@ -376,26 +376,23 @@ public class ModelRailwaySimulation {
public boolean putTrain(final int trainId, final Vector2D position, final Vector2D direction) public boolean putTrain(final int trainId, final Vector2D position, final Vector2D direction)
throws InvalidInputException { throws InvalidInputException {
Train train = trains.get(trainId); Train train = trains.get(trainId);
if (train == null || !train.isProperTrain() || train.isPlaced()) { if (train == null) {
// can only place valid trains that are not already placed throw new InvalidInputException("train not found");
return false; } 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 // attempt to place train
boolean placed = train.placeOn(railNetwork, position, direction); boolean placed = train.placeOn(railNetwork, position, direction);
// check for collisions
if (placed) { if (placed && !getStaticCollisions().isEmpty()) {
// now, check for collisions
List<HashSet<Train>> collisions = getStaticCollisions();
if (!collisions.isEmpty()) {
// remove if colliding
train.removeFromRails(); train.removeFromRails();
return false; return false;
} else { } else {
return true; return placed;
}
} else {
return false;
} }
} }

View File

@ -8,7 +8,7 @@ package edu.kit.informatik.ui;
*/ */
public class InvalidInputException extends Exception { 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 * @param message the error message
*/ */
public InvalidInputException(final String message) { public InvalidInputException(final String message) {

View File

@ -49,7 +49,7 @@ public class CommandFactory {
private static final String SHOW_TRAIN = "show train"; private static final String SHOW_TRAIN = "show train";
private static final String PUT_TRAIN = "put train"; private static final String PUT_TRAIN = "put train";
private static final Pattern PUT_TRAIN_ARGUMENTS 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 STEP = "step";
private static final String POSITIVE_NUMBER = " \\+?\\d+"; private static final String POSITIVE_NUMBER = " \\+?\\d+";
@ -68,11 +68,7 @@ public class CommandFactory {
} }
Vector2D start = Vector2D.parse(matcher.group(1)); Vector2D start = Vector2D.parse(matcher.group(1));
Vector2D end = Vector2D.parse(matcher.group(2)); Vector2D end = Vector2D.parse(matcher.group(2));
if (start.getX() == end.getX() || start.getY() == end.getY()) {
return new AddTrack(start, end); return new AddTrack(start, end);
} else {
throw new InvalidInputException("invalid track segment: not a straight line");
}
} else if (command.startsWith(ADD_SWITCH)) { } else if (command.startsWith(ADD_SWITCH)) {
String arguments = command.substring(ADD_SWITCH.length()); String arguments = command.substring(ADD_SWITCH.length());
Matcher matcher = ADD_SWITCH_ARGUMENTS.matcher(arguments); Matcher matcher = ADD_SWITCH_ARGUMENTS.matcher(arguments);
@ -205,12 +201,8 @@ public class CommandFactory {
} }
int id = Integer.parseInt(matcher.group(1)); int id = Integer.parseInt(matcher.group(1));
Vector2D point = Vector2D.parse(matcher.group(2)); Vector2D point = Vector2D.parse(matcher.group(2));
int x = Integer.parseInt(matcher.group(3)); Vector2D direction = Vector2D.parse(matcher.group(3));
int y = Integer.parseInt(matcher.group(4)); return new PutTrain(id, point, direction);
if (x != 0 && y != 0) {
throw new InvalidInputException("invalid train direction");
}
return new PutTrain(id, point, x, y);
} else if (command.startsWith(STEP)) { } else if (command.startsWith(STEP)) {
String argument = command.substring(STEP.length()); String argument = command.substring(STEP.length());
if (!argument.matches(" [+-]?\\d+")) { if (!argument.matches(" [+-]?\\d+")) {

View File

@ -11,7 +11,7 @@ import edu.kit.informatik.Terminal;
*/ */
public class DeleteTrain extends Command { public class DeleteTrain extends Command {
/** /**
* Identifer of the train to delete. * Identifier of the train to delete.
*/ */
private final int id; private final int id;

View File

@ -12,28 +12,34 @@ import edu.kit.informatik.ui.InvalidInputException;
* @version 1.0 * @version 1.0
*/ */
public class PutTrain extends Command { public class PutTrain extends Command {
/**
* Identifier of the train to place.
*/
private final int id; private final int id;
/**
* Where to place the train.
*/
private final Vector2D point; 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. * Construct a new 'put train' command.
* @param id identifier of the train * @param id identifier of the train
* @param point where to put the train * @param point where to put the train
* @param x initial x direction * @param direction initial direction
* @param y initial y 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.id = id;
this.point = point; this.point = point;
this.x = x; this.direction = direction;
this.y = y;
} }
@Override @Override
public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { 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"); Terminal.printLine("OK");
} else { } else {
Terminal.printError("could not place train"); Terminal.printError("could not place train");

View File

@ -11,7 +11,13 @@ import edu.kit.informatik.Terminal;
* @version 1.0 * @version 1.0
*/ */
public class SetSwitch extends Command { public class SetSwitch extends Command {
/**
* Identifier of the switch to configure.
*/
private final int id; private final int id;
/**
* Position to set the switch to.
*/
private final Vector2D point; private final Vector2D point;
/** /**
@ -21,7 +27,8 @@ public class SetSwitch extends Command {
*/ */
public SetSwitch(final int id, final Vector2D point) { public SetSwitch(final int id, final Vector2D point) {
this.id = id; this.id = id;
this.point = point; // make sure caller can not modify internal state
this.point = new Vector2D(point);
} }
@Override @Override