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
cdc71c8253
commit
3f1594263e
@ -253,7 +253,7 @@ public class ModelRailwaySimulation {
|
||||
* @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) {
|
||||
public RollingStock getRollingStock(final String id) {
|
||||
if (id.matches("W\\+?\\d+")) {
|
||||
int coachId = Integer.parseInt(id.substring(1));
|
||||
for (Coach coach : coaches) {
|
||||
|
@ -16,6 +16,9 @@ import java.util.Set;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class RailwayNetwork {
|
||||
/**
|
||||
* Rail in this railway network. Usually contains tracks and switches.
|
||||
*/
|
||||
private List<Rail> rails = new ArrayList<>();
|
||||
|
||||
/**
|
||||
|
@ -20,25 +20,51 @@ public abstract class RollingStock {
|
||||
*/
|
||||
protected boolean couplingBack;
|
||||
|
||||
/**
|
||||
* @return identifier of this rolling stock
|
||||
*/
|
||||
public abstract String getIdentifier();
|
||||
|
||||
/**
|
||||
* @return length of this rolling stock
|
||||
*/
|
||||
public int getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return whether this rolling stock has a front coupling
|
||||
*/
|
||||
public boolean hasCouplingFront() {
|
||||
return couplingFront;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return whether this rolling stock has a back coupling
|
||||
*/
|
||||
public boolean hasCouplingBack() {
|
||||
return couplingBack;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param rollingStock other rolling stock
|
||||
* @return whether this rolling stock can couple to the back of the other rolling stock
|
||||
*/
|
||||
public abstract boolean canCoupleFrontTo(RollingStock rollingStock);
|
||||
|
||||
/**
|
||||
* @param series series/class of train set
|
||||
* @return whether this rolling stock can couple to train sets of that series
|
||||
*/
|
||||
public abstract boolean canCoupleToTrainSetSeries(String series);
|
||||
|
||||
/**
|
||||
* @return ASCII art representation of this rolling stock
|
||||
*/
|
||||
public abstract String[] textRepresentation();
|
||||
|
||||
/**
|
||||
* @return textual description of this rolling stock
|
||||
*/
|
||||
public abstract String description();
|
||||
}
|
||||
|
@ -7,6 +7,9 @@ package edu.kit.informatik.model;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class SteamEngine extends Engine {
|
||||
/**
|
||||
* ASCII art representation of a steam engine.
|
||||
*/
|
||||
private static final String[] STEAM_ENGINE_TEXT = new String[] {
|
||||
" ++ +------",
|
||||
" || |+-+ | ",
|
||||
|
@ -34,15 +34,17 @@ public final class Switch extends Rail {
|
||||
* @param id identifier of this switch
|
||||
* @throws IllegalArgumentException if the switch is not composed of straight lines
|
||||
*/
|
||||
public Switch(Vector2D start, Vector2D end1, Vector2D end2, int id) throws IllegalArgumentException {
|
||||
public Switch(final Vector2D start, final Vector2D end1, final Vector2D end2, final int id)
|
||||
throws IllegalArgumentException {
|
||||
super(id);
|
||||
if (start.getX() != end1.getX() && start.getY() != end1.getY()
|
||||
|| start.getX() != end2.getX() && start.getY() != end2.getY()) {
|
||||
throw new IllegalArgumentException("start has to be connected in straight lines to end positions!");
|
||||
}
|
||||
this.start = start;
|
||||
this.end1 = end1;
|
||||
this.end2 = end2;
|
||||
// make sure caller can not modify internal state after calling
|
||||
this.start = new Vector2D(start);
|
||||
this.end1 = new Vector2D(end1);
|
||||
this.end2 = new Vector2D(end2);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -15,10 +15,25 @@ import java.util.Set;
|
||||
* @version 1.0
|
||||
*/
|
||||
public final class Train {
|
||||
/**
|
||||
* Numerical identifer of this train.
|
||||
*/
|
||||
private final int identifier;
|
||||
/**
|
||||
* List of rolling stocks in this train.
|
||||
*/
|
||||
private final List<RollingStock> rollingStocks = new ArrayList<>();
|
||||
/**
|
||||
* List of positions this train touches.
|
||||
*/
|
||||
private List<Vector2D> position;
|
||||
/**
|
||||
* Direction of the train head.
|
||||
*/
|
||||
private Vector2D direction;
|
||||
/**
|
||||
* Set of rails this train occupies.
|
||||
*/
|
||||
private Set<Rail> occupiedRails;
|
||||
|
||||
/**
|
||||
@ -215,7 +230,8 @@ public final class Train {
|
||||
* @return the direction of this train
|
||||
*/
|
||||
public Vector2D getDirection() {
|
||||
return direction;
|
||||
// make sure caller can not modify internal state
|
||||
return new Vector2D(direction);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -243,7 +259,8 @@ public final class Train {
|
||||
* @param nextPosition position to move train to
|
||||
*/
|
||||
public void moveTo(final RailwayNetwork railNetwork, final Vector2D nextPosition) {
|
||||
position.add(0, nextPosition);
|
||||
// make sure caller can not modiy internal state
|
||||
position.add(0, new Vector2D(nextPosition));
|
||||
Vector2D last = position.remove(position.size() - 1);
|
||||
updateOccupiedRails(railNetwork, position.get(0), last, position.get(position.size() - 1));
|
||||
}
|
||||
@ -255,8 +272,8 @@ public final class Train {
|
||||
*/
|
||||
public void moveBackTo(final RailwayNetwork railNetwork, final Vector2D rearPosition) {
|
||||
Vector2D last = position.remove(0);
|
||||
position.add(rearPosition);
|
||||
updateOccupiedRails(railNetwork, rearPosition, last, position.get(0));
|
||||
position.add(new Vector2D(rearPosition));
|
||||
updateOccupiedRails(railNetwork, position.get(position.size() - 1), last, position.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -334,7 +351,7 @@ public final class Train {
|
||||
* @param other other train
|
||||
* @return whether this train touches the other train
|
||||
*/
|
||||
public boolean touches(Train other) {
|
||||
public boolean touches(final Train other) {
|
||||
return other.isOnAnyPosition(position);
|
||||
}
|
||||
|
||||
@ -342,7 +359,7 @@ public final class Train {
|
||||
* @param point position to check
|
||||
* @return whether this train is on the specified position
|
||||
*/
|
||||
public boolean isOnPosition(Vector2D point) {
|
||||
public boolean isOnPosition(final Vector2D point) {
|
||||
return position.contains(point);
|
||||
}
|
||||
|
||||
@ -350,7 +367,7 @@ public final class Train {
|
||||
* @param positions list of positions to check
|
||||
* @return whether this train is on any of the specified positions
|
||||
*/
|
||||
public boolean isOnAnyPosition(List<Vector2D> positions) {
|
||||
public boolean isOnAnyPosition(final List<Vector2D> positions) {
|
||||
return position.stream().anyMatch(positions::contains);
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,9 @@ package edu.kit.informatik.model;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class TrainSet extends RollingStock {
|
||||
/**
|
||||
* ASCII art representation of a train set.
|
||||
*/
|
||||
private static final String[] TRAIN_SET_TEXT = new String[] {
|
||||
" ++ ",
|
||||
" || ",
|
||||
@ -18,7 +21,13 @@ public class TrainSet extends RollingStock {
|
||||
" (O) (O) "
|
||||
};
|
||||
|
||||
/**
|
||||
* Series (class) of this train set.
|
||||
*/
|
||||
private String series;
|
||||
/**
|
||||
* Name of this train set. TODO: create NamedRollingStock class
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
|
@ -23,7 +23,7 @@ public class Vector2D {
|
||||
* @param x first component
|
||||
* @param y second component
|
||||
*/
|
||||
public Vector2D(int x, int y) {
|
||||
public Vector2D(final int x, final int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
@ -89,7 +89,7 @@ public class Vector2D {
|
||||
* @param other another vector
|
||||
* @return the difference of this vector and the other vector
|
||||
*/
|
||||
public Vector2D subtract(Vector2D other) {
|
||||
public Vector2D subtract(final Vector2D other) {
|
||||
return new Vector2D(x - other.x, y - other.y);
|
||||
}
|
||||
|
||||
@ -119,7 +119,7 @@ public class Vector2D {
|
||||
* Change the value of the first component of the vector
|
||||
* @param x value to change to
|
||||
*/
|
||||
public void setX(int x) {
|
||||
public void setX(final int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
@ -127,7 +127,7 @@ public class Vector2D {
|
||||
* Change the value of the second component of the vector
|
||||
* @param y value to change to
|
||||
*/
|
||||
public void setY(int y) {
|
||||
public void setY(final int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
|
@ -11,8 +11,17 @@ import edu.kit.informatik.Terminal;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class AddSwitch implements Command {
|
||||
/**
|
||||
* Start position of the switch to add.
|
||||
*/
|
||||
private final Vector2D start;
|
||||
/**
|
||||
* End position 1 of the switch to add.
|
||||
*/
|
||||
private final Vector2D end1;
|
||||
/**
|
||||
* End position 2 of the switch to add.
|
||||
*/
|
||||
private final Vector2D end2;
|
||||
|
||||
/**
|
||||
@ -22,12 +31,13 @@ public class AddSwitch implements Command {
|
||||
* @param end2 end position 2
|
||||
*/
|
||||
public AddSwitch(final Vector2D start, final Vector2D end1, final Vector2D end2) {
|
||||
this.start = start;
|
||||
this.end1 = end1;
|
||||
this.end2 = end2;
|
||||
// make sure caller can not modify internal state
|
||||
this.start = new Vector2D(start);
|
||||
this.end1 = new Vector2D(end1);
|
||||
this.end2 = new Vector2D(end2);
|
||||
}
|
||||
|
||||
public void apply(ModelRailwaySimulation simulation) {
|
||||
public void apply(final ModelRailwaySimulation simulation) {
|
||||
int id = simulation.addSwitch(start, end1, end2);
|
||||
if (id == -1) {
|
||||
Terminal.printError("switch not connected to existing rails");
|
||||
|
@ -11,7 +11,13 @@ import edu.kit.informatik.Terminal;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class AddTrack implements Command {
|
||||
/**
|
||||
* Start position of the new track.
|
||||
*/
|
||||
private Vector2D start;
|
||||
/**
|
||||
* End position of the new track.
|
||||
*/
|
||||
private Vector2D end;
|
||||
|
||||
/**
|
||||
@ -20,8 +26,9 @@ public class AddTrack implements Command {
|
||||
* @param end position of the end of the track
|
||||
*/
|
||||
public AddTrack(final Vector2D start, final Vector2D end) {
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
// make sure caller can not modify internal state
|
||||
this.start = new Vector2D(start);
|
||||
this.end = new Vector2D(end);
|
||||
}
|
||||
|
||||
public void apply(final ModelRailwaySimulation simulation) {
|
||||
|
@ -11,20 +11,26 @@ import edu.kit.informatik.Terminal;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class AddTrain implements Command {
|
||||
/**
|
||||
* Identifier of the train to add or modify.
|
||||
*/
|
||||
private final int trainId;
|
||||
/**
|
||||
* Identifier of the rolling stock to add.
|
||||
*/
|
||||
private final String rollingStockId;
|
||||
|
||||
/**
|
||||
* Construct a new 'add train' command.
|
||||
* @param trainId identifier of the train to be added/modified
|
||||
* @param rollingStockId identifer of the rolling to be added to the train
|
||||
* @param rollingStockId identifier of the rolling to be added to the train
|
||||
*/
|
||||
public AddTrain(final int trainId, final String rollingStockId) {
|
||||
this.trainId = trainId;
|
||||
this.rollingStockId = rollingStockId;
|
||||
}
|
||||
|
||||
public void apply(ModelRailwaySimulation simulation) {
|
||||
public void apply(final ModelRailwaySimulation simulation) {
|
||||
if (simulation.addTrain(trainId, rollingStockId)) {
|
||||
RollingStock rollingStock = simulation.getRollingStock(rollingStockId);
|
||||
Terminal.printLine(String.format("%s %s added to train %d",
|
||||
|
@ -10,5 +10,9 @@ import edu.kit.informatik.model.ModelRailwaySimulation;
|
||||
* @version 1.0
|
||||
*/
|
||||
public interface Command {
|
||||
/**
|
||||
* Apply this command to a model railway simulation.
|
||||
* @param simulation simulation to use
|
||||
*/
|
||||
void apply(ModelRailwaySimulation simulation);
|
||||
}
|
||||
|
@ -11,9 +11,21 @@ import edu.kit.informatik.Terminal;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class CreateCoach implements Command {
|
||||
/**
|
||||
* Type of the new coach.
|
||||
*/
|
||||
private final CoachType type;
|
||||
/**
|
||||
* Length of the new coach.
|
||||
*/
|
||||
private final int length;
|
||||
/**
|
||||
* Whether the new coach should have a front coupling.
|
||||
*/
|
||||
private final boolean couplingFront;
|
||||
/**
|
||||
* Whether the new coach should have a back coupling.
|
||||
*/
|
||||
private final boolean couplingBack;
|
||||
|
||||
/**
|
||||
@ -25,13 +37,16 @@ public class CreateCoach implements Command {
|
||||
*/
|
||||
public CreateCoach(final CoachType type, final int length,
|
||||
final boolean couplingFront, final boolean couplingBack) {
|
||||
if (type == null) {
|
||||
throw new IllegalArgumentException("coach type is null");
|
||||
}
|
||||
this.type = type;
|
||||
this.length = length;
|
||||
this.couplingFront = couplingFront;
|
||||
this.couplingBack = couplingBack;
|
||||
}
|
||||
|
||||
public void apply(ModelRailwaySimulation simulation) {
|
||||
public void apply(final ModelRailwaySimulation simulation) {
|
||||
int id = simulation.createCoach(type, length, couplingFront, couplingBack);
|
||||
if (id == -1) {
|
||||
Terminal.printError("could not add coach");
|
||||
|
@ -15,11 +15,29 @@ import edu.kit.informatik.ui.EngineType;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class CreateEngine implements Command {
|
||||
/**
|
||||
* Type of the new engine.
|
||||
*/
|
||||
private final EngineType type;
|
||||
/**
|
||||
* Series (class) of the new engine.
|
||||
*/
|
||||
private final String series;
|
||||
/**
|
||||
* Name of the new engine.
|
||||
*/
|
||||
private final String name;
|
||||
/**
|
||||
* Length of the new engine.
|
||||
*/
|
||||
private final int length;
|
||||
/**
|
||||
* Whether the new engine should have a front coupling.
|
||||
*/
|
||||
private final boolean couplingFront;
|
||||
/**
|
||||
* Whether the new engine should have a back coupling.
|
||||
*/
|
||||
private final boolean couplingBack;
|
||||
|
||||
/**
|
||||
@ -33,6 +51,9 @@ public class CreateEngine implements Command {
|
||||
*/
|
||||
public CreateEngine(final EngineType type, final String series, final String name, final int length,
|
||||
final boolean couplingFront, final boolean couplingBack) {
|
||||
if (type == null) {
|
||||
throw new IllegalArgumentException("engine type is null!");
|
||||
}
|
||||
this.type = type;
|
||||
this.series = series;
|
||||
this.name = name;
|
||||
@ -42,7 +63,7 @@ public class CreateEngine implements Command {
|
||||
}
|
||||
|
||||
public void apply(final ModelRailwaySimulation simulation) {
|
||||
final Engine engine;
|
||||
Engine engine;
|
||||
switch (type) {
|
||||
case ELECTRICAL:
|
||||
engine = new ElectricalEngine(series, name, length, couplingFront, couplingBack);
|
||||
|
@ -11,10 +11,25 @@ import edu.kit.informatik.model.TrainSet;
|
||||
* @version Arne Keller
|
||||
*/
|
||||
public class CreateTrainSet implements Command {
|
||||
/**
|
||||
* Series (class) of the new train set.
|
||||
*/
|
||||
private final String series;
|
||||
/**
|
||||
* Name of the new train set.
|
||||
*/
|
||||
private final String name;
|
||||
/**
|
||||
* Length of the new train set.
|
||||
*/
|
||||
private final int length;
|
||||
/**
|
||||
* Whether the new train set should have front coupling.
|
||||
*/
|
||||
private final boolean couplingFront;
|
||||
/**
|
||||
* Whether the new train set should have a back coupling.
|
||||
*/
|
||||
private final boolean couplingBack;
|
||||
|
||||
/**
|
||||
|
@ -10,6 +10,9 @@ import edu.kit.informatik.Terminal;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class DeleteRollingStock implements Command {
|
||||
/**
|
||||
* Identifier of the rolling stock to delete.
|
||||
*/
|
||||
private final String id;
|
||||
|
||||
/**
|
||||
|
@ -10,6 +10,9 @@ import edu.kit.informatik.Terminal;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class DeleteTrack implements Command {
|
||||
/**
|
||||
* Identifier of the rail to delete.
|
||||
*/
|
||||
private final int id;
|
||||
|
||||
/**
|
||||
|
@ -10,6 +10,9 @@ import edu.kit.informatik.Terminal;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class DeleteTrain implements Command {
|
||||
/**
|
||||
* Identifer of the train to delete.
|
||||
*/
|
||||
private final int id;
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user