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;
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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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);
}
}

View File

@ -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<String> listTracks() {
return rails.keySet().stream().sorted().map(rails::get).map(Object::toString).collect(Collectors.toList());
}
/**

View File

@ -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<String> show() {
List<StringBuilder> 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());
}
/**

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() {
if (trains.isEmpty()) {
Terminal.printLine("No train exists");
return;
}
trains.values().forEach(Terminal::printLine);
public List<String> 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<String> 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");

View File

@ -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<String> coaches = simulation.listCoaches();
if (coaches.isEmpty()) {
Terminal.printLine("No coach exists");
} else {
coaches.forEach(Terminal::printLine);
}
}
@Override

View File

@ -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<String> engines = simulation.listEngines();
if (engines.isEmpty()) {
Terminal.printLine("No engine exists");
} else {
engines.forEach(Terminal::printLine);
}
}
@Override

View File

@ -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<String> tracks = simulation.listTracks();
if (tracks.isEmpty()) {
Terminal.printLine("No track exists");
} else {
tracks.forEach(Terminal::printLine);
}
}
@Override

View File

@ -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<String> trainSets = simulation.listTrainSets();
if (trainSets.isEmpty()) {
Terminal.printLine("No train-set exists");
} else {
trainSets.forEach(Terminal::printLine);
}
}
@Override

View File

@ -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<String> trains = simulation.listTrains();
if (trains.isEmpty()) {
Terminal.printLine("No train exists");
} else {
trains.forEach(Terminal::printLine);
}
}
@Override

View File

@ -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

View File

@ -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);
}