From cdc71c825311d02382bb29593ce09464fb836f51 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Mon, 17 Feb 2020 10:39:26 +0100 Subject: [PATCH] Checkstyle --- src/edu/kit/informatik/model/Coach.java | 11 ++ .../kit/informatik/model/DieselEngine.java | 3 + .../informatik/model/ElectricalEngine.java | 3 + .../model/ModelRailwaySimulation.java | 117 ++++++++++++++---- src/edu/kit/informatik/ui/CommandLine.java | 3 + 5 files changed, 111 insertions(+), 26 deletions(-) diff --git a/src/edu/kit/informatik/model/Coach.java b/src/edu/kit/informatik/model/Coach.java index ea67269..d4ded99 100644 --- a/src/edu/kit/informatik/model/Coach.java +++ b/src/edu/kit/informatik/model/Coach.java @@ -9,6 +9,9 @@ import edu.kit.informatik.ui.CoachType; * @version 1.0 */ public class Coach extends RollingStock { + /** + * ASCII art representation of a passenger coach. + */ private static final String[] PASSENGER_TEXT = new String[] { "____________________", "| ___ ___ ___ ___ |", @@ -17,6 +20,10 @@ public class Coach extends RollingStock { "|__________________|", " (O) (O) " }; + + /** + * ASCII art representation of a freight coach. + */ private static final String[] FREIGHT_TEXT = new String[] { "| |", "| |", @@ -24,6 +31,10 @@ public class Coach extends RollingStock { "|__________________|", " (O) (O) " }; + + /** + * ASCII art represantation of a special coach. + */ private static final String[] SPECIAL_TEXT = new String[] { " ____", "/--------------| |", diff --git a/src/edu/kit/informatik/model/DieselEngine.java b/src/edu/kit/informatik/model/DieselEngine.java index ee4b452..8b2386e 100644 --- a/src/edu/kit/informatik/model/DieselEngine.java +++ b/src/edu/kit/informatik/model/DieselEngine.java @@ -7,6 +7,9 @@ package edu.kit.informatik.model; * @version 1.0 */ public class DieselEngine extends Engine { + /** + * ASCII art representation of a diesel engine. + */ private static final String[] DIESEL_ENGINE_TEXT = new String[] { " _____________|____ ", " /_| ____________ |_\\ ", diff --git a/src/edu/kit/informatik/model/ElectricalEngine.java b/src/edu/kit/informatik/model/ElectricalEngine.java index d24585c..3d60416 100644 --- a/src/edu/kit/informatik/model/ElectricalEngine.java +++ b/src/edu/kit/informatik/model/ElectricalEngine.java @@ -7,6 +7,9 @@ package edu.kit.informatik.model; * @version 1.0 */ public class ElectricalEngine extends Engine { + /** + * ASCII art of an electrical engine. + */ private static final String[] ELECTRICAL_ENGINE_TEXT = new String[] { " ___ ", " \\ ", diff --git a/src/edu/kit/informatik/model/ModelRailwaySimulation.java b/src/edu/kit/informatik/model/ModelRailwaySimulation.java index 2e461ea..a113f8a 100644 --- a/src/edu/kit/informatik/model/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/model/ModelRailwaySimulation.java @@ -13,11 +13,32 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; +/** + * Model railway simulation. Can simulate trains on a railway network. + * + * @author Arne Keller + * @version 1.0 + */ public class ModelRailwaySimulation { + /** + * Railway network used in this simulation. + */ private final RailwayNetwork railNetwork = new RailwayNetwork(); + /** + * List of engines used in the simulation. + */ private final List engines = new ArrayList<>(); + /** + * List of train sets used in the simulation. + */ private final List trainSets = new ArrayList<>(); + /** + * List of coaches used in the simulation. + */ private final List coaches = new ArrayList<>(); + /** + * List of trains simulated. + */ private final List trains = new ArrayList<>(); /** @@ -93,6 +114,9 @@ public class ModelRailwaySimulation { return true; } + /** + * Print a list of engines added to the simulation. + */ public void printEngines() { if (engines.isEmpty()) { Terminal.printLine("No engine exists"); @@ -130,6 +154,10 @@ public class ModelRailwaySimulation { return id; } + /** + * Calculate the next coach identifier. + * @return the next coach identifier, or -1 if none available + */ private int getNextCoachIdentifier() { int id = 1; for (Coach coach : coaches) { @@ -140,6 +168,9 @@ public class ModelRailwaySimulation { return id; } + /** + * Print a list of coaches added to the simulation. + */ public void printCoaches() { if (coaches.isEmpty()) { Terminal.printLine("No coach exists"); @@ -180,6 +211,9 @@ public class ModelRailwaySimulation { return true; } + /** + * Print a list of train sets added to the simulation. + */ public void printTrainSets() { if (trainSets.isEmpty()) { Terminal.printLine("No train-set exists"); @@ -202,23 +236,24 @@ public class ModelRailwaySimulation { * @param id identifier of the rolling stock to remove * @return whether the rolling was successfully removed */ - public boolean deleteRollingStock(String id) { + public boolean deleteRollingStock(final String id) { RollingStock rollingStock = getRollingStock(id); - if (rollingStock == null) { + if (rollingStock == null || trains.stream().anyMatch(train -> train.contains(rollingStock))) { + // can not delete rolling stock in use return false; } - for (Train train : trains) { - if (train.contains(rollingStock)) { - return false; - } - } engines.remove(rollingStock); trainSets.remove(rollingStock); coaches.remove(rollingStock); return true; } - public RollingStock getRollingStock(String id) { + /** + * Find a rolling stock by identifier. + * @param id identifier of the rolling stock to find + * @return the specified rolling stock, or null if not found + */ + private RollingStock getRollingStock(final String id) { if (id.matches("W\\+?\\d+")) { int coachId = Integer.parseInt(id.substring(1)); for (Coach coach : coaches) { @@ -241,6 +276,12 @@ public class ModelRailwaySimulation { return null; } + /** + * 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 whether the operation was successful + */ public boolean addTrain(int trainId, String rollingStockId) { RollingStock rollingStock = getRollingStock(rollingStockId); if (rollingStock == null) { @@ -267,6 +308,10 @@ public class ModelRailwaySimulation { return true; } + /** + * Calculate the next train identifier. + * @return the next train identfier, or -1 if none available + */ private int getNextTrainIdentifier() { int id = 1; for (Train train : trains) { @@ -277,6 +322,11 @@ public class ModelRailwaySimulation { return id; } + /** + * Delete a train. + * @param id identifier of the train + * @return whether the train could be deleted + */ public boolean deleteTrain(int id) { Train train = getTrain(id); if (train != null) { @@ -286,23 +336,24 @@ public class ModelRailwaySimulation { return false; } + /** + * Find a train by identifier. + * @param id identifier of the train to find. + * @return the specified train, or null if not found + */ private Train getTrain(int id) { - for (Train train : trains) { - if (train.getIdentifier() == id) { - return train; - } - } - return null; + return trains.stream().filter((train) -> train.getIdentifier() == id).findFirst().orElse(null); } + /** + * Print a list of trains in the simulation. + */ public void printTrains() { if (trains.isEmpty()) { Terminal.printLine("No train exists"); // TODO not in fucking spec return; } - for (Train train : trains) { - Terminal.printLine(train); - } + trains.forEach(Terminal::printLine); } /** @@ -337,7 +388,7 @@ public class ModelRailwaySimulation { if (placed) { // now, check for collisions - List> collisions = getStaticCollisions(false); + List> collisions = getStaticCollisions(); if (!collisions.isEmpty()) { // remove if colliding train.removeFromRails(); @@ -350,7 +401,11 @@ public class ModelRailwaySimulation { } } - private ArrayList> getStaticCollisions(boolean removeTrains) { + /** + * Get collisions of trains currently placed. + * @return list of collisions (never null, sometimes empty) + */ + private ArrayList> getStaticCollisions() { if (trains.isEmpty()) { return new ArrayList<>(); } @@ -385,16 +440,13 @@ public class ModelRailwaySimulation { collisions.add(collision); } } - if (removeTrains) { - for (HashSet collision : collisions) { - for (Train t : collision) { - t.removeFromRails(); - } - } - } return collisions; } + /** + * Get collisions of moving the trains one step forward. + * @return list of collisions (never null, sometimes empty) + */ private ArrayList> getCollisionsOfOneStep() { ArrayList> collisions = new ArrayList<>(); Map> occupiedRails = new HashMap<>(); @@ -461,6 +513,10 @@ public class ModelRailwaySimulation { return collisions; } + /** + * Get collisions of moving the trains one step backward. + * @return list of collisions (never null, sometimes empty) + */ private ArrayList> getCollisionsOfOneReverseStep() { ArrayList> collisions = new ArrayList<>(); Map> occupiedRails = new HashMap<>(); @@ -524,6 +580,10 @@ public class ModelRailwaySimulation { return collisions; } + /** + * Move the trains in this simulation, printing their new positions and any collisions. + * @param speed amount of steps to move the trains + */ public void step(final short speed) { if (!railNetwork.isReadyForTrains()) { Terminal.printError("rail tracks/switches not set up"); @@ -535,15 +595,19 @@ public class ModelRailwaySimulation { } List> collisions = new ArrayList<>(); + // first, handle positive speed (forward) for (int i = 0; i < speed; i++) { List> newCollisions = getCollisionsOfOneStep(); collisions.addAll(newCollisions); } + // then, handle negative speed (backward) for (int i = 0; i > speed; i--) { List> newCollisions = getCollisionsOfOneReverseStep(); collisions.addAll(newCollisions); } + // (only one of the loops above actually runs) train: for (Train train : trains) { + // print collision for (Set collisionSet : collisions) { if (!collisionSet.contains(train)) { continue; @@ -557,6 +621,7 @@ public class ModelRailwaySimulation { } continue train; } + // or position, if not in collision if (train.isPlaced()) { Terminal.printLine(String.format("Train %d at %s", train.getIdentifier(), train.getFrontPosition())); } diff --git a/src/edu/kit/informatik/ui/CommandLine.java b/src/edu/kit/informatik/ui/CommandLine.java index 3e8f816..1f191c8 100644 --- a/src/edu/kit/informatik/ui/CommandLine.java +++ b/src/edu/kit/informatik/ui/CommandLine.java @@ -12,6 +12,9 @@ import edu.kit.informatik.ui.command.CommandFactory; * @version 1.0 */ public class CommandLine { + /** + * Command used to exit the simulation and terminate the program. + */ private static final String EXIT = "exit"; /**