diff --git a/src/edu/kit/informatik/model/ModelRailwaySimulation.java b/src/edu/kit/informatik/model/ModelRailwaySimulation.java index 4c67cc7..e5380c2 100644 --- a/src/edu/kit/informatik/model/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/model/ModelRailwaySimulation.java @@ -1,7 +1,6 @@ package edu.kit.informatik.model; import edu.kit.informatik.ui.CoachType; -import edu.kit.informatik.Terminal; import edu.kit.informatik.ui.InvalidInputException; import java.util.ArrayList; @@ -75,11 +74,13 @@ public class ModelRailwaySimulation { } /** - * Print a list of all rails in the network, specifying their identifier, start and end points, their length and + * Get a list of all rails in the network, specifying their identifier, start and end points, their length and * their current configuration. + * + * @return list of rails */ - public void printTracks() { - railNetwork.printTracks(); + public List listTracks() { + return railNetwork.listTracks(); } /** @@ -110,20 +111,18 @@ public class ModelRailwaySimulation { } /** - * Print a list of engines added to the simulation. + * Get a list of engines (their text representation) added to the simulation. + * + * @return list of engines (never null, sometimes empty) */ - public void printEngines() { - if (engines.isEmpty()) { - Terminal.printLine("No engine exists"); - } else { - engines.sort(Comparator.comparing(Engine::getIdentifier)); - for (Engine engine : engines) { - String trainId = trainManager.getTrainContainingRollingStock(engine) - .map(train -> Integer.toString(train.getIdentifier())) - .orElse("none"); - Terminal.printLine(String.format("%s %s", trainId, engine)); - } - } + public List listEngines() { + engines.sort(Comparator.comparing(Engine::getIdentifier)); + return engines.stream().map(engine -> { + String trainId = trainManager.getTrainContainingRollingStock(engine) + .map(train -> Integer.toString(train.getIdentifier())) + .orElse("none"); + return String.format("%s %s", trainId, engine); + }).collect(Collectors.toList()); } /** @@ -167,22 +166,20 @@ public class ModelRailwaySimulation { } /** - * Print a list of coaches added to the simulation. + * Get a list of coaches (their text representation) added to the simulation. + * + * @return list of coaches (never null, sometimes empty) */ - public void printCoaches() { - if (coaches.isEmpty()) { - Terminal.printLine("No coach exists"); - } else { - for (Integer identifier : coaches.keySet().stream().sorted().collect(Collectors.toList())) { - Coach coach = coaches.get(identifier); - String trainId = trainManager.getTrainContainingRollingStock(coach) - .map(train -> Integer.toString(train.getIdentifier())) - .orElse("none"); - Terminal.printLine(String.format("%d %s %s %d %b %b", - coach.getNumericalIdentifier(), trainId, coach.getType(), - coach.getLength(), coach.hasCouplingFront(), coach.hasCouplingBack())); - } - } + public List listCoaches() { + return coaches.keySet().stream().sorted().map(coachId -> { + Coach coach = coaches.get(coachId); + String trainId = trainManager.getTrainContainingRollingStock(coach) + .map(train -> Integer.toString(train.getIdentifier())) + .orElse("none"); + return String.format("%d %s %s %d %b %b", + coach.getNumericalIdentifier(), trainId, coach.getType(), + coach.getLength(), coach.hasCouplingFront(), coach.hasCouplingBack()); + }).collect(Collectors.toList()); } /** @@ -200,20 +197,18 @@ public class ModelRailwaySimulation { } /** - * Print a list of train sets added to the simulation. + * Get a sorted list of train sets (their text representation) added to the simulation. + * + * @return list of train sets (never null, sometimes empty) */ - public void printTrainSets() { - if (trainSets.isEmpty()) { - Terminal.printLine("No train-set exists"); - } else { - trainSets.sort(Comparator.comparing(TrainSet::getIdentifier)); - for (TrainSet trainSet : trainSets) { - String trainId = trainManager.getTrainContainingRollingStock(trainSet) - .map(train -> Integer.toString(train.getIdentifier())) - .orElse("none"); - Terminal.printLine(String.format("%s %s", trainId, trainSet)); - } - } + public List listTrainSets() { + trainSets.sort(Comparator.comparing(TrainSet::getIdentifier)); + return trainSets.stream().map(trainSet -> { + String trainId = trainManager.getTrainContainingRollingStock(trainSet) + .map(train -> Integer.toString(train.getIdentifier())) + .orElse("none"); + return String.format("%s %s", trainId, trainSet); + }).collect(Collectors.toList()); } /** @@ -269,18 +264,22 @@ public class ModelRailwaySimulation { } /** - * Print a list of trains in the simulation. + * Get a list of trains (their text representation) in the simulation. + * + * @return list of trains */ - public void printTrains() { - trainManager.printTrains(); + public List listTrains() { + return trainManager.listTrains(); } /** - * Display a train as ASCII art. + * Get the ASCII art representation of a train. * @param id identifier of the train to show + * @return rows of ASCII art representing this train + * @throws InvalidInputException if train not found */ - public void printTrain(int id) { - trainManager.printTrain(id); + public List showTrain(int id) throws InvalidInputException { + return trainManager.showTrain(id); } /** @@ -299,8 +298,9 @@ public class ModelRailwaySimulation { /** * Move the trains in this simulation, printing their new positions and any collisions. * @param speed amount of steps to move the trains + * @throws InvalidInputException if the simulation is not yet ready */ - public void step(final short speed) { + public void step(final 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 ab05e37..9e20a7a 100644 --- a/src/edu/kit/informatik/model/RailwayNetwork.java +++ b/src/edu/kit/informatik/model/RailwayNetwork.java @@ -1,11 +1,11 @@ package edu.kit.informatik.model; -import edu.kit.informatik.Terminal; import edu.kit.informatik.ui.InvalidInputException; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.Queue; import java.util.Set; @@ -164,17 +164,13 @@ public class RailwayNetwork { } /** - * Print a list of all rails in the network, specifying their identifier, start and end points, their length and + * Get a sorted list of all rails in the network, specifying their identifier, start and end points, their length and * their current configuration. + * + * @return list of rails */ - public void printTracks() { - if (rails.isEmpty()) { - Terminal.printLine("No track exists"); - } else { - for (int id : rails.keySet().stream().sorted().collect(Collectors.toList())) { - Terminal.printLine(rails.get(id)); - } - } + public List listTracks() { + return rails.keySet().stream().sorted().map(rails::get).map(Object::toString).collect(Collectors.toList()); } /** diff --git a/src/edu/kit/informatik/model/Train.java b/src/edu/kit/informatik/model/Train.java index 68e74e0..4f62ed2 100644 --- a/src/edu/kit/informatik/model/Train.java +++ b/src/edu/kit/informatik/model/Train.java @@ -1,6 +1,5 @@ package edu.kit.informatik.model; -import edu.kit.informatik.Terminal; import edu.kit.informatik.ui.InvalidInputException; import java.util.ArrayList; @@ -92,9 +91,11 @@ public final class Train { } /** - * Print this train as ASCII art. + * Get the ASCII art of this train. + * + * @return rows of ASCII art */ - public void print() { + public List show() { List lines = new ArrayList<>(); for (RollingStock rollingStock : rollingStocks) { for (StringBuilder line : lines) { @@ -119,7 +120,7 @@ public final class Train { } } } - lines.forEach(Terminal::printLine); + return lines.stream().map(StringBuilder::toString).collect(Collectors.toList()); } /** diff --git a/src/edu/kit/informatik/model/TrainManager.java b/src/edu/kit/informatik/model/TrainManager.java index 82441a5..3a54745 100644 --- a/src/edu/kit/informatik/model/TrainManager.java +++ b/src/edu/kit/informatik/model/TrainManager.java @@ -114,26 +114,30 @@ public final class TrainManager { } /** - * Print a list of trains in the simulation. + * Get a sorted list of trains (their text representation) in the simulation. + * + * @return list of trains */ - public void printTrains() { - if (trains.isEmpty()) { - Terminal.printLine("No train exists"); - return; - } - trains.values().forEach(Terminal::printLine); + public List listTrains() { + return trains.keySet().stream() + .sorted() + .map(trains::get) + .map(Object::toString) + .collect(Collectors.toList()); } /** - * Display a train as ASCII art. + * Get the ASCII art representation of a train. * @param trainId identifier of the train to show + * @return ASCII art representation of said train + * @throws InvalidInputException if train not found */ - public void printTrain(int trainId) { + public List showTrain(int trainId) throws InvalidInputException { Train train = trains.get(trainId); if (train != null) { - train.print(); + return train.show(); } else { - Terminal.printError("no such train"); + throw new InvalidInputException("no such train"); } } @@ -338,11 +342,11 @@ public final class TrainManager { /** * Move the trains in this simulation, printing their new positions and any collisions. * @param speed amount of steps to move the trains + * @throws InvalidInputException if simulation is not yet ready */ - public void step(short speed) { + public void step(short speed) throws InvalidInputException { if (!railNetwork.isReadyForTrains()) { - Terminal.printError("rail tracks/switches not set up"); - return; + throw new InvalidInputException("rail tracks/switches not set up"); } if (trains.values().stream().noneMatch(Train::isPlaced)) { Terminal.printLine("OK"); diff --git a/src/edu/kit/informatik/ui/command/ListCoaches.java b/src/edu/kit/informatik/ui/command/ListCoaches.java index 7f81747..d5e74cd 100644 --- a/src/edu/kit/informatik/ui/command/ListCoaches.java +++ b/src/edu/kit/informatik/ui/command/ListCoaches.java @@ -1,8 +1,11 @@ package edu.kit.informatik.ui.command; +import edu.kit.informatik.Terminal; import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.ui.InvalidInputException; +import java.util.List; + import static edu.kit.informatik.ui.command.CommandFactory.LIST_COACHES; /** @@ -14,7 +17,12 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_COACHES; public class ListCoaches extends Command { @Override public void apply(final ModelRailwaySimulation simulation) { - simulation.printCoaches(); + List coaches = simulation.listCoaches(); + if (coaches.isEmpty()) { + Terminal.printLine("No coach exists"); + } else { + coaches.forEach(Terminal::printLine); + } } @Override diff --git a/src/edu/kit/informatik/ui/command/ListEngines.java b/src/edu/kit/informatik/ui/command/ListEngines.java index 80276bb..d4e5380 100644 --- a/src/edu/kit/informatik/ui/command/ListEngines.java +++ b/src/edu/kit/informatik/ui/command/ListEngines.java @@ -1,8 +1,11 @@ package edu.kit.informatik.ui.command; +import edu.kit.informatik.Terminal; import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.ui.InvalidInputException; +import java.util.List; + import static edu.kit.informatik.ui.command.CommandFactory.LIST_ENGINES; /** @@ -14,7 +17,12 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_ENGINES; public class ListEngines extends Command { @Override public void apply(final ModelRailwaySimulation simulation) { - simulation.printEngines(); + List engines = simulation.listEngines(); + if (engines.isEmpty()) { + Terminal.printLine("No engine exists"); + } else { + engines.forEach(Terminal::printLine); + } } @Override diff --git a/src/edu/kit/informatik/ui/command/ListTracks.java b/src/edu/kit/informatik/ui/command/ListTracks.java index a2fe4dd..e68fd71 100644 --- a/src/edu/kit/informatik/ui/command/ListTracks.java +++ b/src/edu/kit/informatik/ui/command/ListTracks.java @@ -1,8 +1,11 @@ package edu.kit.informatik.ui.command; +import edu.kit.informatik.Terminal; import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.ui.InvalidInputException; +import java.util.List; + import static edu.kit.informatik.ui.command.CommandFactory.LIST_TRACKS; /** @@ -14,7 +17,12 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_TRACKS; public class ListTracks extends Command { @Override public void apply(final ModelRailwaySimulation simulation) { - simulation.printTracks(); + List tracks = simulation.listTracks(); + if (tracks.isEmpty()) { + Terminal.printLine("No track exists"); + } else { + tracks.forEach(Terminal::printLine); + } } @Override diff --git a/src/edu/kit/informatik/ui/command/ListTrainSets.java b/src/edu/kit/informatik/ui/command/ListTrainSets.java index 3689341..4b6852e 100644 --- a/src/edu/kit/informatik/ui/command/ListTrainSets.java +++ b/src/edu/kit/informatik/ui/command/ListTrainSets.java @@ -1,8 +1,11 @@ package edu.kit.informatik.ui.command; +import edu.kit.informatik.Terminal; import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.ui.InvalidInputException; +import java.util.List; + import static edu.kit.informatik.ui.command.CommandFactory.LIST_TRAIN_SETS; /** @@ -14,7 +17,12 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_TRAIN_SETS; public class ListTrainSets extends Command { @Override public void apply(final ModelRailwaySimulation simulation) { - simulation.printTrainSets(); + List trainSets = simulation.listTrainSets(); + if (trainSets.isEmpty()) { + Terminal.printLine("No train-set exists"); + } else { + trainSets.forEach(Terminal::printLine); + } } @Override diff --git a/src/edu/kit/informatik/ui/command/ListTrains.java b/src/edu/kit/informatik/ui/command/ListTrains.java index 881117c..085ea36 100644 --- a/src/edu/kit/informatik/ui/command/ListTrains.java +++ b/src/edu/kit/informatik/ui/command/ListTrains.java @@ -1,8 +1,11 @@ package edu.kit.informatik.ui.command; +import edu.kit.informatik.Terminal; import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.ui.InvalidInputException; +import java.util.List; + import static edu.kit.informatik.ui.command.CommandFactory.LIST_TRAINS; /** @@ -14,7 +17,12 @@ import static edu.kit.informatik.ui.command.CommandFactory.LIST_TRAINS; public class ListTrains extends Command { @Override public void apply(final ModelRailwaySimulation simulation) { - simulation.printTrains(); + List trains = simulation.listTrains(); + if (trains.isEmpty()) { + Terminal.printLine("No train exists"); + } else { + trains.forEach(Terminal::printLine); + } } @Override diff --git a/src/edu/kit/informatik/ui/command/ShowTrain.java b/src/edu/kit/informatik/ui/command/ShowTrain.java index 348e481..798e73a 100644 --- a/src/edu/kit/informatik/ui/command/ShowTrain.java +++ b/src/edu/kit/informatik/ui/command/ShowTrain.java @@ -1,5 +1,6 @@ package edu.kit.informatik.ui.command; +import edu.kit.informatik.Terminal; import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.ui.InvalidInputException; @@ -19,8 +20,8 @@ public class ShowTrain extends Command { private int id; @Override - public void apply(final ModelRailwaySimulation simulation) { - simulation.printTrain(id); + public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + simulation.showTrain(id).forEach(Terminal::printLine); } @Override diff --git a/src/edu/kit/informatik/ui/command/Step.java b/src/edu/kit/informatik/ui/command/Step.java index 9ac0b61..635f1c5 100644 --- a/src/edu/kit/informatik/ui/command/Step.java +++ b/src/edu/kit/informatik/ui/command/Step.java @@ -18,7 +18,7 @@ public class Step extends Command { private short speed; @Override - public void apply(final ModelRailwaySimulation simulation) { + public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { simulation.step(speed); }