From 0e56a1c73ba9545d88edc3acbd0d57d4fa5a1bc8 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Thu, 5 Mar 2020 10:48:00 +0100 Subject: [PATCH] Checkstyle --- src/edu/kit/informatik/model/Coach.java | 7 +- .../kit/informatik/model/DieselEngine.java | 4 +- .../informatik/model/ElectricalEngine.java | 4 +- src/edu/kit/informatik/model/Engine.java | 4 +- .../model/ModelRailwaySimulation.java | 24 ++-- .../kit/informatik/model/RailwayNetwork.java | 2 +- src/edu/kit/informatik/model/Track.java | 37 ++--- .../kit/informatik/model/TrainManager.java | 128 +++++++++--------- src/edu/kit/informatik/model/Vector2D.java | 16 ++- .../kit/informatik/ui/command/AddSwitch.java | 6 +- .../kit/informatik/ui/command/AddTrack.java | 6 +- .../kit/informatik/ui/command/AddTrain.java | 6 +- .../informatik/ui/command/CommandFactory.java | 8 +- .../informatik/ui/command/CreateCoach.java | 10 +- .../informatik/ui/command/CreateEngine.java | 11 +- .../informatik/ui/command/CreateTrainSet.java | 9 +- .../ui/command/DeleteRollingStock.java | 7 +- .../informatik/ui/command/DeleteTrack.java | 4 +- .../informatik/ui/command/DeleteTrain.java | 4 +- .../informatik/ui/command/ListCoaches.java | 4 +- .../informatik/ui/command/ListEngines.java | 4 +- .../kit/informatik/ui/command/ListTracks.java | 4 +- .../informatik/ui/command/ListTrainSets.java | 4 +- .../kit/informatik/ui/command/ListTrains.java | 4 +- .../kit/informatik/ui/command/PutTrain.java | 4 +- .../kit/informatik/ui/command/SetSwitch.java | 4 +- .../kit/informatik/ui/command/ShowTrain.java | 4 +- src/edu/kit/informatik/ui/command/Step.java | 7 +- 28 files changed, 178 insertions(+), 158 deletions(-) diff --git a/src/edu/kit/informatik/model/Coach.java b/src/edu/kit/informatik/model/Coach.java index dc012d3..c25af05 100644 --- a/src/edu/kit/informatik/model/Coach.java +++ b/src/edu/kit/informatik/model/Coach.java @@ -19,6 +19,10 @@ public abstract class Coach extends RollingStock { * Regex pattern to match coach identifiers. */ public static final Pattern IDENTIFIER_PATTERN = Pattern.compile(IDENTIFIER_PREFIX + "\\+?\\d+"); + /** + * Regex pattern to match coach type (passenger, freight, or special). + */ + public static final String COACH_TYPE = "passenger|freight|special"; /** * The unique identifier of this coach. @@ -33,8 +37,7 @@ public abstract class Coach extends RollingStock { * @param couplingBack whether the coach should have a back coupling * @throws InvalidInputException on invalid user input (zero-sized coach) */ - public Coach(final int identifier, final int length, - final boolean couplingFront, final boolean couplingBack) throws InvalidInputException { + public Coach(int identifier, int length, boolean couplingFront, boolean couplingBack) throws InvalidInputException { super(length, couplingFront, couplingBack); this.identifier = identifier; } diff --git a/src/edu/kit/informatik/model/DieselEngine.java b/src/edu/kit/informatik/model/DieselEngine.java index 91f7789..806fdc5 100644 --- a/src/edu/kit/informatik/model/DieselEngine.java +++ b/src/edu/kit/informatik/model/DieselEngine.java @@ -30,8 +30,8 @@ public class DieselEngine extends Engine { * @param couplingBack whether the engine should have a back coupling * @throws InvalidInputException on invalid user input (e.g. zero-sized engine) */ - public DieselEngine(final String series, final String name, final int length, - final boolean couplingFront, final boolean couplingBack) throws InvalidInputException { + public DieselEngine(String series, String name, int length, + boolean couplingFront, boolean couplingBack) throws InvalidInputException { super(series, name, length, couplingFront, couplingBack); } diff --git a/src/edu/kit/informatik/model/ElectricalEngine.java b/src/edu/kit/informatik/model/ElectricalEngine.java index 56b30b6..52d7da1 100644 --- a/src/edu/kit/informatik/model/ElectricalEngine.java +++ b/src/edu/kit/informatik/model/ElectricalEngine.java @@ -32,8 +32,8 @@ public class ElectricalEngine extends Engine { * @param couplingBack whether the engine should have a back coupling * @throws InvalidInputException on invalid user input (e.g. zero-sized engine) */ - public ElectricalEngine(final String series, final String name, final int length, - final boolean couplingFront, final boolean couplingBack) throws InvalidInputException { + public ElectricalEngine(String series, String name, int length, + boolean couplingFront, boolean couplingBack) throws InvalidInputException { super(series, name, length, couplingFront, couplingBack); } diff --git a/src/edu/kit/informatik/model/Engine.java b/src/edu/kit/informatik/model/Engine.java index 1cd88bc..11aba84 100644 --- a/src/edu/kit/informatik/model/Engine.java +++ b/src/edu/kit/informatik/model/Engine.java @@ -27,8 +27,8 @@ public abstract class Engine extends RollingStock { * @param couplingBack whether this engine should have a back coupling * @throws InvalidInputException on invalid user input (zero-sized coach) */ - protected Engine(final String series, final String name, final int length, - final boolean couplingFront, final boolean couplingBack) throws InvalidInputException { + protected Engine(String series, String name, int length, + boolean couplingFront, boolean couplingBack) throws InvalidInputException { super(length, couplingFront, couplingBack); this.series = series; this.name = name; diff --git a/src/edu/kit/informatik/model/ModelRailwaySimulation.java b/src/edu/kit/informatik/model/ModelRailwaySimulation.java index 7807eae..674a3df 100644 --- a/src/edu/kit/informatik/model/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/model/ModelRailwaySimulation.java @@ -47,7 +47,7 @@ public class ModelRailwaySimulation { * @return the positive identifier of the new track if successful, -1 if none available * @throws InvalidInputException if user input is invalid (e.g. zero length track) */ - public int addTrack(final Vector2D start, final Vector2D end) throws InvalidInputException { + public int addTrack(Vector2D start, Vector2D end) throws InvalidInputException { return railNetwork.addTrack(start, end); } @@ -90,7 +90,7 @@ public class ModelRailwaySimulation { * @return whether the switch could be set */ public boolean setSwitch(int id, Vector2D position) { - boolean success = railNetwork.setSwitch(id, position); + final boolean success = railNetwork.setSwitch(id, position); if (success) { // derail trains on switch, explicitly not (!) printing any removed trains (source: forum post) trainManager.removeTrainsOnRail(railNetwork.getRail(id)); } @@ -102,7 +102,7 @@ public class ModelRailwaySimulation { * @throws InvalidInputException when the identifier is already in use */ public void createEngine(Engine newEngine) throws InvalidInputException { - String id = newEngine.getIdentifier(); + final String id = newEngine.getIdentifier(); if (Stream.concat(engines.stream(), trainSets.stream()) .anyMatch(rollingStock -> rollingStock.getIdentifier().equals(id))) { throw new InvalidInputException("engine identifier already used"); @@ -118,7 +118,7 @@ public class ModelRailwaySimulation { public List listEngines() { engines.sort(Comparator.comparing(Engine::getIdentifier)); return engines.stream().map(engine -> { - String trainId = trainManager.getTrainContainingRollingStock(engine) + final String trainId = trainManager.getTrainContainingRollingStock(engine) .map(train -> Integer.toString(train.getIdentifier())) .orElse("none"); return String.format("%s %s", trainId, engine); @@ -136,7 +136,7 @@ public class ModelRailwaySimulation { */ public int createCoach(CoachType coachType, int length, boolean couplingFront, boolean couplingBack) throws InvalidInputException { - int id = getNextCoachIdentifier(); + final int id = getNextCoachIdentifier(); if (id < 0) { return -1; } @@ -172,8 +172,8 @@ public class ModelRailwaySimulation { */ public List listCoaches() { return coaches.keySet().stream().sorted().map(coachId -> { - Coach coach = coaches.get(coachId); - String trainId = trainManager.getTrainContainingRollingStock(coach) + final Coach coach = coaches.get(coachId); + final String trainId = trainManager.getTrainContainingRollingStock(coach) .map(train -> Integer.toString(train.getIdentifier())) .orElse("none"); return String.format("%d %s %s %d %b %b", @@ -188,7 +188,7 @@ public class ModelRailwaySimulation { * @throws InvalidInputException if the identifier is already used */ public void createTrainSet(TrainSet newTrainSet) throws InvalidInputException { - String id = newTrainSet.getIdentifier(); + final String id = newTrainSet.getIdentifier(); if (Stream.concat(engines.stream(), trainSets.stream()) .anyMatch(rollingStock -> rollingStock.getIdentifier().equals(id))) { throw new InvalidInputException("train set identifier already used"); @@ -217,7 +217,7 @@ public class ModelRailwaySimulation { * @return whether the rolling was successfully removed */ public boolean deleteRollingStock(String id) { - RollingStock rollingStock = getRollingStock(id); + final RollingStock rollingStock = getRollingStock(id); if (trainManager.getTrainContainingRollingStock(rollingStock).isPresent()) { return false; // can not delete rolling stock in use } @@ -231,7 +231,7 @@ public class ModelRailwaySimulation { */ public RollingStock getRollingStock(String id) { if (Coach.IDENTIFIER_PATTERN.matcher(id).matches()) { - int coachId = Integer.parseInt(id.substring(1)); + final int coachId = Integer.parseInt(id.substring(1)); return coaches.get(coachId); } else { return Stream.concat(engines.stream(), trainSets.stream()) @@ -247,7 +247,7 @@ public class ModelRailwaySimulation { * @throws InvalidInputException if input is incorrect */ public void addTrain(int trainId, String rollingStockId) throws InvalidInputException { - RollingStock rollingStock = getRollingStock(rollingStockId); + final RollingStock rollingStock = getRollingStock(rollingStockId); if (rollingStock == null) { throw new InvalidInputException("rolling stock not found"); } @@ -300,7 +300,7 @@ public class ModelRailwaySimulation { * @param speed amount of steps to move the trains * @throws InvalidInputException if the simulation is not yet ready */ - public void step(final short speed) throws InvalidInputException { + public void step(short speed) throws InvalidInputException { trainManager.step(speed); } } \ No newline at end of file diff --git a/src/edu/kit/informatik/model/RailwayNetwork.java b/src/edu/kit/informatik/model/RailwayNetwork.java index 017a684..ad2ccbf 100644 --- a/src/edu/kit/informatik/model/RailwayNetwork.java +++ b/src/edu/kit/informatik/model/RailwayNetwork.java @@ -166,7 +166,7 @@ public class RailwayNetwork { * @param position position to set the switch to * @return whether the switch could be set */ - public boolean setSwitch(final int id, final Vector2D position) { + public boolean setSwitch(int id, Vector2D position) { final Rail toSwitch = rails.get(id); if (toSwitch != null) { return toSwitch.switchTo(position); diff --git a/src/edu/kit/informatik/model/Track.java b/src/edu/kit/informatik/model/Track.java index ac6c0d9..0773f53 100644 --- a/src/edu/kit/informatik/model/Track.java +++ b/src/edu/kit/informatik/model/Track.java @@ -22,12 +22,13 @@ public final class Track extends Rail { /** * 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 InvalidInputException if the positions are not on a straight line */ - public Track(final Vector2D start, final Vector2D end, final int id) throws InvalidInputException { + public Track(Vector2D start, Vector2D end, int id) throws InvalidInputException { super(id); if (start.getX() != end.getX() && start.getY() != end.getY()) { throw new InvalidInputException("invalid track segment: not a straight line"); @@ -62,15 +63,11 @@ public final class Track extends Rail { return false; } if (start.getX() == end.getX() && position.getX() == start.getX()) { - long startY = start.getY(); - long endY = end.getY(); - long positionY = position.getY(); - return startY < positionY && positionY < endY || startY > positionY && positionY > endY; + return start.getY() < position.getY() && position.getY() < end.getY() + || start.getY() > position.getY() && position.getY() > end.getY(); } else if (start.getY() == end.getY() && position.getY() == start.getY()) { - long startX = start.getX(); - long endX = end.getX(); - long positionX = position.getX(); - return startX < positionX && positionX < endX || startX > positionX && positionX > endX; + return start.getX() < position.getX() && position.getX() < end.getX() + || start.getX() > position.getX() && position.getX() > end.getX(); } return false; } @@ -82,17 +79,17 @@ public final class Track extends Rail { @Override public Vector2D move(Vector2D position, Vector2D direction, long steps) { - Vector2D nextPosition = position.add(direction.scale(steps)); + final Vector2D nextPosition = position.add(direction.scale(steps)); if (contains(nextPosition) || connectsTo(nextPosition)) { return nextPosition; } else if (direction.equals(getDirectionFrom(start))) { return new Vector2D(end); } else if (direction.equals(getDirectionFrom(end))) { return new Vector2D(start); - } else if (position.equals(end)) { + } else if (position.equals(end) && !direction.equals(getDirectionFrom(start))) { direction.copyFrom(getDirectionFrom(end)); return move(position, getDirectionFrom(end), steps); - } else if (position.equals(start)) { + } else if (position.equals(start) && !direction.equals(getDirectionFrom(end))) { direction.copyFrom(getDirectionFrom(start)); return move(position, getDirectionFrom(start), steps); } @@ -101,13 +98,10 @@ public final class Track extends Rail { @Override public Vector2D getDirectionFrom(Vector2D position) { - // have to use long arithmetic here to avoid overflows if (start.equals(position)) { - return new Vector2D(Long.signum(end.getX() - start.getX()), - Long.signum(end.getY() - start.getY())); + return new Vector2D(Long.signum(end.getX() - start.getX()), Long.signum(end.getY() - start.getY())); } else if (end.equals(position)) { - return new Vector2D(Long.signum(start.getX() - end.getX()), - Long.signum(start.getY() - end.getY())); + return new Vector2D(Long.signum(start.getX() - end.getX()), Long.signum(start.getY() - end.getY())); } else { // in the middle of track, simply return direction from start return getDirectionFrom(start); @@ -116,11 +110,8 @@ public final class Track extends Rail { @Override public boolean allowsDirectionFrom(Vector2D position, Vector2D direction) { - if (contains(position)) { - return true; - } else if (connectsTo(position)) { - return getDirectionFrom(position).equals(direction) - || getDirectionFrom(position).negated().equals(direction); + if (contains(position) || connectsTo(position)) { + return getDirectionFrom(position).isParallelTo(direction); } return false; } @@ -144,7 +135,7 @@ public final class Track extends Rail { } @Override - public boolean equals(final Object obj) { + public boolean equals(Object obj) { if (obj != null && getClass().equals(obj.getClass())) { final Track track = (Track) obj; diff --git a/src/edu/kit/informatik/model/TrainManager.java b/src/edu/kit/informatik/model/TrainManager.java index 73a458e..87daa47 100644 --- a/src/edu/kit/informatik/model/TrainManager.java +++ b/src/edu/kit/informatik/model/TrainManager.java @@ -36,7 +36,7 @@ public final class TrainManager { /** * Construct a new train manager that will operate on the provided rail network. - * + * * @param railNetwork rail network to use */ public TrainManager(RailwayNetwork railNetwork) { @@ -46,7 +46,7 @@ public final class TrainManager { /** * Check whether a train is on the rail with the specified identifier. Note that a train must be partially on that * rail, simply touching one of the end points is not enough. - * + * * @param rail the rail to check * @return whether a train is on that rail */ @@ -56,6 +56,7 @@ public final class TrainManager { /** * Remove any trains on the rail with the specified identifier. + * * @param rail rail to clear */ public void removeTrainsOnRail(Rail rail) { @@ -64,7 +65,7 @@ public final class TrainManager { /** * Get the train containing the specified rolling stock. - * + * * @param rollingStock rolling stock to search for * @return the train containing the rolling stock */ @@ -74,7 +75,7 @@ public final class TrainManager { /** * Add a rolling stock to an existing train or create a new one. - * + * * @param trainId train identifier * @param rollingStock rolling stock to add * @throws InvalidInputException on invalid user input (e.g. rolling stock in use) @@ -83,29 +84,30 @@ public final class TrainManager { if (getTrainContainingRollingStock(rollingStock).isPresent()) { throw new InvalidInputException("rolling stock already used"); } - Train train = trains.get(trainId); + final Train train = trains.get(trainId); if (train != null && train.isPlaced()) { throw new InvalidInputException("can not add rolling stock to placed train"); } if (train != null) { train.add(rollingStock); } else { - int correctId = getNextTrainIdentifier(); + final int correctId = getNextTrainIdentifier(); if (trainId != correctId) { throw new InvalidInputException("new train identifier must be next free identifier"); } - Train newTrain = new Train(trainId, rollingStock); + final Train newTrain = new Train(trainId, rollingStock); trains.put(trainId, newTrain); } } /** - * Delete a train. + * Delete a train. Will not delete rolling stock contained in that train. + * * @param trainId identifier of the train * @return whether the train could be deleted */ public boolean deleteTrain(int trainId) { - Train train = trains.get(trainId); + final Train train = trains.get(trainId); if (train != null) { trains.remove(trainId); return true; @@ -133,7 +135,7 @@ public final class TrainManager { * @throws InvalidInputException if train not found */ public List showTrain(int trainId) throws InvalidInputException { - Train train = trains.get(trainId); + final Train train = trains.get(trainId); if (train != null) { return train.show(); } else { @@ -150,7 +152,7 @@ public final class TrainManager { * @throws InvalidInputException when the train is too long */ public boolean putTrain(int trainId, Vector2D position, Vector2D direction) throws InvalidInputException { - Train train = trains.get(trainId); + final Train train = trains.get(trainId); if (train == null) { throw new InvalidInputException("train not found"); } else if (!train.isProperTrain()) { @@ -163,7 +165,7 @@ public final class TrainManager { throw new InvalidInputException("switches not set up"); } // attempt to place train - boolean placed = train.placeOn(railNetwork, position, direction); + final boolean placed = train.placeOn(railNetwork, position, direction); // check for collisions if (placed && !getPlacementCollisions().isEmpty()) { train.removeFromRails(); @@ -187,28 +189,21 @@ public final class TrainManager { * @param collisions list of collisions (never null, sometimes empty) */ private void getStaticCollisions(List> collisions) { - int maxId = trains.keySet().stream().max(Integer::compareTo).orElse(0); - for (int id1 = 1; id1 <= maxId; id1++) { - Train train1 = trains.get(id1); + final int maxId = trains.keySet().stream().max(Integer::compareTo).orElse(0); + for (int id = 1; id <= maxId; id++) { + final Train train1 = trains.get(id); if (train1 == null || !train1.isPlaced()) { continue; } - HashSet collision = new HashSet<>(); - // check for same position - IntStream.rangeClosed(id1 + 1, maxId) + final Set occupiedRails = train1.getOccupiedRails(); + // check for same position, and rail collisions + final Set collision = IntStream.rangeClosed(id + 1, maxId) .mapToObj(trains::get) .filter(Objects::nonNull) .filter(Train::isPlaced) - .filter(train1::touches) - .forEach(collision::add); - // check for rail collisions - Set occupiedRails = train1.getOccupiedRails(); - IntStream.rangeClosed(id1 + 1, maxId) - .mapToObj(trains::get) - .filter(Objects::nonNull) - .filter(Train::isPlaced) - .filter(train -> train.getOccupiedRails().stream().anyMatch(occupiedRails::contains)) - .forEach(collision::add); + .filter(train -> train1.touches(train) + || train.getOccupiedRails().stream().anyMatch(occupiedRails::contains)) + .collect(Collectors.toSet()); if (!collision.isEmpty()) { collision.add(train1); addToSetOrAddNew(collisions, collision); @@ -218,25 +213,27 @@ public final class TrainManager { /** * Implementation of the silly *second* collision checking algorithm. + * Will put two trains in a collision even if they only touch the same rail. + * * @return list of collisions */ private List> getPlacementCollisions() { - List> collisions = new ArrayList<>(); - trains.values().stream().filter(Train::isPlaced).forEach(train1 -> { - trains.values().stream().filter(train -> train != train1).filter(Train::isPlaced).forEach(train2 -> { - Set occupiedByTrain1 = train1.getOccupiedRails(); - Collections.addAll(occupiedByTrain1, railNetwork.findTouchingRails(train1.getFrontPosition())); - Collections.addAll(occupiedByTrain1, railNetwork.findTouchingRails(train1.getRearPosition())); - Set occupiedByTrain2 = train2.getOccupiedRails(); - Collections.addAll(occupiedByTrain2, railNetwork.findTouchingRails(train2.getFrontPosition())); - Collections.addAll(occupiedByTrain2, railNetwork.findTouchingRails(train2.getRearPosition())); - occupiedByTrain2.retainAll(occupiedByTrain1); - if (!occupiedByTrain2.isEmpty()) { - Set collision = Stream.of(train1, train2).collect(Collectors.toSet()); - addToSetOrAddNew(collisions, collision); - } - }); - }); + final List> collisions = new ArrayList<>(); + trains.values().stream().filter(Train::isPlaced).forEach(train1 -> + trains.values().stream().filter(train -> train != train1).filter(Train::isPlaced).forEach(train2 -> { + final Set occupiedByTrain1 = train1.getOccupiedRails(); + Collections.addAll(occupiedByTrain1, railNetwork.findTouchingRails(train1.getFrontPosition())); + Collections.addAll(occupiedByTrain1, railNetwork.findTouchingRails(train1.getRearPosition())); + final Set occupiedByTrain2 = train2.getOccupiedRails(); + Collections.addAll(occupiedByTrain2, railNetwork.findTouchingRails(train2.getFrontPosition())); + Collections.addAll(occupiedByTrain2, railNetwork.findTouchingRails(train2.getRearPosition())); + // calculate intersection + occupiedByTrain2.retainAll(occupiedByTrain1); + if (!occupiedByTrain2.isEmpty()) { + final Set collision = Stream.of(train1, train2).collect(Collectors.toSet()); + addToSetOrAddNew(collisions, collision); + } + })); return collisions; } @@ -266,19 +263,22 @@ public final class TrainManager { } /** - * Get collisions of moving the trains one step forward. + * Get collisions of moving the trains one step forward, removing crashing trains from the rails in the process. + * * @return list of collisions (never null, sometimes empty) */ private List> getCollisionsOfOneStep() { - List> collisions = new ArrayList<>(); + final List> collisions = new ArrayList<>(); trains.values().stream().filter(Train::isPlaced).forEach(train -> { - Vector2D position = train.getFrontPosition(); - Vector2D direction = train.getDirection(); - Vector2D nextPosition = railNetwork.move(position, direction, 1); + final Vector2D position = train.getFrontPosition(); + final Vector2D direction = train.getDirection(); + final Vector2D nextPosition = railNetwork.move(position, direction, 1); if (nextPosition == null || nextPosition.equals(position)) { + // train is derailing train.moveTo(railNetwork, null); collisions.add(new HashSet<>(Arrays.asList(train))); } else { + // train is moving successfully train.moveTo(railNetwork, nextPosition); } }); @@ -288,21 +288,23 @@ public final class TrainManager { } /** - * Get collisions of moving the trains one step backward. + * Get collisions of moving the trains one step backward, removing crashing trains from the rails in the process. + * * @return list of collisions (never null, sometimes empty) */ private List> getCollisionsOfOneReverseStep() { - List> collisions = new ArrayList<>(); + final List> collisions = new ArrayList<>(); // perform step trains.values().stream().filter(Train::isPlaced).forEach(train -> { - Vector2D position = train.getRearPosition(); - Vector2D direction = train.getRearDirection(); - Vector2D nextPosition = railNetwork.move(position, direction, 1); - if (nextPosition == null - || train.isOnPosition(nextPosition) && !train.getRearPosition().equals(train.getFrontPosition())) { + final Vector2D position = train.getRearPosition(); + final Vector2D direction = train.getRearDirection(); + final Vector2D nextPosition = railNetwork.move(position, direction, 1); + if (nextPosition == null || nextPosition.equals(position)) { + // derailing train.moveBackTo(railNetwork, nextPosition); collisions.add(new HashSet<>(Arrays.asList(train))); } else { + // train moving successfully train.moveBackTo(railNetwork, nextPosition); } }); @@ -325,19 +327,21 @@ public final class TrainManager { return; } - List> collisions = IntStream.range(0, Math.abs(speed)) + final List> collisions = IntStream.range(0, Math.abs(speed)) .mapToObj(step -> speed >= 0 ? getCollisionsOfOneStep() : getCollisionsOfOneReverseStep()) .flatMap(List::stream).collect(Collectors.toList()); - for (int id : trains.keySet().stream().sorted().collect(Collectors.toList())) { - Train train = trains.get(id); - Set collisionSet = collisions.stream() + for (final int id : trains.keySet().stream().sorted().collect(Collectors.toList())) { + final Train train = trains.get(id); + final Set collisionSet = collisions.stream() .filter(collision -> collision.contains(train)) .findFirst().orElse(null); if (collisionSet != null) { // print collision - int first = collisionSet.stream().min(Comparator.comparing(Train::getIdentifier)).get().getIdentifier(); + final int first = collisionSet.stream() + .min(Comparator.comparing(Train::getIdentifier)).get().getIdentifier(); if (train.getIdentifier() == first) { // only print each collision once - List collision = collisionSet.stream().sorted(Comparator.comparing(Train::getIdentifier)) + final List collision = collisionSet.stream() + .sorted(Comparator.comparing(Train::getIdentifier)) .collect(Collectors.toList()); Terminal.printLine("Crash of train " + String.join(",", collision.stream() .map(crashedTrain -> Integer.toString(crashedTrain.getIdentifier())) diff --git a/src/edu/kit/informatik/model/Vector2D.java b/src/edu/kit/informatik/model/Vector2D.java index 5a3e2c3..63b9950 100644 --- a/src/edu/kit/informatik/model/Vector2D.java +++ b/src/edu/kit/informatik/model/Vector2D.java @@ -41,7 +41,7 @@ public class Vector2D { * @param input two 32-bit numbers separated by a comma * @return a vector containing the two numbers, null otherwise */ - public static Vector2D parse(final String input) { + public static Vector2D parse(String input) { final String[] coordinates = input.split(",", 2); final int x = Integer.parseInt(coordinates[0]); final int y = Integer.parseInt(coordinates[1]); @@ -62,6 +62,20 @@ public class Vector2D { return other != null ? Math.abs(this.x - other.x) + Math.abs(this.y - other.y) : 0; } + /** + * Check whether this vector is parallel to another vector. Note that this method only works for vectors that + * are parallel to one of the coordinate axes. + * + * @param other the point to measure distance to + * @return the manhattan distance + */ + public boolean isParallelTo(Vector2D other) { + if (other == null) { + return false; + } + return x == 0 && other.x == 0 || y == 0 && other.y == 0; + } + /** * @return a vector with separately (!) normalized components */ diff --git a/src/edu/kit/informatik/ui/command/AddSwitch.java b/src/edu/kit/informatik/ui/command/AddSwitch.java index e601023..b286477 100644 --- a/src/edu/kit/informatik/ui/command/AddSwitch.java +++ b/src/edu/kit/informatik/ui/command/AddSwitch.java @@ -35,11 +35,11 @@ public class AddSwitch extends Command { private Vector2D end2; @Override - public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { if (start == null) { throw new IllegalStateException("command not initialized"); } - int id = simulation.addSwitch(start, end1, end2); + final int id = simulation.addSwitch(start, end1, end2); if (id == -1) { Terminal.printError("switch not connected to existing rails"); } else { @@ -52,7 +52,7 @@ public class AddSwitch extends Command { if (input == null || !input.startsWith(ADD_SWITCH)) { throw new InvalidInputException("unknown command"); } - Matcher matcher = ADD_SWITCH_ARGUMENTS.matcher(input.substring(ADD_SWITCH.length())); + final Matcher matcher = ADD_SWITCH_ARGUMENTS.matcher(input.substring(ADD_SWITCH.length())); if (!matcher.matches()) { throw new InvalidInputException("invalid add switch argument syntax"); } diff --git a/src/edu/kit/informatik/ui/command/AddTrack.java b/src/edu/kit/informatik/ui/command/AddTrack.java index 8ea0a88..59d9e85 100644 --- a/src/edu/kit/informatik/ui/command/AddTrack.java +++ b/src/edu/kit/informatik/ui/command/AddTrack.java @@ -31,11 +31,11 @@ public class AddTrack extends Command { private Vector2D end; @Override - public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { if (start == null) { throw new IllegalStateException("command not initialized"); } - int id = simulation.addTrack(start, end); + final int id = simulation.addTrack(start, end); if (id == -1) { Terminal.printError("id space exhausted"); } else { @@ -48,7 +48,7 @@ public class AddTrack extends Command { if (input == null || !input.startsWith(ADD_TRACK)) { throw new InvalidInputException("unknown command"); } - Matcher matcher = ADD_TRACK_ARGUMENTS.matcher(input.substring(ADD_TRACK.length())); + final Matcher matcher = ADD_TRACK_ARGUMENTS.matcher(input.substring(ADD_TRACK.length())); if (!matcher.matches()) { throw new InvalidInputException("invalid add track argument syntax"); } diff --git a/src/edu/kit/informatik/ui/command/AddTrain.java b/src/edu/kit/informatik/ui/command/AddTrain.java index 74f3759..a54bc8b 100644 --- a/src/edu/kit/informatik/ui/command/AddTrain.java +++ b/src/edu/kit/informatik/ui/command/AddTrain.java @@ -32,12 +32,12 @@ public class AddTrain extends Command { private String rollingStockId; @Override - public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { if (rollingStockId == null) { throw new IllegalStateException("command not initialized"); } simulation.addTrain(trainId, rollingStockId); - RollingStock rollingStock = simulation.getRollingStock(rollingStockId); + final RollingStock rollingStock = simulation.getRollingStock(rollingStockId); Terminal.printLine(String.format("%s %s added to train %d", rollingStock.description(), rollingStock.getIdentifier(), trainId)); } @@ -47,7 +47,7 @@ public class AddTrain extends Command { if (input == null || !input.startsWith(ADD_TRAIN)) { throw new InvalidInputException("unknown command"); } - Matcher matcher = ADD_TRAIN_ARGUMENTS.matcher(input.substring(ADD_TRAIN.length())); + final Matcher matcher = ADD_TRAIN_ARGUMENTS.matcher(input.substring(ADD_TRAIN.length())); if (!matcher.matches()) { throw new InvalidInputException("invalid add train arguments"); } diff --git a/src/edu/kit/informatik/ui/command/CommandFactory.java b/src/edu/kit/informatik/ui/command/CommandFactory.java index 5101194..dccf4df 100644 --- a/src/edu/kit/informatik/ui/command/CommandFactory.java +++ b/src/edu/kit/informatik/ui/command/CommandFactory.java @@ -31,6 +31,10 @@ public final class CommandFactory { */ public static final String ROLLING_STOCK_IDENTIFIER = "(" + ALPHANUMERIC_WORD + "-" + ALPHANUMERIC_WORD + ")|" + Coach.IDENTIFIER_PATTERN; + /** + * Regex to accept one boolean (true or false). + */ + public static final String BOOL = "true|false"; /** * Name of the add track command. @@ -143,9 +147,9 @@ public final class CommandFactory { * @throws InvalidInputException if user input is invalid */ public static Command getCommand(String input) throws InvalidInputException { - for (Map.Entry> entry : COMMANDS.entrySet()) { + for (final Map.Entry> entry : COMMANDS.entrySet()) { if (input.startsWith(entry.getKey())) { - Command command = entry.getValue().get(); + final Command command = entry.getValue().get(); command.parse(input); return command; } diff --git a/src/edu/kit/informatik/ui/command/CreateCoach.java b/src/edu/kit/informatik/ui/command/CreateCoach.java index 6e7ea29..4f45645 100644 --- a/src/edu/kit/informatik/ui/command/CreateCoach.java +++ b/src/edu/kit/informatik/ui/command/CreateCoach.java @@ -8,6 +8,8 @@ import edu.kit.informatik.ui.InvalidInputException; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static edu.kit.informatik.model.Coach.COACH_TYPE; +import static edu.kit.informatik.ui.command.CommandFactory.BOOL; import static edu.kit.informatik.ui.command.CommandFactory.CREATE_COACH; import static edu.kit.informatik.ui.command.CommandFactory.NUMBER; @@ -19,7 +21,7 @@ import static edu.kit.informatik.ui.command.CommandFactory.NUMBER; */ public class CreateCoach extends Command { private static final Pattern CREATE_COACH_ARGUMENTS - = Pattern.compile(" (passenger|freight|special) (" + NUMBER + ") (true|false) (true|false)"); + = Pattern.compile(" (" + COACH_TYPE + ") (" + NUMBER + ") (" + BOOL + ") (" + BOOL + ")"); /** * Type of the new coach. @@ -39,11 +41,11 @@ public class CreateCoach extends Command { private boolean couplingBack; @Override - public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { if (type == null) { throw new IllegalStateException("command not initialized"); } - int id = simulation.createCoach(type, length, couplingFront, couplingBack); + final int id = simulation.createCoach(type, length, couplingFront, couplingBack); if (id == -1) { Terminal.printError("id space exhausted"); } else { @@ -56,7 +58,7 @@ public class CreateCoach extends Command { if (input == null || !input.startsWith(CREATE_COACH)) { throw new InvalidInputException("unknown command"); } - Matcher matcher = CREATE_COACH_ARGUMENTS.matcher(input.substring(CREATE_COACH.length())); + final Matcher matcher = CREATE_COACH_ARGUMENTS.matcher(input.substring(CREATE_COACH.length())); if (!matcher.matches()) { throw new InvalidInputException("invalid create coach arguments"); } diff --git a/src/edu/kit/informatik/ui/command/CreateEngine.java b/src/edu/kit/informatik/ui/command/CreateEngine.java index b71947c..4351151 100644 --- a/src/edu/kit/informatik/ui/command/CreateEngine.java +++ b/src/edu/kit/informatik/ui/command/CreateEngine.java @@ -14,6 +14,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import static edu.kit.informatik.ui.command.CommandFactory.ALPHANUMERIC_WORD; +import static edu.kit.informatik.ui.command.CommandFactory.BOOL; import static edu.kit.informatik.ui.command.CommandFactory.CREATE_ENGINE; import static edu.kit.informatik.ui.command.CommandFactory.NUMBER; @@ -26,7 +27,7 @@ import static edu.kit.informatik.ui.command.CommandFactory.NUMBER; public class CreateEngine extends Command { private static final Pattern CREATE_ENGINE_ARGUMENTS = Pattern.compile(" (electrical|diesel|steam) (" + ALPHANUMERIC_WORD + ") (" + ALPHANUMERIC_WORD + ") (" - + NUMBER + ") (true|false) (true|false)"); + + NUMBER + ") (" + BOOL + ") (" + BOOL + ")"); /** * Type of the new engine. @@ -54,11 +55,11 @@ public class CreateEngine extends Command { private boolean couplingBack; @Override - public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { if (type == null) { throw new IllegalStateException("command not initialized"); } - Engine engine; + final Engine engine; switch (type) { case ELECTRICAL: engine = new ElectricalEngine(series, name, length, couplingFront, couplingBack); @@ -70,7 +71,7 @@ public class CreateEngine extends Command { engine = new DieselEngine(series, name, length, couplingFront, couplingBack); break; default: - throw new IllegalStateException("engine type is null!"); + throw new IllegalStateException("command not initialized"); } simulation.createEngine(engine); Terminal.printLine(engine.getIdentifier()); @@ -81,7 +82,7 @@ public class CreateEngine extends Command { if (input == null || !input.startsWith(CREATE_ENGINE)) { throw new InvalidInputException("unknown command"); } - Matcher matcher = CREATE_ENGINE_ARGUMENTS.matcher(input.substring(CREATE_ENGINE.length())); + final Matcher matcher = CREATE_ENGINE_ARGUMENTS.matcher(input.substring(CREATE_ENGINE.length())); if (!matcher.matches()) { throw new InvalidInputException("invalid create engine argument syntax"); } diff --git a/src/edu/kit/informatik/ui/command/CreateTrainSet.java b/src/edu/kit/informatik/ui/command/CreateTrainSet.java index 430b0dc..57cb818 100644 --- a/src/edu/kit/informatik/ui/command/CreateTrainSet.java +++ b/src/edu/kit/informatik/ui/command/CreateTrainSet.java @@ -10,6 +10,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import static edu.kit.informatik.ui.command.CommandFactory.ALPHANUMERIC_WORD; +import static edu.kit.informatik.ui.command.CommandFactory.BOOL; import static edu.kit.informatik.ui.command.CommandFactory.CREATE_TRAIN_SET; import static edu.kit.informatik.ui.command.CommandFactory.NUMBER; @@ -22,7 +23,7 @@ import static edu.kit.informatik.ui.command.CommandFactory.NUMBER; public class CreateTrainSet extends Command { private static final Pattern CREATE_TRAIN_SET_ARGUMENTS = Pattern.compile(" (" + ALPHANUMERIC_WORD + ") (" + ALPHANUMERIC_WORD + ") (" - + NUMBER + ") (true|false) (true|false)"); + + NUMBER + ") (" + BOOL + ") (" + BOOL + ")"); /** * Series (class) of the new train set. @@ -46,11 +47,11 @@ public class CreateTrainSet extends Command { private boolean couplingBack; @Override - public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { if (name == null) { throw new IllegalStateException("command not initialized"); } - TrainSet trainSet = new TrainSet(series, name, length, couplingFront, couplingBack); + final TrainSet trainSet = new TrainSet(series, name, length, couplingFront, couplingBack); simulation.createTrainSet(trainSet); Terminal.printLine(trainSet.getIdentifier()); } @@ -60,7 +61,7 @@ public class CreateTrainSet extends Command { if (input == null || !input.startsWith(CREATE_TRAIN_SET)) { throw new InvalidInputException("unknown command"); } - Matcher matcher = CREATE_TRAIN_SET_ARGUMENTS.matcher(input.substring(CREATE_TRAIN_SET.length())); + final Matcher matcher = CREATE_TRAIN_SET_ARGUMENTS.matcher(input.substring(CREATE_TRAIN_SET.length())); if (!matcher.matches()) { throw new InvalidInputException("invalid create train-set arguments"); } diff --git a/src/edu/kit/informatik/ui/command/DeleteRollingStock.java b/src/edu/kit/informatik/ui/command/DeleteRollingStock.java index c3259ef..76d3402 100644 --- a/src/edu/kit/informatik/ui/command/DeleteRollingStock.java +++ b/src/edu/kit/informatik/ui/command/DeleteRollingStock.java @@ -17,8 +17,7 @@ import static edu.kit.informatik.ui.command.CommandFactory.ROLLING_STOCK_IDENTIF * @version 1.0 */ public class DeleteRollingStock extends Command { - private static final Pattern DELETE_ROLLING_STOCK_ARGUMENT - = Pattern.compile(" (" + ROLLING_STOCK_IDENTIFIER + ")"); + private static final Pattern DELETE_ROLLING_STOCK_ARGUMENT = Pattern.compile(" (" + ROLLING_STOCK_IDENTIFIER + ")"); /** * Identifier of the rolling stock to delete. @@ -26,7 +25,7 @@ public class DeleteRollingStock extends Command { private String id; @Override - public void apply(final ModelRailwaySimulation simulation) { + public void apply(ModelRailwaySimulation simulation) { if (id == null) { throw new IllegalStateException("command not initialized"); } @@ -42,7 +41,7 @@ public class DeleteRollingStock extends Command { if (input == null || !input.startsWith(DELETE_ROLLING_STOCK)) { throw new InvalidInputException("unknown command"); } - Matcher matcher = DELETE_ROLLING_STOCK_ARGUMENT.matcher(input.substring(DELETE_ROLLING_STOCK.length())); + final Matcher matcher = DELETE_ROLLING_STOCK_ARGUMENT.matcher(input.substring(DELETE_ROLLING_STOCK.length())); if (!matcher.matches()) { throw new InvalidInputException("invalid delete rolling stock argument"); } diff --git a/src/edu/kit/informatik/ui/command/DeleteTrack.java b/src/edu/kit/informatik/ui/command/DeleteTrack.java index bac78fd..17aa117 100644 --- a/src/edu/kit/informatik/ui/command/DeleteTrack.java +++ b/src/edu/kit/informatik/ui/command/DeleteTrack.java @@ -20,7 +20,7 @@ public class DeleteTrack extends Command { private int id; @Override - public void apply(final ModelRailwaySimulation simulation) { + public void apply(ModelRailwaySimulation simulation) { if (simulation.removeRail(id)) { Terminal.printLine("OK"); } else { @@ -33,7 +33,7 @@ public class DeleteTrack extends Command { if (input == null || !input.startsWith(DELETE_TRACK)) { throw new InvalidInputException("unknown command"); } - String argument = input.substring(DELETE_TRACK.length()); + final String argument = input.substring(DELETE_TRACK.length()); if (!argument.matches(" " + NUMBER)) { throw new InvalidInputException("invalid/missing delete track argument"); } diff --git a/src/edu/kit/informatik/ui/command/DeleteTrain.java b/src/edu/kit/informatik/ui/command/DeleteTrain.java index 1d64d48..b6f0339 100644 --- a/src/edu/kit/informatik/ui/command/DeleteTrain.java +++ b/src/edu/kit/informatik/ui/command/DeleteTrain.java @@ -20,7 +20,7 @@ public class DeleteTrain extends Command { private int id; @Override - public void apply(final ModelRailwaySimulation simulation) { + public void apply(ModelRailwaySimulation simulation) { if (simulation.deleteTrain(id)) { Terminal.printLine("OK"); } else { @@ -33,7 +33,7 @@ public class DeleteTrain extends Command { if (input == null || !input.startsWith(DELETE_TRAIN)) { throw new InvalidInputException("unknown command"); } - String argument = input.substring(DELETE_TRAIN.length()); + final String argument = input.substring(DELETE_TRAIN.length()); if (!argument.matches(" " + NUMBER)) { throw new InvalidInputException("invalid delete train argument"); } diff --git a/src/edu/kit/informatik/ui/command/ListCoaches.java b/src/edu/kit/informatik/ui/command/ListCoaches.java index d5e74cd..ea81d70 100644 --- a/src/edu/kit/informatik/ui/command/ListCoaches.java +++ b/src/edu/kit/informatik/ui/command/ListCoaches.java @@ -16,8 +16,8 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_COACHES; */ public class ListCoaches extends Command { @Override - public void apply(final ModelRailwaySimulation simulation) { - List coaches = simulation.listCoaches(); + public void apply(ModelRailwaySimulation simulation) { + final List coaches = simulation.listCoaches(); if (coaches.isEmpty()) { Terminal.printLine("No coach exists"); } else { diff --git a/src/edu/kit/informatik/ui/command/ListEngines.java b/src/edu/kit/informatik/ui/command/ListEngines.java index d4e5380..6ed34a8 100644 --- a/src/edu/kit/informatik/ui/command/ListEngines.java +++ b/src/edu/kit/informatik/ui/command/ListEngines.java @@ -16,8 +16,8 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_ENGINES; */ public class ListEngines extends Command { @Override - public void apply(final ModelRailwaySimulation simulation) { - List engines = simulation.listEngines(); + public void apply(ModelRailwaySimulation simulation) { + final List engines = simulation.listEngines(); if (engines.isEmpty()) { Terminal.printLine("No engine exists"); } else { diff --git a/src/edu/kit/informatik/ui/command/ListTracks.java b/src/edu/kit/informatik/ui/command/ListTracks.java index e68fd71..b9abfb6 100644 --- a/src/edu/kit/informatik/ui/command/ListTracks.java +++ b/src/edu/kit/informatik/ui/command/ListTracks.java @@ -16,8 +16,8 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_TRACKS; */ public class ListTracks extends Command { @Override - public void apply(final ModelRailwaySimulation simulation) { - List tracks = simulation.listTracks(); + public void apply(ModelRailwaySimulation simulation) { + final List tracks = simulation.listTracks(); if (tracks.isEmpty()) { Terminal.printLine("No track exists"); } else { diff --git a/src/edu/kit/informatik/ui/command/ListTrainSets.java b/src/edu/kit/informatik/ui/command/ListTrainSets.java index 4b6852e..ceaeaa9 100644 --- a/src/edu/kit/informatik/ui/command/ListTrainSets.java +++ b/src/edu/kit/informatik/ui/command/ListTrainSets.java @@ -16,8 +16,8 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_TRAIN_SETS; */ public class ListTrainSets extends Command { @Override - public void apply(final ModelRailwaySimulation simulation) { - List trainSets = simulation.listTrainSets(); + public void apply(ModelRailwaySimulation simulation) { + final List trainSets = simulation.listTrainSets(); if (trainSets.isEmpty()) { Terminal.printLine("No train-set exists"); } else { diff --git a/src/edu/kit/informatik/ui/command/ListTrains.java b/src/edu/kit/informatik/ui/command/ListTrains.java index 085ea36..4dbb856 100644 --- a/src/edu/kit/informatik/ui/command/ListTrains.java +++ b/src/edu/kit/informatik/ui/command/ListTrains.java @@ -16,8 +16,8 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_TRAINS; */ public class ListTrains extends Command { @Override - public void apply(final ModelRailwaySimulation simulation) { - List trains = simulation.listTrains(); + public void apply(ModelRailwaySimulation simulation) { + final List trains = simulation.listTrains(); if (trains.isEmpty()) { Terminal.printLine("No train exists"); } else { diff --git a/src/edu/kit/informatik/ui/command/PutTrain.java b/src/edu/kit/informatik/ui/command/PutTrain.java index 21ea7e9..3e0a73e 100644 --- a/src/edu/kit/informatik/ui/command/PutTrain.java +++ b/src/edu/kit/informatik/ui/command/PutTrain.java @@ -36,7 +36,7 @@ public class PutTrain extends Command { private Vector2D direction; @Override - public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { if (point == null) { throw new IllegalStateException("command not initialized"); } @@ -52,7 +52,7 @@ public class PutTrain extends Command { if (input == null || !input.startsWith(PUT_TRAIN)) { throw new InvalidInputException("unknown command"); } - Matcher matcher = PUT_TRAIN_ARGUMENTS.matcher(input.substring(PUT_TRAIN.length())); + final Matcher matcher = PUT_TRAIN_ARGUMENTS.matcher(input.substring(PUT_TRAIN.length())); if (!matcher.matches()) { throw new InvalidInputException("invalid put train arguments"); } diff --git a/src/edu/kit/informatik/ui/command/SetSwitch.java b/src/edu/kit/informatik/ui/command/SetSwitch.java index dc6eb82..c0b335b 100644 --- a/src/edu/kit/informatik/ui/command/SetSwitch.java +++ b/src/edu/kit/informatik/ui/command/SetSwitch.java @@ -32,7 +32,7 @@ public class SetSwitch extends Command { private Vector2D point; @Override - public void apply(final ModelRailwaySimulation simulation) { + public void apply(ModelRailwaySimulation simulation) { if (point == null) { throw new IllegalStateException("command not initialized"); } @@ -48,7 +48,7 @@ public class SetSwitch extends Command { if (input == null || !input.startsWith(SET_SWITCH)) { throw new InvalidInputException("unknown command"); } - Matcher matcher = SET_SWITCH_ARGUMENTS.matcher(input.substring(SET_SWITCH.length())); + final Matcher matcher = SET_SWITCH_ARGUMENTS.matcher(input.substring(SET_SWITCH.length())); if (!matcher.matches()) { throw new InvalidInputException("invalid set switch argument syntax"); } diff --git a/src/edu/kit/informatik/ui/command/ShowTrain.java b/src/edu/kit/informatik/ui/command/ShowTrain.java index 798e73a..c367aa4 100644 --- a/src/edu/kit/informatik/ui/command/ShowTrain.java +++ b/src/edu/kit/informatik/ui/command/ShowTrain.java @@ -20,7 +20,7 @@ public class ShowTrain extends Command { private int id; @Override - public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { simulation.showTrain(id).forEach(Terminal::printLine); } @@ -29,7 +29,7 @@ public class ShowTrain extends Command { if (input == null || !input.startsWith(SHOW_TRAIN)) { throw new InvalidInputException("unknown command"); } - String argument = input.substring(SHOW_TRAIN.length()); + final String argument = input.substring(SHOW_TRAIN.length()); if (!argument.matches(" " + NUMBER)) { throw new InvalidInputException("invalid show train argument"); } diff --git a/src/edu/kit/informatik/ui/command/Step.java b/src/edu/kit/informatik/ui/command/Step.java index 635f1c5..126ea5f 100644 --- a/src/edu/kit/informatik/ui/command/Step.java +++ b/src/edu/kit/informatik/ui/command/Step.java @@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.ui.InvalidInputException; +import static edu.kit.informatik.ui.command.CommandFactory.NUMBER; import static edu.kit.informatik.ui.command.CommandFactory.STEP; /** @@ -18,7 +19,7 @@ public class Step extends Command { private short speed; @Override - public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { simulation.step(speed); } @@ -27,8 +28,8 @@ public class Step extends Command { if (input == null || !input.startsWith(STEP)) { throw new InvalidInputException("unknown command"); } - String argument = input.substring(STEP.length()); - if (!argument.matches(" [+-]?\\d+")) { + final String argument = input.substring(STEP.length()); + if (!argument.matches(" " + NUMBER)) { throw new InvalidInputException("invalid step argument"); } speed = Short.parseShort(argument.substring(1));