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
|
||||
*/
|
||||
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[] {
|
||||
" ____",
|
||||
"/--------------| |",
|
||||
|
@ -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[] {
|
||||
" _____________|____ ",
|
||||
" /_| ____________ |_\\ ",
|
||||
|
@ -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[] {
|
||||
" ___ ",
|
||||
" \\ ",
|
||||
|
@ -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<Engine> engines = new ArrayList<>();
|
||||
/**
|
||||
* List of train sets used in the simulation.
|
||||
*/
|
||||
private final List<TrainSet> trainSets = new ArrayList<>();
|
||||
/**
|
||||
* List of coaches used in the simulation.
|
||||
*/
|
||||
private final List<Coach> coaches = new ArrayList<>();
|
||||
/**
|
||||
* List of trains simulated.
|
||||
*/
|
||||
private final List<Train> 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<HashSet<Train>> collisions = getStaticCollisions(false);
|
||||
List<HashSet<Train>> collisions = getStaticCollisions();
|
||||
if (!collisions.isEmpty()) {
|
||||
// remove if colliding
|
||||
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()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
@ -385,16 +440,13 @@ public class ModelRailwaySimulation {
|
||||
collisions.add(collision);
|
||||
}
|
||||
}
|
||||
if (removeTrains) {
|
||||
for (HashSet<Train> 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<Set<Train>> getCollisionsOfOneStep() {
|
||||
ArrayList<Set<Train>> collisions = new ArrayList<>();
|
||||
Map<Train, Set<Rail>> 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<Set<Train>> getCollisionsOfOneReverseStep() {
|
||||
ArrayList<Set<Train>> collisions = new ArrayList<>();
|
||||
Map<Train, Set<Rail>> 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<Set<Train>> collisions = new ArrayList<>();
|
||||
// first, handle positive speed (forward)
|
||||
for (int i = 0; i < speed; i++) {
|
||||
List<Set<Train>> newCollisions = getCollisionsOfOneStep();
|
||||
collisions.addAll(newCollisions);
|
||||
}
|
||||
// then, handle negative speed (backward)
|
||||
for (int i = 0; i > speed; i--) {
|
||||
List<Set<Train>> newCollisions = getCollisionsOfOneReverseStep();
|
||||
collisions.addAll(newCollisions);
|
||||
}
|
||||
// (only one of the loops above actually runs)
|
||||
train: for (Train train : trains) {
|
||||
// print collision
|
||||
for (Set<Train> 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()));
|
||||
}
|
||||
|
@ -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";
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user