mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-24 09:24:58 +00:00
Checkstyle
This commit is contained in:
parent
48b4562bde
commit
cdc71c8253
@ -9,6 +9,9 @@ import edu.kit.informatik.ui.CoachType;
|
|||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class Coach extends RollingStock {
|
public class Coach extends RollingStock {
|
||||||
|
/**
|
||||||
|
* ASCII art representation of a passenger coach.
|
||||||
|
*/
|
||||||
private static final String[] PASSENGER_TEXT = new String[] {
|
private static final String[] PASSENGER_TEXT = new String[] {
|
||||||
"____________________",
|
"____________________",
|
||||||
"| ___ ___ ___ ___ |",
|
"| ___ ___ ___ ___ |",
|
||||||
@ -17,6 +20,10 @@ public class Coach extends RollingStock {
|
|||||||
"|__________________|",
|
"|__________________|",
|
||||||
" (O) (O) "
|
" (O) (O) "
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ASCII art representation of a freight coach.
|
||||||
|
*/
|
||||||
private static final String[] FREIGHT_TEXT = new String[] {
|
private static final String[] FREIGHT_TEXT = new String[] {
|
||||||
"| |",
|
"| |",
|
||||||
"| |",
|
"| |",
|
||||||
@ -24,6 +31,10 @@ public class Coach extends RollingStock {
|
|||||||
"|__________________|",
|
"|__________________|",
|
||||||
" (O) (O) "
|
" (O) (O) "
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ASCII art represantation of a special coach.
|
||||||
|
*/
|
||||||
private static final String[] SPECIAL_TEXT = new String[] {
|
private static final String[] SPECIAL_TEXT = new String[] {
|
||||||
" ____",
|
" ____",
|
||||||
"/--------------| |",
|
"/--------------| |",
|
||||||
|
@ -7,6 +7,9 @@ package edu.kit.informatik.model;
|
|||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class DieselEngine extends Engine {
|
public class DieselEngine extends Engine {
|
||||||
|
/**
|
||||||
|
* ASCII art representation of a diesel engine.
|
||||||
|
*/
|
||||||
private static final String[] DIESEL_ENGINE_TEXT = new String[] {
|
private static final String[] DIESEL_ENGINE_TEXT = new String[] {
|
||||||
" _____________|____ ",
|
" _____________|____ ",
|
||||||
" /_| ____________ |_\\ ",
|
" /_| ____________ |_\\ ",
|
||||||
|
@ -7,6 +7,9 @@ package edu.kit.informatik.model;
|
|||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class ElectricalEngine extends Engine {
|
public class ElectricalEngine extends Engine {
|
||||||
|
/**
|
||||||
|
* ASCII art of an electrical engine.
|
||||||
|
*/
|
||||||
private static final String[] ELECTRICAL_ENGINE_TEXT = new String[] {
|
private static final String[] ELECTRICAL_ENGINE_TEXT = new String[] {
|
||||||
" ___ ",
|
" ___ ",
|
||||||
" \\ ",
|
" \\ ",
|
||||||
|
@ -13,11 +13,32 @@ import java.util.Map;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Model railway simulation. Can simulate trains on a railway network.
|
||||||
|
*
|
||||||
|
* @author Arne Keller
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
public class ModelRailwaySimulation {
|
public class ModelRailwaySimulation {
|
||||||
|
/**
|
||||||
|
* Railway network used in this simulation.
|
||||||
|
*/
|
||||||
private final RailwayNetwork railNetwork = new RailwayNetwork();
|
private final RailwayNetwork railNetwork = new RailwayNetwork();
|
||||||
|
/**
|
||||||
|
* List of engines used in the simulation.
|
||||||
|
*/
|
||||||
private final List<Engine> engines = new ArrayList<>();
|
private final List<Engine> engines = new ArrayList<>();
|
||||||
|
/**
|
||||||
|
* List of train sets used in the simulation.
|
||||||
|
*/
|
||||||
private final List<TrainSet> trainSets = new ArrayList<>();
|
private final List<TrainSet> trainSets = new ArrayList<>();
|
||||||
|
/**
|
||||||
|
* List of coaches used in the simulation.
|
||||||
|
*/
|
||||||
private final List<Coach> coaches = new ArrayList<>();
|
private final List<Coach> coaches = new ArrayList<>();
|
||||||
|
/**
|
||||||
|
* List of trains simulated.
|
||||||
|
*/
|
||||||
private final List<Train> trains = new ArrayList<>();
|
private final List<Train> trains = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -93,6 +114,9 @@ public class ModelRailwaySimulation {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print a list of engines added to the simulation.
|
||||||
|
*/
|
||||||
public void printEngines() {
|
public void printEngines() {
|
||||||
if (engines.isEmpty()) {
|
if (engines.isEmpty()) {
|
||||||
Terminal.printLine("No engine exists");
|
Terminal.printLine("No engine exists");
|
||||||
@ -130,6 +154,10 @@ public class ModelRailwaySimulation {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the next coach identifier.
|
||||||
|
* @return the next coach identifier, or -1 if none available
|
||||||
|
*/
|
||||||
private int getNextCoachIdentifier() {
|
private int getNextCoachIdentifier() {
|
||||||
int id = 1;
|
int id = 1;
|
||||||
for (Coach coach : coaches) {
|
for (Coach coach : coaches) {
|
||||||
@ -140,6 +168,9 @@ public class ModelRailwaySimulation {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print a list of coaches added to the simulation.
|
||||||
|
*/
|
||||||
public void printCoaches() {
|
public void printCoaches() {
|
||||||
if (coaches.isEmpty()) {
|
if (coaches.isEmpty()) {
|
||||||
Terminal.printLine("No coach exists");
|
Terminal.printLine("No coach exists");
|
||||||
@ -180,6 +211,9 @@ public class ModelRailwaySimulation {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print a list of train sets added to the simulation.
|
||||||
|
*/
|
||||||
public void printTrainSets() {
|
public void printTrainSets() {
|
||||||
if (trainSets.isEmpty()) {
|
if (trainSets.isEmpty()) {
|
||||||
Terminal.printLine("No train-set exists");
|
Terminal.printLine("No train-set exists");
|
||||||
@ -202,23 +236,24 @@ public class ModelRailwaySimulation {
|
|||||||
* @param id identifier of the rolling stock to remove
|
* @param id identifier of the rolling stock to remove
|
||||||
* @return whether the rolling was successfully removed
|
* @return whether the rolling was successfully removed
|
||||||
*/
|
*/
|
||||||
public boolean deleteRollingStock(String id) {
|
public boolean deleteRollingStock(final String id) {
|
||||||
RollingStock rollingStock = getRollingStock(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;
|
return false;
|
||||||
}
|
}
|
||||||
for (Train train : trains) {
|
|
||||||
if (train.contains(rollingStock)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
engines.remove(rollingStock);
|
engines.remove(rollingStock);
|
||||||
trainSets.remove(rollingStock);
|
trainSets.remove(rollingStock);
|
||||||
coaches.remove(rollingStock);
|
coaches.remove(rollingStock);
|
||||||
return true;
|
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+")) {
|
if (id.matches("W\\+?\\d+")) {
|
||||||
int coachId = Integer.parseInt(id.substring(1));
|
int coachId = Integer.parseInt(id.substring(1));
|
||||||
for (Coach coach : coaches) {
|
for (Coach coach : coaches) {
|
||||||
@ -241,6 +276,12 @@ public class ModelRailwaySimulation {
|
|||||||
return null;
|
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) {
|
public boolean addTrain(int trainId, String rollingStockId) {
|
||||||
RollingStock rollingStock = getRollingStock(rollingStockId);
|
RollingStock rollingStock = getRollingStock(rollingStockId);
|
||||||
if (rollingStock == null) {
|
if (rollingStock == null) {
|
||||||
@ -267,6 +308,10 @@ public class ModelRailwaySimulation {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the next train identifier.
|
||||||
|
* @return the next train identfier, or -1 if none available
|
||||||
|
*/
|
||||||
private int getNextTrainIdentifier() {
|
private int getNextTrainIdentifier() {
|
||||||
int id = 1;
|
int id = 1;
|
||||||
for (Train train : trains) {
|
for (Train train : trains) {
|
||||||
@ -277,6 +322,11 @@ public class ModelRailwaySimulation {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a train.
|
||||||
|
* @param id identifier of the train
|
||||||
|
* @return whether the train could be deleted
|
||||||
|
*/
|
||||||
public boolean deleteTrain(int id) {
|
public boolean deleteTrain(int id) {
|
||||||
Train train = getTrain(id);
|
Train train = getTrain(id);
|
||||||
if (train != null) {
|
if (train != null) {
|
||||||
@ -286,23 +336,24 @@ public class ModelRailwaySimulation {
|
|||||||
return false;
|
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) {
|
private Train getTrain(int id) {
|
||||||
for (Train train : trains) {
|
return trains.stream().filter((train) -> train.getIdentifier() == id).findFirst().orElse(null);
|
||||||
if (train.getIdentifier() == id) {
|
|
||||||
return train;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print a list of trains in the simulation.
|
||||||
|
*/
|
||||||
public void printTrains() {
|
public void printTrains() {
|
||||||
if (trains.isEmpty()) {
|
if (trains.isEmpty()) {
|
||||||
Terminal.printLine("No train exists"); // TODO not in fucking spec
|
Terminal.printLine("No train exists"); // TODO not in fucking spec
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (Train train : trains) {
|
trains.forEach(Terminal::printLine);
|
||||||
Terminal.printLine(train);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -337,7 +388,7 @@ public class ModelRailwaySimulation {
|
|||||||
|
|
||||||
if (placed) {
|
if (placed) {
|
||||||
// now, check for collisions
|
// now, check for collisions
|
||||||
List<HashSet<Train>> collisions = getStaticCollisions(false);
|
List<HashSet<Train>> collisions = getStaticCollisions();
|
||||||
if (!collisions.isEmpty()) {
|
if (!collisions.isEmpty()) {
|
||||||
// remove if colliding
|
// remove if colliding
|
||||||
train.removeFromRails();
|
train.removeFromRails();
|
||||||
@ -350,7 +401,11 @@ public class ModelRailwaySimulation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArrayList<HashSet<Train>> getStaticCollisions(boolean removeTrains) {
|
/**
|
||||||
|
* Get collisions of trains currently placed.
|
||||||
|
* @return list of collisions (never null, sometimes empty)
|
||||||
|
*/
|
||||||
|
private ArrayList<HashSet<Train>> getStaticCollisions() {
|
||||||
if (trains.isEmpty()) {
|
if (trains.isEmpty()) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
@ -385,16 +440,13 @@ public class ModelRailwaySimulation {
|
|||||||
collisions.add(collision);
|
collisions.add(collision);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (removeTrains) {
|
|
||||||
for (HashSet<Train> collision : collisions) {
|
|
||||||
for (Train t : collision) {
|
|
||||||
t.removeFromRails();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return collisions;
|
return collisions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get collisions of moving the trains one step forward.
|
||||||
|
* @return list of collisions (never null, sometimes empty)
|
||||||
|
*/
|
||||||
private ArrayList<Set<Train>> getCollisionsOfOneStep() {
|
private ArrayList<Set<Train>> getCollisionsOfOneStep() {
|
||||||
ArrayList<Set<Train>> collisions = new ArrayList<>();
|
ArrayList<Set<Train>> collisions = new ArrayList<>();
|
||||||
Map<Train, Set<Rail>> occupiedRails = new HashMap<>();
|
Map<Train, Set<Rail>> occupiedRails = new HashMap<>();
|
||||||
@ -461,6 +513,10 @@ public class ModelRailwaySimulation {
|
|||||||
return collisions;
|
return collisions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get collisions of moving the trains one step backward.
|
||||||
|
* @return list of collisions (never null, sometimes empty)
|
||||||
|
*/
|
||||||
private ArrayList<Set<Train>> getCollisionsOfOneReverseStep() {
|
private ArrayList<Set<Train>> getCollisionsOfOneReverseStep() {
|
||||||
ArrayList<Set<Train>> collisions = new ArrayList<>();
|
ArrayList<Set<Train>> collisions = new ArrayList<>();
|
||||||
Map<Train, Set<Rail>> occupiedRails = new HashMap<>();
|
Map<Train, Set<Rail>> occupiedRails = new HashMap<>();
|
||||||
@ -524,6 +580,10 @@ public class ModelRailwaySimulation {
|
|||||||
return collisions;
|
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) {
|
public void step(final short speed) {
|
||||||
if (!railNetwork.isReadyForTrains()) {
|
if (!railNetwork.isReadyForTrains()) {
|
||||||
Terminal.printError("rail tracks/switches not set up");
|
Terminal.printError("rail tracks/switches not set up");
|
||||||
@ -535,15 +595,19 @@ public class ModelRailwaySimulation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<Set<Train>> collisions = new ArrayList<>();
|
List<Set<Train>> collisions = new ArrayList<>();
|
||||||
|
// first, handle positive speed (forward)
|
||||||
for (int i = 0; i < speed; i++) {
|
for (int i = 0; i < speed; i++) {
|
||||||
List<Set<Train>> newCollisions = getCollisionsOfOneStep();
|
List<Set<Train>> newCollisions = getCollisionsOfOneStep();
|
||||||
collisions.addAll(newCollisions);
|
collisions.addAll(newCollisions);
|
||||||
}
|
}
|
||||||
|
// then, handle negative speed (backward)
|
||||||
for (int i = 0; i > speed; i--) {
|
for (int i = 0; i > speed; i--) {
|
||||||
List<Set<Train>> newCollisions = getCollisionsOfOneReverseStep();
|
List<Set<Train>> newCollisions = getCollisionsOfOneReverseStep();
|
||||||
collisions.addAll(newCollisions);
|
collisions.addAll(newCollisions);
|
||||||
}
|
}
|
||||||
|
// (only one of the loops above actually runs)
|
||||||
train: for (Train train : trains) {
|
train: for (Train train : trains) {
|
||||||
|
// print collision
|
||||||
for (Set<Train> collisionSet : collisions) {
|
for (Set<Train> collisionSet : collisions) {
|
||||||
if (!collisionSet.contains(train)) {
|
if (!collisionSet.contains(train)) {
|
||||||
continue;
|
continue;
|
||||||
@ -557,6 +621,7 @@ public class ModelRailwaySimulation {
|
|||||||
}
|
}
|
||||||
continue train;
|
continue train;
|
||||||
}
|
}
|
||||||
|
// or position, if not in collision
|
||||||
if (train.isPlaced()) {
|
if (train.isPlaced()) {
|
||||||
Terminal.printLine(String.format("Train %d at %s", train.getIdentifier(), train.getFrontPosition()));
|
Terminal.printLine(String.format("Train %d at %s", train.getIdentifier(), train.getFrontPosition()));
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,9 @@ import edu.kit.informatik.ui.command.CommandFactory;
|
|||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class CommandLine {
|
public class CommandLine {
|
||||||
|
/**
|
||||||
|
* Command used to exit the simulation and terminate the program.
|
||||||
|
*/
|
||||||
private static final String EXIT = "exit";
|
private static final String EXIT = "exit";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user