Checkstyle

This commit is contained in:
Arne Keller 2020-02-17 11:19:56 +01:00
parent 3f1594263e
commit fbf80103be
7 changed files with 63 additions and 73 deletions

View File

@ -29,16 +29,13 @@ public class DieselEngine extends Engine {
*/ */
public DieselEngine(final String series, final String name, final int length, public DieselEngine(final String series, final String name, final int length,
final boolean couplingFront, final boolean couplingBack) { final boolean couplingFront, final boolean couplingBack) {
super.name = name; super(series, name, length, couplingFront, couplingBack);
super.series = series;
super.length = length;
super.couplingFront = couplingFront;
super.couplingBack = couplingBack;
} }
@Override @Override
public String toString() { public String toString() {
return String.format("d %s %s %d %b %b", series, name, length, couplingFront, couplingBack); return String.format("d %s %s %d %b %b", getSeries(), getName(), getLength(),
hasCouplingFront(), hasCouplingBack());
} }
@Override @Override

View File

@ -31,16 +31,13 @@ public class ElectricalEngine extends Engine {
*/ */
public ElectricalEngine(final String series, final String name, final int length, public ElectricalEngine(final String series, final String name, final int length,
final boolean couplingFront, final boolean couplingBack) { final boolean couplingFront, final boolean couplingBack) {
super.name = name; super(series, name, length, couplingFront, couplingBack);
super.series = series;
super.length = length;
super.couplingFront = couplingFront;
super.couplingBack = couplingBack;
} }
@Override @Override
public String toString() { public String toString() {
return String.format("e %s %s %d %b %b", series, name, length, couplingFront, couplingBack); return String.format("e %s %s %d %b %b", getSeries(), getName(), getLength(),
hasCouplingFront(), hasCouplingBack());
} }
@Override @Override

View File

@ -10,11 +10,18 @@ public abstract class Engine extends RollingStock {
/** /**
* Series/class of this engine. * Series/class of this engine.
*/ */
protected String series; private String series;
/** /**
* Name of this engine. * Name of this engine.
*/ */
protected String name; private String name;
protected Engine(final String series, final String name, final int length,
final boolean couplingFront, final boolean couplingBack) {
super(length, couplingFront, couplingBack);
this.series = series;
this.name = name;
}
/** /**
* @return the name of this engine * @return the name of this engine
@ -23,6 +30,13 @@ public abstract class Engine extends RollingStock {
return name; return name;
} }
/**
* @return the series of this engine
*/
public String getSeries() {
return series;
}
@Override @Override
public String getIdentifier() { public String getIdentifier() {
return String.format("%s-%s", series, getName()); return String.format("%s-%s", series, getName());

View File

@ -10,7 +10,7 @@ public abstract class Rail {
/** /**
* Unique identifier of this rail. * Unique identifier of this rail.
*/ */
protected final int id; private final int id;
/** /**
* Initialize a new rail with the specified identifier. * Initialize a new rail with the specified identifier.

View File

@ -10,15 +10,21 @@ public abstract class RollingStock {
/** /**
* Length of this rolling stock. * Length of this rolling stock.
*/ */
protected int length; private int length;
/** /**
* Whether this rolling stock has a front coupling. * Whether this rolling stock has a front coupling.
*/ */
protected boolean couplingFront; private boolean couplingFront;
/** /**
* Whether this rolling stack has a back coupling. * Whether this rolling stack has a back coupling.
*/ */
protected boolean couplingBack; private boolean couplingBack;
protected RollingStock(final int length, final boolean couplingFront, final boolean couplingBack) {
this.length = length;
this.couplingFront = couplingFront;
this.couplingBack = couplingBack;
}
/** /**
* @return identifier of this rolling stock * @return identifier of this rolling stock

View File

@ -29,16 +29,13 @@ public class SteamEngine extends Engine {
*/ */
public SteamEngine(final String series, final String name, final int length, public SteamEngine(final String series, final String name, final int length,
final boolean couplingFront, final boolean couplingBack) { final boolean couplingFront, final boolean couplingBack) {
super.name = name; super(series, name, length, couplingFront, couplingBack);
super.series = series;
super.length = length;
super.couplingFront = couplingFront;
super.couplingBack = couplingBack;
} }
@Override @Override
public String toString() { public String toString() {
return String.format("s %s %s %d %b %b", series, name, length, couplingFront, couplingBack); return String.format("s %s %s %d %b %b", getSeries(), getName(), getLength(),
hasCouplingFront(), hasCouplingBack());
} }
@Override @Override

View File

@ -1,6 +1,5 @@
package edu.kit.informatik.ui.command; package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.Vector2D; import edu.kit.informatik.model.Vector2D;
import edu.kit.informatik.ui.CoachType; import edu.kit.informatik.ui.CoachType;
import edu.kit.informatik.ui.EngineType; import edu.kit.informatik.ui.EngineType;
@ -65,23 +64,20 @@ public class CommandFactory {
String arguments = command.substring(ADD_TRACK.length()); String arguments = command.substring(ADD_TRACK.length());
Matcher matcher = ADD_TRACK_ARGUMENTS.matcher(arguments); Matcher matcher = ADD_TRACK_ARGUMENTS.matcher(arguments);
if (!matcher.matches()) { if (!matcher.matches()) {
Terminal.printError("invalid add track argument syntax"); throw new InvalidInputException("invalid add track argument syntax");
return null;
} }
Vector2D start = Vector2D.parse(matcher.group(1)); Vector2D start = Vector2D.parse(matcher.group(1));
Vector2D end = Vector2D.parse(matcher.group(2)); Vector2D end = Vector2D.parse(matcher.group(2));
if (start.getX() == end.getX() || start.getY() == end.getY()) { if (start.getX() == end.getX() || start.getY() == end.getY()) {
return new AddTrack(start, end); return new AddTrack(start, end);
} else { } else {
Terminal.printError("invalid track segment: not a straight line"); throw new InvalidInputException("invalid track segment: not a straight line");
return null;
} }
} else if (command.startsWith(ADD_SWITCH)) { } else if (command.startsWith(ADD_SWITCH)) {
String arguments = command.substring(ADD_SWITCH.length()); String arguments = command.substring(ADD_SWITCH.length());
Matcher matcher = ADD_SWITCH_ARGUMENTS.matcher(arguments); Matcher matcher = ADD_SWITCH_ARGUMENTS.matcher(arguments);
if (!matcher.matches()) { if (!matcher.matches()) {
Terminal.printError("invalid add switch argument syntax"); throw new InvalidInputException("invalid add switch argument syntax");
return null;
} }
Vector2D start = Vector2D.parse(matcher.group(1)); Vector2D start = Vector2D.parse(matcher.group(1));
Vector2D end1 = Vector2D.parse(matcher.group(2)); Vector2D end1 = Vector2D.parse(matcher.group(2));
@ -95,23 +91,20 @@ public class CommandFactory {
} else if (command.startsWith(DELETE_TRACK)) { } else if (command.startsWith(DELETE_TRACK)) {
String argument = command.substring(DELETE_TRACK.length()); String argument = command.substring(DELETE_TRACK.length());
if (!argument.matches(POSITIVE_NUMBER)) { if (!argument.matches(POSITIVE_NUMBER)) {
Terminal.printError("invalid/missing delete track argument"); throw new InvalidInputException("invalid/missing delete track argument");
return null;
} }
int id = Integer.parseInt(argument.substring(1)); int id = Integer.parseInt(argument.substring(1));
return new DeleteTrack(id); return new DeleteTrack(id);
} else if (command.startsWith(LIST_TRACKS)) { } else if (command.startsWith(LIST_TRACKS)) {
if (command.length() > LIST_TRACKS.length()) { if (command.length() > LIST_TRACKS.length()) {
Terminal.printError("too many arguments for list tracks"); throw new InvalidInputException("too many arguments for list tracks");
return null;
} }
return new ListTracks(); return new ListTracks();
} else if (command.startsWith(SET_SWITCH)) { } else if (command.startsWith(SET_SWITCH)) {
String arguments = command.substring(SET_SWITCH.length()); String arguments = command.substring(SET_SWITCH.length());
Matcher matcher = SET_SWITCH_ARGUMENTS.matcher(arguments); Matcher matcher = SET_SWITCH_ARGUMENTS.matcher(arguments);
if (!matcher.matches()) { if (!matcher.matches()) {
Terminal.printError("invalid set switch argument syntax"); throw new InvalidInputException("invalid set switch argument syntax");
return null;
} }
int id = Integer.parseInt(matcher.group(1)); int id = Integer.parseInt(matcher.group(1));
Vector2D point = Vector2D.parse(matcher.group(2)); Vector2D point = Vector2D.parse(matcher.group(2));
@ -120,32 +113,31 @@ public class CommandFactory {
String arguments = command.substring(CREATE_ENGINE.length()); String arguments = command.substring(CREATE_ENGINE.length());
Matcher matcher = CREATE_ENGINE_ARGUMENTS.matcher(arguments); Matcher matcher = CREATE_ENGINE_ARGUMENTS.matcher(arguments);
if (!matcher.matches()) { if (!matcher.matches()) {
Terminal.printError("invalid create engine argument syntax"); throw new InvalidInputException("invalid create engine argument syntax");
return null;
} }
EngineType type = EngineType.parse(matcher.group(1)); EngineType type = EngineType.parse(matcher.group(1));
String series = matcher.group(2); String series = matcher.group(2);
if (series.startsWith("W")) { if (series.equals("W")) {
Terminal.printError("invalid engine class/series"); throw new InvalidInputException("invalid engine class/series");
return null;
} }
String name = matcher.group(3); String name = matcher.group(3);
int length = Integer.parseInt(matcher.group(4)); int length = Integer.parseInt(matcher.group(4));
if (length < 1) {
throw new InvalidInputException("engine has to be positive length");
}
boolean couplingFront = Boolean.parseBoolean(matcher.group(5)); boolean couplingFront = Boolean.parseBoolean(matcher.group(5));
boolean couplingBack = Boolean.parseBoolean(matcher.group(6)); boolean couplingBack = Boolean.parseBoolean(matcher.group(6));
return new CreateEngine(type, series, name, length, couplingFront, couplingBack); return new CreateEngine(type, series, name, length, couplingFront, couplingBack);
} else if (command.startsWith(LIST_ENGINES)) { } else if (command.startsWith(LIST_ENGINES)) {
if (command.length() > LIST_ENGINES.length()) { if (command.length() > LIST_ENGINES.length()) {
Terminal.printError("too many list engines arguments"); throw new InvalidInputException("too many list engines arguments");
return null;
} }
return new ListEngines(); return new ListEngines();
} else if (command.startsWith(CREATE_COACH)) { } else if (command.startsWith(CREATE_COACH)) {
String arguments = command.substring(CREATE_COACH.length()); String arguments = command.substring(CREATE_COACH.length());
Matcher matcher = CREATE_COACH_ARGUMENTS.matcher(arguments); Matcher matcher = CREATE_COACH_ARGUMENTS.matcher(arguments);
if (!matcher.matches()) { if (!matcher.matches()) {
Terminal.printError("invalid create coach arguments"); throw new InvalidInputException("invalid create coach arguments");
return null;
} }
CoachType type = CoachType.parse(matcher.group(1)); CoachType type = CoachType.parse(matcher.group(1));
int length = Integer.parseInt(matcher.group(2)); int length = Integer.parseInt(matcher.group(2));
@ -154,21 +146,18 @@ public class CommandFactory {
return new CreateCoach(type, length, couplingFront, couplingBack); return new CreateCoach(type, length, couplingFront, couplingBack);
} else if (command.startsWith(LIST_COACHES)) { } else if (command.startsWith(LIST_COACHES)) {
if (command.length() > LIST_COACHES.length()) { if (command.length() > LIST_COACHES.length()) {
Terminal.printError("too many list coaches arguments"); throw new InvalidInputException("too many list coaches arguments");
return null;
} }
return new ListCoaches(); return new ListCoaches();
} else if (command.startsWith(CREATE_TRAIN_SET)) { } else if (command.startsWith(CREATE_TRAIN_SET)) {
String arguments = command.substring(CREATE_TRAIN_SET.length()); String arguments = command.substring(CREATE_TRAIN_SET.length());
Matcher matcher = CREATE_TRAIN_SET_ARGUMENTS.matcher(arguments); Matcher matcher = CREATE_TRAIN_SET_ARGUMENTS.matcher(arguments);
if (!matcher.matches()) { if (!matcher.matches()) {
Terminal.printError("invalid create train-set arguments"); throw new InvalidInputException("invalid create train-set arguments");
return null;
} }
String series = matcher.group(1); String series = matcher.group(1);
if (series.startsWith("W")) { if (series.equals("W")) {
Terminal.printError("invalid train-set class/series"); throw new InvalidInputException("invalid train-set class/series");
return null;
} }
String name = matcher.group(2); String name = matcher.group(2);
int length = Integer.parseInt(matcher.group(3)); int length = Integer.parseInt(matcher.group(3));
@ -177,16 +166,14 @@ public class CommandFactory {
return new CreateTrainSet(series, name, length, couplingFront, couplingBack); return new CreateTrainSet(series, name, length, couplingFront, couplingBack);
} else if (command.startsWith(LIST_TRAIN_SETS)) { } else if (command.startsWith(LIST_TRAIN_SETS)) {
if (command.length() > LIST_TRAIN_SETS.length()) { if (command.length() > LIST_TRAIN_SETS.length()) {
Terminal.printError("too many list train-sets arguments"); throw new InvalidInputException("too many list train-sets arguments");
return null;
} }
return new ListTrainSets(); return new ListTrainSets();
} else if (command.startsWith(DELETE_ROLLING_STOCK)) { } else if (command.startsWith(DELETE_ROLLING_STOCK)) {
String argument = command.substring(DELETE_ROLLING_STOCK.length()); String argument = command.substring(DELETE_ROLLING_STOCK.length());
Matcher matcher = DELETE_ROLLING_STOCK_ARGUMENT.matcher(argument); Matcher matcher = DELETE_ROLLING_STOCK_ARGUMENT.matcher(argument);
if (!matcher.matches()) { if (!matcher.matches()) {
Terminal.printLine("invalid delete rolling stock argument"); throw new InvalidInputException("invalid delete rolling stock argument");
return null;
} }
String id = matcher.group(1); String id = matcher.group(1);
return new DeleteRollingStock(id); return new DeleteRollingStock(id);
@ -194,8 +181,7 @@ public class CommandFactory {
String arguments = command.substring(ADD_TRAIN.length()); String arguments = command.substring(ADD_TRAIN.length());
Matcher matcher = ADD_TRAIN_ARGUMENTS.matcher(arguments); Matcher matcher = ADD_TRAIN_ARGUMENTS.matcher(arguments);
if (!matcher.matches()) { if (!matcher.matches()) {
Terminal.printError("invalid add train arguments"); throw new InvalidInputException("invalid add train arguments");
return null;
} }
int trainId = Integer.parseInt(matcher.group(1)); int trainId = Integer.parseInt(matcher.group(1));
String rollingStockId = matcher.group(2); String rollingStockId = matcher.group(2);
@ -203,22 +189,19 @@ public class CommandFactory {
} else if (command.startsWith(DELETE_TRAIN)) { } else if (command.startsWith(DELETE_TRAIN)) {
String argument = command.substring(DELETE_TRAIN.length()); String argument = command.substring(DELETE_TRAIN.length());
if (!argument.matches(POSITIVE_NUMBER)) { if (!argument.matches(POSITIVE_NUMBER)) {
Terminal.printError("invalid delete train argument"); throw new InvalidInputException("invalid delete train argument");
return null;
} }
int id = Integer.parseInt(argument.substring(1)); int id = Integer.parseInt(argument.substring(1));
return new DeleteTrain(id); return new DeleteTrain(id);
} else if (command.startsWith(LIST_TRAINS)) { } else if (command.startsWith(LIST_TRAINS)) {
if (command.length() > LIST_TRAINS.length()) { if (command.length() > LIST_TRAINS.length()) {
Terminal.printError("too many list trains arguments"); throw new InvalidInputException("too many list trains arguments");
return null;
} }
return new ListTrains(); return new ListTrains();
} else if (command.startsWith(SHOW_TRAIN)) { } else if (command.startsWith(SHOW_TRAIN)) {
String argument = command.substring(SHOW_TRAIN.length()); String argument = command.substring(SHOW_TRAIN.length());
if (!argument.matches(POSITIVE_NUMBER)) { if (!argument.matches(POSITIVE_NUMBER)) {
Terminal.printError("invalid show train argument"); throw new InvalidInputException("invalid show train argument");
return null;
} }
int id = Integer.parseInt(argument.substring(1)); int id = Integer.parseInt(argument.substring(1));
return new ShowTrain(id); return new ShowTrain(id);
@ -226,29 +209,25 @@ public class CommandFactory {
String arguments = command.substring(PUT_TRAIN.length()); String arguments = command.substring(PUT_TRAIN.length());
Matcher matcher = PUT_TRAIN_ARGUMENTS.matcher(arguments); Matcher matcher = PUT_TRAIN_ARGUMENTS.matcher(arguments);
if (!matcher.matches()) { if (!matcher.matches()) {
Terminal.printError("invalid put train arguments"); throw new InvalidInputException("invalid put train arguments");
return null;
} }
int id = Integer.parseInt(matcher.group(1)); int id = Integer.parseInt(matcher.group(1));
Vector2D point = Vector2D.parse(matcher.group(2)); Vector2D point = Vector2D.parse(matcher.group(2));
int x = Integer.parseInt(matcher.group(3)); int x = Integer.parseInt(matcher.group(3));
int y = Integer.parseInt(matcher.group(4)); int y = Integer.parseInt(matcher.group(4));
if (x != 0 && y != 0) { if (x != 0 && y != 0) {
Terminal.printError("invalid train direction"); throw new InvalidInputException("invalid train direction");
return null;
} }
return new PutTrain(id, point, x, y); return new PutTrain(id, point, x, y);
} else if (command.startsWith(STEP)) { } else if (command.startsWith(STEP)) {
String argument = command.substring(STEP.length()); String argument = command.substring(STEP.length());
if (!argument.matches(" [+-]?\\d+")) { if (!argument.matches(" [+-]?\\d+")) {
Terminal.printError("invalid step argument"); throw new InvalidInputException("invalid step argument");
return null;
} }
short speed = Short.parseShort(argument.substring(1)); short speed = Short.parseShort(argument.substring(1));
return new Step(speed); return new Step(speed);
} else { } else {
Terminal.printError("unknown command"); throw new InvalidInputException("unknown command");
return null;
} }
} }
} }