diff --git a/src/edu/kit/informatik/model/DieselEngine.java b/src/edu/kit/informatik/model/DieselEngine.java index 8b2386e..5a8f4e5 100644 --- a/src/edu/kit/informatik/model/DieselEngine.java +++ b/src/edu/kit/informatik/model/DieselEngine.java @@ -29,16 +29,13 @@ public class DieselEngine extends Engine { */ public DieselEngine(final String series, final String name, final int length, final boolean couplingFront, final boolean couplingBack) { - super.name = name; - super.series = series; - super.length = length; - super.couplingFront = couplingFront; - super.couplingBack = couplingBack; + super(series, name, length, couplingFront, couplingBack); } @Override 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 diff --git a/src/edu/kit/informatik/model/ElectricalEngine.java b/src/edu/kit/informatik/model/ElectricalEngine.java index 3d60416..4040332 100644 --- a/src/edu/kit/informatik/model/ElectricalEngine.java +++ b/src/edu/kit/informatik/model/ElectricalEngine.java @@ -31,16 +31,13 @@ public class ElectricalEngine extends Engine { */ public ElectricalEngine(final String series, final String name, final int length, final boolean couplingFront, final boolean couplingBack) { - super.name = name; - super.series = series; - super.length = length; - super.couplingFront = couplingFront; - super.couplingBack = couplingBack; + super(series, name, length, couplingFront, couplingBack); } @Override 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 diff --git a/src/edu/kit/informatik/model/Engine.java b/src/edu/kit/informatik/model/Engine.java index af2f618..8cdb6d7 100644 --- a/src/edu/kit/informatik/model/Engine.java +++ b/src/edu/kit/informatik/model/Engine.java @@ -10,11 +10,18 @@ public abstract class Engine extends RollingStock { /** * Series/class of this engine. */ - protected String series; + private String series; /** * 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 @@ -23,6 +30,13 @@ public abstract class Engine extends RollingStock { return name; } + /** + * @return the series of this engine + */ + public String getSeries() { + return series; + } + @Override public String getIdentifier() { return String.format("%s-%s", series, getName()); diff --git a/src/edu/kit/informatik/model/Rail.java b/src/edu/kit/informatik/model/Rail.java index 2a31bb0..5c10338 100644 --- a/src/edu/kit/informatik/model/Rail.java +++ b/src/edu/kit/informatik/model/Rail.java @@ -10,7 +10,7 @@ public abstract class Rail { /** * Unique identifier of this rail. */ - protected final int id; + private final int id; /** * Initialize a new rail with the specified identifier. diff --git a/src/edu/kit/informatik/model/RollingStock.java b/src/edu/kit/informatik/model/RollingStock.java index 48bbd96..7963532 100644 --- a/src/edu/kit/informatik/model/RollingStock.java +++ b/src/edu/kit/informatik/model/RollingStock.java @@ -10,15 +10,21 @@ public abstract class RollingStock { /** * Length of this rolling stock. */ - protected int length; + private int length; /** * Whether this rolling stock has a front coupling. */ - protected boolean couplingFront; + private boolean couplingFront; /** * 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 diff --git a/src/edu/kit/informatik/model/SteamEngine.java b/src/edu/kit/informatik/model/SteamEngine.java index 2ad4147..d426261 100644 --- a/src/edu/kit/informatik/model/SteamEngine.java +++ b/src/edu/kit/informatik/model/SteamEngine.java @@ -29,16 +29,13 @@ public class SteamEngine extends Engine { */ public SteamEngine(final String series, final String name, final int length, final boolean couplingFront, final boolean couplingBack) { - super.name = name; - super.series = series; - super.length = length; - super.couplingFront = couplingFront; - super.couplingBack = couplingBack; + super(series, name, length, couplingFront, couplingBack); } @Override 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 diff --git a/src/edu/kit/informatik/ui/command/CommandFactory.java b/src/edu/kit/informatik/ui/command/CommandFactory.java index 5b4bf4d..704874b 100644 --- a/src/edu/kit/informatik/ui/command/CommandFactory.java +++ b/src/edu/kit/informatik/ui/command/CommandFactory.java @@ -1,6 +1,5 @@ package edu.kit.informatik.ui.command; -import edu.kit.informatik.Terminal; import edu.kit.informatik.model.Vector2D; import edu.kit.informatik.ui.CoachType; import edu.kit.informatik.ui.EngineType; @@ -65,23 +64,20 @@ public class CommandFactory { String arguments = command.substring(ADD_TRACK.length()); Matcher matcher = ADD_TRACK_ARGUMENTS.matcher(arguments); if (!matcher.matches()) { - Terminal.printError("invalid add track argument syntax"); - return null; + throw new InvalidInputException("invalid add track argument syntax"); } Vector2D start = Vector2D.parse(matcher.group(1)); Vector2D end = Vector2D.parse(matcher.group(2)); if (start.getX() == end.getX() || start.getY() == end.getY()) { return new AddTrack(start, end); } else { - Terminal.printError("invalid track segment: not a straight line"); - return null; + throw new InvalidInputException("invalid track segment: not a straight line"); } } else if (command.startsWith(ADD_SWITCH)) { String arguments = command.substring(ADD_SWITCH.length()); Matcher matcher = ADD_SWITCH_ARGUMENTS.matcher(arguments); if (!matcher.matches()) { - Terminal.printError("invalid add switch argument syntax"); - return null; + throw new InvalidInputException("invalid add switch argument syntax"); } Vector2D start = Vector2D.parse(matcher.group(1)); Vector2D end1 = Vector2D.parse(matcher.group(2)); @@ -95,23 +91,20 @@ public class CommandFactory { } else if (command.startsWith(DELETE_TRACK)) { String argument = command.substring(DELETE_TRACK.length()); if (!argument.matches(POSITIVE_NUMBER)) { - Terminal.printError("invalid/missing delete track argument"); - return null; + throw new InvalidInputException("invalid/missing delete track argument"); } int id = Integer.parseInt(argument.substring(1)); return new DeleteTrack(id); } else if (command.startsWith(LIST_TRACKS)) { if (command.length() > LIST_TRACKS.length()) { - Terminal.printError("too many arguments for list tracks"); - return null; + throw new InvalidInputException("too many arguments for list tracks"); } return new ListTracks(); } else if (command.startsWith(SET_SWITCH)) { String arguments = command.substring(SET_SWITCH.length()); Matcher matcher = SET_SWITCH_ARGUMENTS.matcher(arguments); if (!matcher.matches()) { - Terminal.printError("invalid set switch argument syntax"); - return null; + throw new InvalidInputException("invalid set switch argument syntax"); } int id = Integer.parseInt(matcher.group(1)); Vector2D point = Vector2D.parse(matcher.group(2)); @@ -120,32 +113,31 @@ public class CommandFactory { String arguments = command.substring(CREATE_ENGINE.length()); Matcher matcher = CREATE_ENGINE_ARGUMENTS.matcher(arguments); if (!matcher.matches()) { - Terminal.printError("invalid create engine argument syntax"); - return null; + throw new InvalidInputException("invalid create engine argument syntax"); } EngineType type = EngineType.parse(matcher.group(1)); String series = matcher.group(2); - if (series.startsWith("W")) { - Terminal.printError("invalid engine class/series"); - return null; + if (series.equals("W")) { + throw new InvalidInputException("invalid engine class/series"); } String name = matcher.group(3); 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 couplingBack = Boolean.parseBoolean(matcher.group(6)); return new CreateEngine(type, series, name, length, couplingFront, couplingBack); } else if (command.startsWith(LIST_ENGINES)) { if (command.length() > LIST_ENGINES.length()) { - Terminal.printError("too many list engines arguments"); - return null; + throw new InvalidInputException("too many list engines arguments"); } return new ListEngines(); } else if (command.startsWith(CREATE_COACH)) { String arguments = command.substring(CREATE_COACH.length()); Matcher matcher = CREATE_COACH_ARGUMENTS.matcher(arguments); if (!matcher.matches()) { - Terminal.printError("invalid create coach arguments"); - return null; + throw new InvalidInputException("invalid create coach arguments"); } CoachType type = CoachType.parse(matcher.group(1)); int length = Integer.parseInt(matcher.group(2)); @@ -154,21 +146,18 @@ public class CommandFactory { return new CreateCoach(type, length, couplingFront, couplingBack); } else if (command.startsWith(LIST_COACHES)) { if (command.length() > LIST_COACHES.length()) { - Terminal.printError("too many list coaches arguments"); - return null; + throw new InvalidInputException("too many list coaches arguments"); } return new ListCoaches(); } else if (command.startsWith(CREATE_TRAIN_SET)) { String arguments = command.substring(CREATE_TRAIN_SET.length()); Matcher matcher = CREATE_TRAIN_SET_ARGUMENTS.matcher(arguments); if (!matcher.matches()) { - Terminal.printError("invalid create train-set arguments"); - return null; + throw new InvalidInputException("invalid create train-set arguments"); } String series = matcher.group(1); - if (series.startsWith("W")) { - Terminal.printError("invalid train-set class/series"); - return null; + if (series.equals("W")) { + throw new InvalidInputException("invalid train-set class/series"); } String name = matcher.group(2); int length = Integer.parseInt(matcher.group(3)); @@ -177,16 +166,14 @@ public class CommandFactory { return new CreateTrainSet(series, name, length, couplingFront, couplingBack); } else if (command.startsWith(LIST_TRAIN_SETS)) { if (command.length() > LIST_TRAIN_SETS.length()) { - Terminal.printError("too many list train-sets arguments"); - return null; + throw new InvalidInputException("too many list train-sets arguments"); } return new ListTrainSets(); } else if (command.startsWith(DELETE_ROLLING_STOCK)) { String argument = command.substring(DELETE_ROLLING_STOCK.length()); Matcher matcher = DELETE_ROLLING_STOCK_ARGUMENT.matcher(argument); if (!matcher.matches()) { - Terminal.printLine("invalid delete rolling stock argument"); - return null; + throw new InvalidInputException("invalid delete rolling stock argument"); } String id = matcher.group(1); return new DeleteRollingStock(id); @@ -194,8 +181,7 @@ public class CommandFactory { String arguments = command.substring(ADD_TRAIN.length()); Matcher matcher = ADD_TRAIN_ARGUMENTS.matcher(arguments); if (!matcher.matches()) { - Terminal.printError("invalid add train arguments"); - return null; + throw new InvalidInputException("invalid add train arguments"); } int trainId = Integer.parseInt(matcher.group(1)); String rollingStockId = matcher.group(2); @@ -203,22 +189,19 @@ public class CommandFactory { } else if (command.startsWith(DELETE_TRAIN)) { String argument = command.substring(DELETE_TRAIN.length()); if (!argument.matches(POSITIVE_NUMBER)) { - Terminal.printError("invalid delete train argument"); - return null; + throw new InvalidInputException("invalid delete train argument"); } int id = Integer.parseInt(argument.substring(1)); return new DeleteTrain(id); } else if (command.startsWith(LIST_TRAINS)) { if (command.length() > LIST_TRAINS.length()) { - Terminal.printError("too many list trains arguments"); - return null; + throw new InvalidInputException("too many list trains arguments"); } return new ListTrains(); } else if (command.startsWith(SHOW_TRAIN)) { String argument = command.substring(SHOW_TRAIN.length()); if (!argument.matches(POSITIVE_NUMBER)) { - Terminal.printError("invalid show train argument"); - return null; + throw new InvalidInputException("invalid show train argument"); } int id = Integer.parseInt(argument.substring(1)); return new ShowTrain(id); @@ -226,29 +209,25 @@ public class CommandFactory { String arguments = command.substring(PUT_TRAIN.length()); Matcher matcher = PUT_TRAIN_ARGUMENTS.matcher(arguments); if (!matcher.matches()) { - Terminal.printError("invalid put train arguments"); - return null; + throw new InvalidInputException("invalid put train arguments"); } int id = Integer.parseInt(matcher.group(1)); Vector2D point = Vector2D.parse(matcher.group(2)); int x = Integer.parseInt(matcher.group(3)); int y = Integer.parseInt(matcher.group(4)); if (x != 0 && y != 0) { - Terminal.printError("invalid train direction"); - return null; + throw new InvalidInputException("invalid train direction"); } return new PutTrain(id, point, x, y); } else if (command.startsWith(STEP)) { String argument = command.substring(STEP.length()); if (!argument.matches(" [+-]?\\d+")) { - Terminal.printError("invalid step argument"); - return null; + throw new InvalidInputException("invalid step argument"); } short speed = Short.parseShort(argument.substring(1)); return new Step(speed); } else { - Terminal.printError("unknown command"); - return null; + throw new InvalidInputException("unknown command"); } } }