Move Terminal printing out of model classes

This commit is contained in:
Arne Keller 2020-02-22 18:23:12 +01:00
parent 40cc52d2a3
commit e487a23251
11 changed files with 130 additions and 88 deletions

View File

@ -1,7 +1,6 @@
package edu.kit.informatik.model; package edu.kit.informatik.model;
import edu.kit.informatik.ui.CoachType; import edu.kit.informatik.ui.CoachType;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.ArrayList; 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. * their current configuration.
*
* @return list of rails
*/ */
public void printTracks() { public List<String> listTracks() {
railNetwork.printTracks(); 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() { public List<String> listEngines() {
if (engines.isEmpty()) {
Terminal.printLine("No engine exists");
} else {
engines.sort(Comparator.comparing(Engine::getIdentifier)); engines.sort(Comparator.comparing(Engine::getIdentifier));
for (Engine engine : engines) { return engines.stream().map(engine -> {
String trainId = trainManager.getTrainContainingRollingStock(engine) String trainId = trainManager.getTrainContainingRollingStock(engine)
.map(train -> Integer.toString(train.getIdentifier())) .map(train -> Integer.toString(train.getIdentifier()))
.orElse("none"); .orElse("none");
Terminal.printLine(String.format("%s %s", trainId, engine)); 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() { public List<String> listCoaches() {
if (coaches.isEmpty()) { return coaches.keySet().stream().sorted().map(coachId -> {
Terminal.printLine("No coach exists"); Coach coach = coaches.get(coachId);
} else {
for (Integer identifier : coaches.keySet().stream().sorted().collect(Collectors.toList())) {
Coach coach = coaches.get(identifier);
String trainId = trainManager.getTrainContainingRollingStock(coach) String trainId = trainManager.getTrainContainingRollingStock(coach)
.map(train -> Integer.toString(train.getIdentifier())) .map(train -> Integer.toString(train.getIdentifier()))
.orElse("none"); .orElse("none");
Terminal.printLine(String.format("%d %s %s %d %b %b", return String.format("%d %s %s %d %b %b",
coach.getNumericalIdentifier(), trainId, coach.getType(), coach.getNumericalIdentifier(), trainId, coach.getType(),
coach.getLength(), coach.hasCouplingFront(), coach.hasCouplingBack())); 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() { public List<String> listTrainSets() {
if (trainSets.isEmpty()) {
Terminal.printLine("No train-set exists");
} else {
trainSets.sort(Comparator.comparing(TrainSet::getIdentifier)); trainSets.sort(Comparator.comparing(TrainSet::getIdentifier));
for (TrainSet trainSet : trainSets) { return trainSets.stream().map(trainSet -> {
String trainId = trainManager.getTrainContainingRollingStock(trainSet) String trainId = trainManager.getTrainContainingRollingStock(trainSet)
.map(train -> Integer.toString(train.getIdentifier())) .map(train -> Integer.toString(train.getIdentifier()))
.orElse("none"); .orElse("none");
Terminal.printLine(String.format("%s %s", trainId, trainSet)); 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() { public List<String> listTrains() {
trainManager.printTrains(); 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 * @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) { public List<String> showTrain(int id) throws InvalidInputException {
trainManager.printTrain(id); return trainManager.showTrain(id);
} }
/** /**
@ -299,8 +298,9 @@ public class ModelRailwaySimulation {
/** /**
* Move the trains in this simulation, printing their new positions and any collisions. * Move the trains in this simulation, printing their new positions and any collisions.
* @param speed amount of steps to move the trains * @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); trainManager.step(speed);
} }
} }

View File

@ -1,11 +1,11 @@
package edu.kit.informatik.model; package edu.kit.informatik.model;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Queue; import java.util.Queue;
import java.util.Set; 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. * their current configuration.
*
* @return list of rails
*/ */
public void printTracks() { public List<String> listTracks() {
if (rails.isEmpty()) { return rails.keySet().stream().sorted().map(rails::get).map(Object::toString).collect(Collectors.toList());
Terminal.printLine("No track exists");
} else {
for (int id : rails.keySet().stream().sorted().collect(Collectors.toList())) {
Terminal.printLine(rails.get(id));
}
}
} }
/** /**

View File

@ -1,6 +1,5 @@
package edu.kit.informatik.model; package edu.kit.informatik.model;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.ArrayList; 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<String> show() {
List<StringBuilder> lines = new ArrayList<>(); List<StringBuilder> lines = new ArrayList<>();
for (RollingStock rollingStock : rollingStocks) { for (RollingStock rollingStock : rollingStocks) {
for (StringBuilder line : lines) { 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());
} }
/** /**

View File

@ -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() { public List<String> listTrains() {
if (trains.isEmpty()) { return trains.keySet().stream()
Terminal.printLine("No train exists"); .sorted()
return; .map(trains::get)
} .map(Object::toString)
trains.values().forEach(Terminal::printLine); .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 * @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<String> showTrain(int trainId) throws InvalidInputException {
Train train = trains.get(trainId); Train train = trains.get(trainId);
if (train != null) { if (train != null) {
train.print(); return train.show();
} else { } 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. * Move the trains in this simulation, printing their new positions and any collisions.
* @param speed amount of steps to move the trains * @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()) { if (!railNetwork.isReadyForTrains()) {
Terminal.printError("rail tracks/switches not set up"); throw new InvalidInputException("rail tracks/switches not set up");
return;
} }
if (trains.values().stream().noneMatch(Train::isPlaced)) { if (trains.values().stream().noneMatch(Train::isPlaced)) {
Terminal.printLine("OK"); Terminal.printLine("OK");

View File

@ -1,8 +1,11 @@
package edu.kit.informatik.ui.command; package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.model.ModelRailwaySimulation;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.List;
import static edu.kit.informatik.ui.command.CommandFactory.LIST_COACHES; 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 { public class ListCoaches extends Command {
@Override @Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
simulation.printCoaches(); List<String> coaches = simulation.listCoaches();
if (coaches.isEmpty()) {
Terminal.printLine("No coach exists");
} else {
coaches.forEach(Terminal::printLine);
}
} }
@Override @Override

View File

@ -1,8 +1,11 @@
package edu.kit.informatik.ui.command; package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.model.ModelRailwaySimulation;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.List;
import static edu.kit.informatik.ui.command.CommandFactory.LIST_ENGINES; 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 { public class ListEngines extends Command {
@Override @Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
simulation.printEngines(); List<String> engines = simulation.listEngines();
if (engines.isEmpty()) {
Terminal.printLine("No engine exists");
} else {
engines.forEach(Terminal::printLine);
}
} }
@Override @Override

View File

@ -1,8 +1,11 @@
package edu.kit.informatik.ui.command; package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.model.ModelRailwaySimulation;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.List;
import static edu.kit.informatik.ui.command.CommandFactory.LIST_TRACKS; 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 { public class ListTracks extends Command {
@Override @Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
simulation.printTracks(); List<String> tracks = simulation.listTracks();
if (tracks.isEmpty()) {
Terminal.printLine("No track exists");
} else {
tracks.forEach(Terminal::printLine);
}
} }
@Override @Override

View File

@ -1,8 +1,11 @@
package edu.kit.informatik.ui.command; package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.model.ModelRailwaySimulation;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.List;
import static edu.kit.informatik.ui.command.CommandFactory.LIST_TRAIN_SETS; 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 { public class ListTrainSets extends Command {
@Override @Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
simulation.printTrainSets(); List<String> trainSets = simulation.listTrainSets();
if (trainSets.isEmpty()) {
Terminal.printLine("No train-set exists");
} else {
trainSets.forEach(Terminal::printLine);
}
} }
@Override @Override

View File

@ -1,8 +1,11 @@
package edu.kit.informatik.ui.command; package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.model.ModelRailwaySimulation;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.List;
import static edu.kit.informatik.ui.command.CommandFactory.LIST_TRAINS; 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 { public class ListTrains extends Command {
@Override @Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
simulation.printTrains(); List<String> trains = simulation.listTrains();
if (trains.isEmpty()) {
Terminal.printLine("No train exists");
} else {
trains.forEach(Terminal::printLine);
}
} }
@Override @Override

View File

@ -1,5 +1,6 @@
package edu.kit.informatik.ui.command; package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.model.ModelRailwaySimulation;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
@ -19,8 +20,8 @@ public class ShowTrain extends Command {
private int id; private int id;
@Override @Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException {
simulation.printTrain(id); simulation.showTrain(id).forEach(Terminal::printLine);
} }
@Override @Override

View File

@ -18,7 +18,7 @@ public class Step extends Command {
private short speed; private short speed;
@Override @Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException {
simulation.step(speed); simulation.step(speed);
} }