diff --git a/src/edu/kit/informatik/model/ModelRailwaySimulation.java b/src/edu/kit/informatik/model/ModelRailwaySimulation.java index 46be686..736fc78 100644 --- a/src/edu/kit/informatik/model/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/model/ModelRailwaySimulation.java @@ -71,7 +71,7 @@ public class ModelRailwaySimulation { */ public boolean removeRail(int id) { // check whether any trains are on this rail - return railNetwork.getRail(id).isPresent() && !trainManager.anyTrainOnRail(railNetwork.getRail(id).get()) + return !railNetwork.getRail(id).map(trainManager::anyTrainOnRail).orElse(true) && railNetwork.removeRail(id); } @@ -250,16 +250,19 @@ public class ModelRailwaySimulation { /** * Create a new train or add rolling stock to an existing train. + * * @param trainId identifier of the train * @param rollingStockId identifier of the rolling stock + * @return the added rolling stock * @throws InvalidInputException if input is incorrect */ - public void addTrain(int trainId, String rollingStockId) throws InvalidInputException { + public RollingStock addTrain(int trainId, String rollingStockId) throws InvalidInputException { final Optional rollingStock = getRollingStock(rollingStockId); if (!rollingStock.isPresent()) { throw new InvalidInputException("rolling stock not found"); } trainManager.addTrain(trainId, rollingStock.get()); + return rollingStock.get(); } /** diff --git a/src/edu/kit/informatik/model/Train.java b/src/edu/kit/informatik/model/Train.java index 11242a5..e668320 100644 --- a/src/edu/kit/informatik/model/Train.java +++ b/src/edu/kit/informatik/model/Train.java @@ -153,9 +153,9 @@ public final class Train { length -= distanceToLastPosition; final Rail occupiedRail = railNetwork.findRail(positionList.getLast(), rollingStockPosition).get(); - if (occupiedRailsSet.contains(occupiedRail) && positionList.size() >= 4) { + if (occupiedRailsSet.contains(occupiedRail) && positionList.size() > 4) { // perhaps a self intersection - final long occupiedAtFrontOfTrain = positionList.get(0).distanceTo(positionList.get(1)); + final long occupiedAtFrontOfTrain = positionList.getFirst().distanceTo(positionList.get(1)); final long occupiedAtBackOfTrain = positionList.getLast() .distanceTo(positionList.get(positionList.size() - 2)); if (occupiedAtFrontOfTrain + occupiedAtBackOfTrain > occupiedRail.getLength()) { @@ -269,6 +269,7 @@ public final class Train { * @param backPosition position to move back of the train to */ public void moveBackTo(RailwayNetwork railNetwork, Vector2D backPosition) { + // TODO: create common method for both directions final Optional railUnderFrontOfTrain = railNetwork.findContainingRail(positions.getFirst()); positions.getFirst().subtractInPlace(getDirection()); if (positions.getFirst().equals(positions.get(1))) { diff --git a/src/edu/kit/informatik/ui/CommandLine.java b/src/edu/kit/informatik/ui/CommandLine.java index 1720d66..e832d4f 100644 --- a/src/edu/kit/informatik/ui/CommandLine.java +++ b/src/edu/kit/informatik/ui/CommandLine.java @@ -32,10 +32,10 @@ public final class CommandLine { while (true) { final String input = Terminal.readLine(); if (input == null) { - break; // TODO remove + break; // FIXME remove } if (input.startsWith("#")) { - continue; // TODO remove + continue; // FIXME remove } if (input.startsWith(EXIT)) { @@ -43,7 +43,7 @@ public final class CommandLine { Terminal.printError("input after exit command"); continue; } else { - break; + return; } } diff --git a/src/edu/kit/informatik/ui/command/AddTrain.java b/src/edu/kit/informatik/ui/command/AddTrain.java index b598183..1ad6606 100644 --- a/src/edu/kit/informatik/ui/command/AddTrain.java +++ b/src/edu/kit/informatik/ui/command/AddTrain.java @@ -36,8 +36,7 @@ public class AddTrain extends Command { if (rollingStockId == null) { throw new IllegalStateException("command not initialized"); } - simulation.addTrain(trainId, rollingStockId); - final RollingStock rollingStock = simulation.getRollingStock(rollingStockId).get(); + final RollingStock rollingStock = simulation.addTrain(trainId, rollingStockId); Terminal.printLine(String.format("%s %s added to train %d", rollingStock.description(), rollingStock.getIdentifier(), trainId)); }