Checkstyle

This commit is contained in:
Arne Keller 2020-02-15 17:33:08 +01:00
parent b24b7cd83f
commit 3610cfac8d
28 changed files with 318 additions and 226 deletions

View File

@ -35,6 +35,9 @@ public class CommandLine {
} catch (NumberFormatException e) {
Terminal.printError("number too big");
continue;
} catch (InvalidInputException e) {
Terminal.printError(e.getMessage());
continue;
}
if (command != null) {
try {

View File

@ -0,0 +1,13 @@
package edu.kit.informatik;
/**
* Thrown on invalid user input.
*
* @author Arne Keller
* @version 1.0
*/
public class InvalidInputException extends Exception {
public InvalidInputException(String message) {
super(message);
}
}

View File

@ -16,7 +16,7 @@ public class ModelRailwaySimulation {
* @param end
* @return
*/
public int addTrack(final Point start, final Point end) {
public int addTrack(final Vector2D start, final Vector2D end) {
if (start.distanceTo(end) == 0) {
return -1;
}
@ -52,7 +52,7 @@ public class ModelRailwaySimulation {
* @param end2
* @return
*/
public int addSwitch(final Point start, final Point end1, final Point end2) {
public int addSwitch(final Vector2D start, final Vector2D end1, final Vector2D end2) {
if (start.distanceTo(end1) == 0 || start.distanceTo(end2) == 0 || end1.distanceTo(end2) == 0) {
return -1;
}
@ -173,7 +173,7 @@ public class ModelRailwaySimulation {
* @param position
* @return
*/
public boolean setSwitch(int id, Point position) {
public boolean setSwitch(int id, Vector2D position) {
// TODO Falls Gleiseweichen auf denen ein Zug bereitssteht, geschaltet werden, entgleist der daraufstehende Zug
for (Rail rail : rails) {
if (rail.getIdentifier() == id) {
@ -428,8 +428,8 @@ public class ModelRailwaySimulation {
* @param rawDirection
* @return
*/
public boolean putTrain(final int trainId, final Point position, final Point rawDirection) {
final Point direction = rawDirection.normalized();
public boolean putTrain(final int trainId, final Vector2D position, final Vector2D rawDirection) {
final Vector2D direction = rawDirection.normalized();
Train train = getTrain(trainId);
if (train == null) {
return false;
@ -439,10 +439,10 @@ public class ModelRailwaySimulation {
}
int length = train.getLength();
List<Point> positions = new ArrayList<>();
List<Vector2D> positions = new ArrayList<>();
positions.add(position);
Point positioningDirection = direction.negated();
Point rollingStockPosition = position;
Vector2D positioningDirection = direction.negated();
Vector2D rollingStockPosition = position;
if (length > 10000) {
return false; // TODO: remove!
}
@ -526,9 +526,9 @@ public class ModelRailwaySimulation {
if (!train.isPlaced()) {
continue;
}
Point position = train.getFrontPosition();
Point direction = train.getDirection();
Point nextPosition = move(position, direction);
Vector2D position = train.getFrontPosition();
Vector2D direction = train.getDirection();
Vector2D nextPosition = move(position, direction);
if (nextPosition == null
|| (train.isOnPosition(nextPosition)
&& !train.getBackPosition().equals(train.getFrontPosition()))) {
@ -593,10 +593,10 @@ public class ModelRailwaySimulation {
if (!train.isPlaced()) {
continue;
}
Point front = train.getFrontPosition();
Point position = train.getBackPosition();
Point direction = train.getBackDirection();
Point nextPosition = move(position, direction);
Vector2D front = train.getFrontPosition();
Vector2D position = train.getBackPosition();
Vector2D direction = train.getBackDirection();
Vector2D nextPosition = move(position, direction);
if (nextPosition == null
|| (train.isOnPosition(nextPosition) && !train.getFrontPosition().equals(nextPosition))) {
collisions.add(new HashSet<>(Arrays.asList(train)));
@ -648,10 +648,10 @@ public class ModelRailwaySimulation {
* @param direction has to be (0,1) (0,-1) (1,0) (-1,0) will be modified if necessary
* @return
*/
private Point move(final Point position, final Point direction) {
private Vector2D move(final Vector2D position, final Vector2D direction) {
Rail containingRail = findContainingRail(position);
if (containingRail != null) {
return position.moveBy(direction);
return position.add(direction);
}
Rail[] touchingRails = findTouchingRails(position);
if (touchingRails.length == 0) {
@ -660,36 +660,36 @@ public class ModelRailwaySimulation {
if (touchingRails.length == 1) {
// at the end of a rail
// either derail or move backwards
Point nextPosition = position.moveBy(direction);
Vector2D nextPosition = position.add(direction);
if (!touchingRails[0].contains(nextPosition)) {
return null;
} else {
return nextPosition;
}
}
Point nextPosition = position.moveBy(direction);
Vector2D nextPosition = position.add(direction);
if (touchingRails[0].contains(nextPosition) || touchingRails[0].connectsTo(nextPosition)) {
return nextPosition;
} else if (touchingRails[1].contains(nextPosition)) {
Point nextDirection = touchingRails[1].getDirectionFrom(position);
direction.x = nextDirection.x;
direction.y = nextDirection.y;
Vector2D nextDirection = touchingRails[1].getDirectionFrom(position);
direction.setX(nextDirection.getX());
direction.setY(nextDirection.getY());
return nextPosition;
}
Point previousPosition = position.moveBy(direction.negated());
Vector2D previousPosition = position.add(direction.negated());
Rail nextRail = (touchingRails[0].contains(previousPosition)
|| touchingRails[0].canConnectTo(previousPosition)) ? touchingRails[1] : touchingRails[0];
Point nextDirection = nextRail.getDirectionFrom(position);
Vector2D nextDirection = nextRail.getDirectionFrom(position);
if (nextDirection != null) {
direction.x = nextDirection.x;
direction.y = nextDirection.y;
return position.moveBy(direction);
direction.setX(nextDirection.getX());
direction.setY(nextDirection.getY());
return position.add(direction);
} else {
return null;
}
}
private Rail findContainingRail(final Point position) {
private Rail findContainingRail(final Vector2D position) {
for (Rail rail : rails) {
if (rail.contains(position)) {
return rail;
@ -698,7 +698,7 @@ public class ModelRailwaySimulation {
return null;
}
private Rail[] findTouchingRails(final Point position) {
private Rail[] findTouchingRails(final Vector2D position) {
return rails.stream().filter((rail) -> rail.canConnectTo(position)).toArray(Rail[]::new);
}

View File

@ -1,83 +0,0 @@
package edu.kit.informatik;
import java.util.Objects;
public class Point {
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
/**
* assumes good input like x,y
* @param input
* @return
*/
public static Point parse(final String input) {
String[] coordinates = input.split(",", 2);
int x = Integer.parseInt(coordinates[0]);
int y = Integer.parseInt(coordinates[1]);
return new Point(x, y);
}
@Override
public String toString() {
return String.format("(%d,%d)", x, y);
}
/**
* Taxicab distance to other point
* @param other
* @return
*/
public int distanceTo(final Point other) {
return Math.abs(this.x - other.x) + Math.abs(this.y - other.y);
}
/**
* normalize x and y *separately*
* @return
*/
public Point normalized() {
if (x >= 0 && x <= 1 && y >= 0 && y <= 1) {
return this;
} else if (x != 0 && y != 0) {
return new Point(x / Math.abs(x), y / Math.abs(y));
} else if (x != 0) {
return new Point(x / Math.abs(x), 0);
} else { // y != 0
return new Point(0, y / Math.abs(y));
}
}
public Point negated() {
return new Point(-x, -y);
}
public Point subtract(Point other) {
return new Point(x - other.x, y - other.y);
}
public Point moveBy(final Point movement) {
return new Point(x + movement.x, y + movement.y);
}
@Override
public boolean equals(final Object obj) {
if (obj != null && getClass().equals(obj.getClass())) {
final Point point = (Point) obj;
return x == point.x && y == point.y;
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}

View File

@ -17,13 +17,13 @@ public abstract class Rail {
* @param point point to check for connection
* @return whether the rail currently connects to the point
*/
public abstract boolean connectsTo(Point point);
public abstract boolean connectsTo(Vector2D point);
/**
* @param point point to check for connection
* @return whether the rail can connect to the point
*/
public abstract boolean canConnectTo(Point point);
public abstract boolean canConnectTo(Vector2D point);
/**
* @param rail rail to check for connection
@ -36,7 +36,7 @@ public abstract class Rail {
* @param position
* @return
*/
public abstract boolean switchTo(Point position);
public abstract boolean switchTo(Vector2D position);
/**
* @return whether the rail is ready for trains running on it
@ -47,7 +47,7 @@ public abstract class Rail {
* @param position the point to check
* @return whether the point is inside this rail (not on the edge)
*/
public abstract boolean contains(Point position);
public abstract boolean contains(Vector2D position);
public abstract Point getDirectionFrom(Point position);
public abstract Vector2D getDirectionFrom(Vector2D position);
}

View File

@ -2,13 +2,43 @@ package edu.kit.informatik;
import java.util.Objects;
/**
* A switch in the rail network. It connects a start position to two other positions.
*
* @author Arne Keller
* @version 1.0
*/
public final class Switch extends Rail {
public final Point start;
public final Point end1;
public final Point end2;
public Point selection;
/**
* Start position.
*/
private final Vector2D start;
/**
* End position one, has to be in a straight line from start.
*/
private final Vector2D end1;
/**
* End position two, has to be in a straight line from start.
*/
private final Vector2D end2;
/**
* Currently selected position (either one or two).
*/
private Vector2D selection;
public Switch(Point start, Point end1, Point end2, int id) {
/**
* Construct a new switch.
* @param start start position
* @param end1 end position 1
* @param end2 end position 2
* @param id identifier of this switch
* @throws IllegalArgumentException if the switch is not composed of straight lines
*/
public Switch(Vector2D start, Vector2D end1, Vector2D end2, int id) throws IllegalArgumentException {
if (start.getX() != end1.getX() && start.getY() != end1.getY()
|| start.getX() != end2.getX() && start.getY() != end2.getY()) {
throw new IllegalArgumentException("start has to be connected in straight lines to end positions!");
}
this.start = start;
this.end1 = end1;
this.end2 = end2;
@ -16,12 +46,12 @@ public final class Switch extends Rail {
}
@Override
public boolean canConnectTo(Point point) {
public boolean canConnectTo(Vector2D point) {
return point.equals(start) || point.equals(end1) || point.equals(end2);
}
@Override
public boolean connectsTo(Point point) {
public boolean connectsTo(Vector2D point) {
return point.equals(start) || point.equals(selection);
}
@ -31,9 +61,9 @@ public final class Switch extends Rail {
}
@Override
public boolean switchTo(Point position) {
public boolean switchTo(Vector2D position) {
if (position.equals(end1) || position.equals(end2)) {
selection = new Point(position.x, position.y);
selection = new Vector2D(position.getX(), position.getY());
return true;
}
return false;
@ -45,34 +75,34 @@ public final class Switch extends Rail {
}
@Override
public boolean contains(final Point position) {
public boolean contains(final Vector2D position) {
if (selection == null) {
return false;
}
if (start.x == selection.x && position.x == start.x) {
if (start.y < selection.y) {
return start.y < position.y && position.y < selection.y;
if (start.getX() == selection.getX() && position.getX() == start.getX()) {
if (start.getY() < selection.getY()) {
return start.getY() < position.getY() && position.getY() < selection.getY();
} else {
return start.y > position.y && position.y > selection.y;
return start.getY() > position.getY() && position.getY() > selection.getY();
}
} else if (start.y == selection.y && position.y == start.y) {
if (start.x < selection.x) {
return start.x < position.x && position.x < selection.x;
} else if (start.getY() == selection.getY() && position.getY() == start.getY()) {
if (start.getX() < selection.getX()) {
return start.getX() < position.getX() && position.getX() < selection.getX();
} else {
return start.x > position.x && position.x > selection.x;
return start.getX() > position.getX() && position.getX() > selection.getX();
}
}
return false;
}
@Override
public Point getDirectionFrom(Point position) {
public Vector2D getDirectionFrom(Vector2D position) {
if (selection == null) {
return null;
} else if (start.equals(position)) {
return new Point(selection.x - start.x, selection.y - start.y).normalized();
return new Vector2D(selection.getX() - start.getX(), selection.getY() - start.getY()).normalized();
} else if (selection.equals(position)) {
return new Point(start.x - selection.x, start.y - selection.y).normalized();
return new Vector2D(start.getX() - selection.getX(), start.getY() - selection.getY()).normalized();
} else {
return null;
}

View File

@ -3,28 +3,38 @@ package edu.kit.informatik;
import java.util.Objects;
/**
* Straight track.
* A straight track.
*
* @author Arne Keller
* @version 1.0
*/
public final class Track extends Rail {
public final Point start;
public final Point end;
private final Vector2D start;
private final Vector2D end;
public Track(final Point start, final Point end, final int id) {
/**
* Construct a new track. Start and end positions have to be on a straight line!
* @param start start position of the track
* @param end end position of the track
* @param id identifier to use
* @throws IllegalArgumentException if the positions are not on a straight line
*/
public Track(final Vector2D start, final Vector2D end, final int id) throws IllegalArgumentException {
if (start.getX() != end.getX() && start.getY() != end.getY()) {
throw new IllegalArgumentException("start and end have to be in a straight line!");
}
this.start = start;
this.end = end;
super.id = id;
}
@Override
public boolean canConnectTo(Point point) {
public boolean canConnectTo(Vector2D point) {
return point.equals(start) || point.equals(end);
}
@Override
public boolean connectsTo(Point point) {
public boolean connectsTo(Vector2D point) {
return canConnectTo(point);
}
@ -37,7 +47,7 @@ public final class Track extends Rail {
}
@Override
public boolean switchTo(Point position) {
public boolean switchTo(Vector2D position) {
return false;
}
@ -47,27 +57,27 @@ public final class Track extends Rail {
}
@Override
public boolean contains(Point position) {
if (start.x == end.x && position.x == start.x) {
int dy0 = Math.abs(start.y - end.y);
int dy1 = Math.abs(start.y - position.y);
int dy2 = Math.abs(end.y - position.y);
public boolean contains(Vector2D position) {
if (start.getX() == end.getX() && position.getX() == start.getX()) {
int dy0 = Math.abs(start.getY() - end.getY());
int dy1 = Math.abs(start.getY() - position.getY());
int dy2 = Math.abs(end.getY() - position.getY());
return dy1 > 0 && dy2 > 0 && dy1 < dy0 && dy2 < dy0;
} else if (start.y == end.y && position.y == start.y) {
int dx0 = Math.abs(start.x - end.x);
int dx1 = Math.abs(start.x - position.x);
int dx2 = Math.abs(end.x - position.x);
} else if (start.getY() == end.getY() && position.getY() == start.getY()) {
int dx0 = Math.abs(start.getX() - end.getX());
int dx1 = Math.abs(start.getX() - position.getX());
int dx2 = Math.abs(end.getX() - position.getX());
return dx1 > 0 && dx2 > 0 && dx1 < dx0 && dx2 < dx0;
}
return false;
}
@Override
public Point getDirectionFrom(Point position) {
public Vector2D getDirectionFrom(Vector2D position) {
if (start.equals(position)) {
return new Point(end.x - start.x, end.y - start.y).normalized();
return new Vector2D(end.getX() - start.getX(), end.getY() - start.getY()).normalized();
} else if (end.equals(position)) {
return new Point(start.x - end.x, start.y - end.y).normalized();
return new Vector2D(start.getX() - end.getX(), start.getY() - end.getY()).normalized();
} else {
throw new IllegalArgumentException();
}

View File

@ -12,8 +12,8 @@ import java.util.List;
public final class Train {
private final int identifier;
private final List<RollingStock> rollingStocks = new ArrayList<>();
private List<Point> position;
private Point direction;
private List<Vector2D> position;
private Vector2D direction;
/**
* Construct a new train.
@ -93,7 +93,7 @@ public final class Train {
return position != null && direction != null;
}
public void storePositionAndDirection(final List<Point> positions, final Point direction) {
public void storePositionAndDirection(final List<Vector2D> positions, final Vector2D direction) {
this.position = positions;
this.direction = direction;
}
@ -112,7 +112,7 @@ public final class Train {
|| position.stream().filter(rail::connectsTo).count() == 2;
}
public Point getFrontPosition() {
public Vector2D getFrontPosition() {
if (position != null) {
return position.get(0);
} else {
@ -120,7 +120,7 @@ public final class Train {
}
}
public Point getBackPosition() {
public Vector2D getBackPosition() {
if (position != null) {
return position.get(position.size() - 1);
} else {
@ -128,11 +128,11 @@ public final class Train {
}
}
public Point getDirection() {
public Vector2D getDirection() {
return direction;
}
public Point getBackDirection() {
public Vector2D getBackDirection() {
if (position.size() == 1) {
return direction.negated();
} else {
@ -140,17 +140,17 @@ public final class Train {
}
}
public void moveTo(Point nextPosition) {
public void moveTo(Vector2D nextPosition) {
position.add(0, nextPosition);
position.remove(position.size() - 1);
}
public void moveBackTo(Point backPosition) {
public void moveBackTo(Vector2D backPosition) {
position.remove(0);
position.add(backPosition);
}
public void setDirection(Point newDirection) {
public void setDirection(Vector2D newDirection) {
direction = newDirection;
}
@ -163,11 +163,11 @@ public final class Train {
return other.isOnAnyPosition(position);
}
public boolean isOnPosition(Point point) {
public boolean isOnPosition(Vector2D point) {
return position.contains(point);
}
public boolean isOnAnyPosition(List<Point> positions) {
public boolean isOnAnyPosition(List<Vector2D> positions) {
return position.stream().anyMatch(positions::contains);
}

View File

@ -0,0 +1,140 @@
package edu.kit.informatik;
import java.util.Objects;
/**
* A two-dimensional vector, usually a position or movement direction vector.
*
* @author Arne Keller
* @version 1.0
*/
public class Vector2D {
/**
* First coordinate of the vector, usually referred to as 'x'.
*/
private int x;
/**
* Second coordinate of the vector, usally referred to as 'y'.
*/
private int y;
/**
* Construct a new vector.
* @param x first component
* @param y second component
*/
public Vector2D(int x, int y) {
this.x = x;
this.y = y;
}
/**
* @param input two numbers separated by a comma
* @return a vector containing the two numbers, null otherwise
*/
public static Vector2D parse(final String input) {
String[] coordinates = input.split(",", 2);
int x = Integer.parseInt(coordinates[0]);
int y = Integer.parseInt(coordinates[1]);
return new Vector2D(x, y);
}
@Override
public String toString() {
return String.format("(%d,%d)", x, y);
}
/**
* Calculate the distance between this vector (interpreted as position) and another position.
* @param other the point to measure distance to
* @return the manhattan distance
*/
public int distanceTo(final Vector2D other) {
return Math.abs(this.x - other.x) + Math.abs(this.y - other.y);
}
/**
* @return a vector with separately (!) normalized components
*/
public Vector2D normalized() {
if (x >= 0 && x <= 1 && y >= 0 && y <= 1) {
return this;
} else if (x != 0 && y != 0) {
return new Vector2D(x / Math.abs(x), y / Math.abs(y));
} else if (x != 0) {
return new Vector2D(x / Math.abs(x), 0);
} else { // y != 0
return new Vector2D(0, y / Math.abs(y));
}
}
/**
* @return (-1) * this vector
*/
public Vector2D negated() {
return new Vector2D(-x, -y);
}
/**
* Subtract another vector from this one.
* @param other another vector
* @return the difference of this vector and the other vector
*/
public Vector2D subtract(Vector2D other) {
return new Vector2D(x - other.x, y - other.y);
}
/**
* @param movement vector to add
* @return component-wise sum of this vector and the other vector
*/
public Vector2D add(final Vector2D movement) {
return new Vector2D(x + movement.x, y + movement.y);
}
/**
* @return the first component of this vector
*/
public int getX() {
return x;
}
/**
* @return the second component of this vector
*/
public int getY() {
return y;
}
/**
* Change the value of the first component of the vector
* @param x value to change to
*/
public void setX(int x) {
this.x = x;
}
/**
* Change the value of the second component of the vector
* @param y value to change to
*/
public void setY(int y) {
this.y = y;
}
@Override
public boolean equals(final Object obj) {
if (obj != null && getClass().equals(obj.getClass())) {
final Vector2D point = (Vector2D) obj;
return x == point.x && y == point.y;
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}

View File

@ -1,7 +1,7 @@
package edu.kit.informatik.command;
import edu.kit.informatik.ModelRailwaySimulation;
import edu.kit.informatik.Point;
import edu.kit.informatik.Vector2D;
import edu.kit.informatik.Terminal;
/**
@ -11,9 +11,9 @@ import edu.kit.informatik.Terminal;
* @version 1.0
*/
public class AddSwitch implements Command {
private final Point start;
private final Point end1;
private final Point end2;
private final Vector2D start;
private final Vector2D end1;
private final Vector2D end2;
/**
* Construct a new 'add switch' command.
@ -21,13 +21,12 @@ public class AddSwitch implements Command {
* @param end1 end position 1
* @param end2 end position 2
*/
public AddSwitch(final Point start, final Point end1, final Point end2) {
public AddSwitch(final Vector2D start, final Vector2D end1, final Vector2D end2) {
this.start = start;
this.end1 = end1;
this.end2 = end2;
}
@Override
public void apply(ModelRailwaySimulation simulation) {
int id = simulation.addSwitch(start, end1, end2);
if (id == -1) {

View File

@ -1,7 +1,7 @@
package edu.kit.informatik.command;
import edu.kit.informatik.ModelRailwaySimulation;
import edu.kit.informatik.Point;
import edu.kit.informatik.Vector2D;
import edu.kit.informatik.Terminal;
/**
@ -11,20 +11,19 @@ import edu.kit.informatik.Terminal;
* @version 1.0
*/
public class AddTrack implements Command {
private Point start;
private Point end;
private Vector2D start;
private Vector2D end;
/**
* Construct a new 'add track' command.
* @param start position of the start of the track
* @param end position of the end of the track
*/
public AddTrack(final Point start, final Point end) {
public AddTrack(final Vector2D start, final Vector2D end) {
this.start = start;
this.end = end;
}
@Override
public void apply(final ModelRailwaySimulation simulation) {
if (start.equals(end)) {
Terminal.printError("track has length 0");

View File

@ -24,7 +24,6 @@ public class AddTrain implements Command {
this.rollingStockId = rollingStockId;
}
@Override
public void apply(ModelRailwaySimulation simulation) {
if (simulation.addTrain(trainId, rollingStockId)) {
RollingStock rollingStock = simulation.getRollingStock(rollingStockId);

View File

@ -1,9 +1,6 @@
package edu.kit.informatik.command;
import edu.kit.informatik.CoachType;
import edu.kit.informatik.EngineType;
import edu.kit.informatik.Point;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -57,8 +54,9 @@ public class CommandFactory {
* Parse a single line of user input into one command.
* @param command user input line
* @return a fully specified command object
* @throws InvalidInputException if user input is invalid
*/
public static Command getCommand(final String command) {
public static Command getCommand(final String command) throws InvalidInputException {
if (command.startsWith(ADD_TRACK)) {
String arguments = command.substring(ADD_TRACK.length());
Matcher matcher = ADD_TRACK_ARGUMENTS.matcher(arguments);
@ -66,9 +64,9 @@ public class CommandFactory {
Terminal.printError("invalid add track argument syntax");
return null;
}
Point start = Point.parse(matcher.group(1));
Point end = Point.parse(matcher.group(2));
if (start.x == end.x || start.y == end.y) {
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 {
Terminal.printError("invalid track segment: not a straight line");
@ -81,14 +79,13 @@ public class CommandFactory {
Terminal.printError("invalid add switch argument syntax");
return null;
}
Point start = Point.parse(matcher.group(1));
Point end1 = Point.parse(matcher.group(2));
Point end2 = Point.parse(matcher.group(3));
if ((start.x == end1.x || start.y == end1.y) && (start.x == end2.x || start.y == end2.y)) {
Vector2D start = Vector2D.parse(matcher.group(1));
Vector2D end1 = Vector2D.parse(matcher.group(2));
Vector2D end2 = Vector2D.parse(matcher.group(3));
if ((start.getX() == end1.getX() || start.getY() == end1.getY()) && (start.getX() == end2.getX() || start.getY() == end2.getY())) {
return new AddSwitch(start, end1, end2);
} else {
Terminal.printError("invalid switch: not consisting of straight lines");
return null;
throw new InvalidInputException("switch rails have to be straight lines");
}
} else if (command.startsWith(DELETE_TRACK)) {
String argument = command.substring(DELETE_TRACK.length());
@ -112,7 +109,7 @@ public class CommandFactory {
return null;
}
int id = Integer.parseInt(matcher.group(1));
Point point = Point.parse(matcher.group(2));
Vector2D point = Vector2D.parse(matcher.group(2));
return new SetSwitch(id, point);
} else if (command.startsWith(CREATE_ENGINE)) {
String arguments = command.substring(CREATE_ENGINE.length());
@ -228,7 +225,7 @@ public class CommandFactory {
return null;
}
int id = Integer.parseInt(matcher.group(1));
Point point = Point.parse(matcher.group(2));
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) {

View File

@ -31,7 +31,6 @@ public class CreateCoach implements Command {
this.couplingBack = couplingBack;
}
@Override
public void apply(ModelRailwaySimulation simulation) {
int id = simulation.createCoach(type, length, couplingFront, couplingBack);
if (id == -1) {

View File

@ -35,7 +35,6 @@ public class CreateEngine implements Command {
this.couplingBack = couplingBack;
}
@Override
public void apply(final ModelRailwaySimulation simulation) {
final Engine engine;
switch (type) {

View File

@ -34,7 +34,6 @@ public class CreateTrainSet implements Command {
this.couplingBack = couplingBack;
}
@Override
public void apply(final ModelRailwaySimulation simulation) {
TrainSet trainSet = new TrainSet(series, name, length, couplingFront, couplingBack);
if (simulation.createTrainSet(trainSet)) {

View File

@ -20,7 +20,6 @@ public class DeleteRollingStock implements Command {
this.id = id;
}
@Override
public void apply(final ModelRailwaySimulation simulation) {
if (simulation.deleteRollingStock(id)) {
Terminal.printLine("OK");

View File

@ -20,7 +20,6 @@ public class DeleteTrack implements Command {
this.id = id;
}
@Override
public void apply(final ModelRailwaySimulation simulation) {
if (simulation.removeRail(id)) {
Terminal.printLine("OK");

View File

@ -20,7 +20,6 @@ public class DeleteTrain implements Command {
this.id = id;
}
@Override
public void apply(final ModelRailwaySimulation simulation) {
if (simulation.deleteTrain(id)) {
Terminal.printLine("OK");

View File

@ -9,7 +9,6 @@ import edu.kit.informatik.ModelRailwaySimulation;
* @version 1.0
*/
public class ListCoaches implements Command {
@Override
public void apply(final ModelRailwaySimulation simulation) {
simulation.printCoaches();
}

View File

@ -3,13 +3,12 @@ package edu.kit.informatik.command;
import edu.kit.informatik.ModelRailwaySimulation;
/**
* Command used to list engines.
* Command used to print a list of engines.
*
* @author Arne Keller
* @version 1.0
*/
public class ListEngines implements Command {
@Override
public void apply(final ModelRailwaySimulation simulation) {
simulation.printEngines();
}

View File

@ -3,13 +3,12 @@ package edu.kit.informatik.command;
import edu.kit.informatik.ModelRailwaySimulation;
/**
* Command used to list tracks and switches.
* Command used to print a list of tracks and switches.
*
* @author Arne Keller
* @version 1.0
*/
public class ListTracks implements Command {
@Override
public void apply(final ModelRailwaySimulation simulation) {
simulation.printTracks();
}

View File

@ -3,13 +3,12 @@ package edu.kit.informatik.command;
import edu.kit.informatik.ModelRailwaySimulation;
/**
* Command used to list train sets.
* Command used to print a list of train sets on the terminal.
*
* @author Arne Keller
* @version 1.0
*/
public class ListTrainSets implements Command {
@Override
public void apply(final ModelRailwaySimulation simulation) {
simulation.printTrainSets();
}

View File

@ -3,13 +3,12 @@ package edu.kit.informatik.command;
import edu.kit.informatik.ModelRailwaySimulation;
/**
* Command used to list trains.
* Command used to print a list of trains on the terminal.
*
* @author Arne Keller
* @version 1.0
*/
public class ListTrains implements Command {
@Override
public void apply(final ModelRailwaySimulation simulation) {
simulation.printTrains();
}

View File

@ -1,7 +1,7 @@
package edu.kit.informatik.command;
import edu.kit.informatik.ModelRailwaySimulation;
import edu.kit.informatik.Point;
import edu.kit.informatik.Vector2D;
import edu.kit.informatik.Terminal;
/**
@ -12,7 +12,7 @@ import edu.kit.informatik.Terminal;
*/
public class PutTrain implements Command {
private final int id;
private final Point point;
private final Vector2D point;
private final int x;
private final int y;
@ -23,16 +23,15 @@ public class PutTrain implements Command {
* @param x initial x direction
* @param y initial y direction
*/
public PutTrain(final int id, final Point point, final int x, final int y) {
public PutTrain(final int id, final Vector2D point, final int x, final int y) {
this.id = id;
this.point = point;
this.x = x;
this.y = y;
}
@Override
public void apply(final ModelRailwaySimulation simulation) {
if (simulation.putTrain(id, point, new Point(x, y))) {
if (simulation.putTrain(id, point, new Vector2D(x, y))) {
Terminal.printLine("OK");
} else {
Terminal.printError("could not place train");

View File

@ -1,30 +1,29 @@
package edu.kit.informatik.command;
import edu.kit.informatik.ModelRailwaySimulation;
import edu.kit.informatik.Point;
import edu.kit.informatik.Vector2D;
import edu.kit.informatik.Terminal;
/**
* Command used to set the position a switch is set to.
* Command used to specify the position a switch is set to.
*
* @author Arne Keller
* @version 1.0
*/
public class SetSwitch implements Command {
private final int id;
private final Point point;
private final Vector2D point;
/**
* Construct a new 'set switch' command.
* @param id identifier of the switch
* @param point position to set the switch to
*/
public SetSwitch(final int id, final Point point) {
public SetSwitch(final int id, final Vector2D point) {
this.id = id;
this.point = point;
}
@Override
public void apply(final ModelRailwaySimulation simulation) {
if (simulation.setSwitch(id, point)) {
Terminal.printLine("OK");

View File

@ -19,7 +19,6 @@ public class ShowTrain implements Command {
this.id = id;
}
@Override
public void apply(final ModelRailwaySimulation simulation) {
simulation.printTrain(id);
}

View File

@ -3,7 +3,7 @@ package edu.kit.informatik.command;
import edu.kit.informatik.ModelRailwaySimulation;
/**
* Command used to run the simulation.
* Command used to advance the simulation.
*
* @author Arne Keller
* @version 1.0
@ -13,13 +13,12 @@ public class Step implements Command {
/**
* Construct a new 'step' command.
* @param speed the amount of steps to perform
* @param speed the amount of steps to perform, can be any value
*/
public Step(final short speed) {
this.speed = speed;
}
@Override
public void apply(final ModelRailwaySimulation simulation) {
simulation.step(speed);
}