diff --git a/src/edu/kit/informatik/CoachType.java b/src/edu/kit/informatik/CoachType.java index caccaf9..317d75e 100644 --- a/src/edu/kit/informatik/CoachType.java +++ b/src/edu/kit/informatik/CoachType.java @@ -5,6 +5,19 @@ public enum CoachType { FREIGHT, SPECIAL; + public static CoachType parse(String value) { + switch (value) { + case "passenger": + return PASSENGER; + case "freight": + return FREIGHT; + case "special": + return SPECIAL; + default: + return null; + } + } + @Override public String toString() { switch (this) { diff --git a/src/edu/kit/informatik/EngineType.java b/src/edu/kit/informatik/EngineType.java new file mode 100644 index 0000000..9315555 --- /dev/null +++ b/src/edu/kit/informatik/EngineType.java @@ -0,0 +1,32 @@ +package edu.kit.informatik; + +/** + * Type of locomotive. Can be either diesel, steam or electrical. + */ +public enum EngineType { + /** + * Diesel engine. + */ + DIESEL, + /** + * Steam engine. + */ + STEAM, + /** + * Electrical engine. + */ + ELECTRICAL; + + public static EngineType parse(String value) { + switch (value) { + case "diesel": + return DIESEL; + case "steam": + return STEAM; + case "electrical": + return ELECTRICAL; + default: + return null; + } + } +} diff --git a/src/edu/kit/informatik/command/CommandFactory.java b/src/edu/kit/informatik/command/CommandFactory.java index 3df7f1c..98cdc80 100644 --- a/src/edu/kit/informatik/command/CommandFactory.java +++ b/src/edu/kit/informatik/command/CommandFactory.java @@ -1,6 +1,7 @@ package edu.kit.informatik.command; import edu.kit.informatik.CoachType; +import edu.kit.informatik.EngineType; import edu.kit.informatik.Point; import edu.kit.informatik.Terminal; @@ -120,7 +121,7 @@ public class CommandFactory { Terminal.printError("invalid create engine argument syntax"); return null; } - String type = matcher.group(1); + EngineType type = EngineType.parse(matcher.group(1)); String series = matcher.group(2); if (series.startsWith("W")) { Terminal.printError("invalid engine class/series"); @@ -144,20 +145,11 @@ public class CommandFactory { Terminal.printError("invalid create coach arguments"); return null; } + CoachType type = CoachType.parse(matcher.group(1)); int length = Integer.parseInt(matcher.group(2)); boolean couplingFront = Boolean.parseBoolean(matcher.group(3)); boolean couplingBack = Boolean.parseBoolean(matcher.group(4)); - switch (matcher.group(1)) { - case "passenger": - return new CreateCoach(CoachType.PASSENGER, length, couplingFront, couplingBack); - case "freight": - return new CreateCoach(CoachType.FREIGHT, length, couplingFront, couplingBack); - case "special": - return new CreateCoach(CoachType.SPECIAL, length, couplingFront, couplingBack); - default: - throw new IllegalStateException("regex matched unmatchable argument"); - - } + 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"); diff --git a/src/edu/kit/informatik/command/CreateEngine.java b/src/edu/kit/informatik/command/CreateEngine.java index e02b858..0d410dd 100644 --- a/src/edu/kit/informatik/command/CreateEngine.java +++ b/src/edu/kit/informatik/command/CreateEngine.java @@ -9,7 +9,7 @@ import edu.kit.informatik.*; * @version 1.0 */ public class CreateEngine extends Command { - private final String type; + private final EngineType type; private final String series; private final String name; private final int length; @@ -25,7 +25,7 @@ public class CreateEngine extends Command { * @param couplingFront whether the new engine should have a front coupling * @param couplingBack whether the new engine should have a back coupling */ - public CreateEngine(final String type, final String series, final String name, final int length, final boolean couplingFront, final boolean couplingBack) { + public CreateEngine(final EngineType type, final String series, final String name, final int length, final boolean couplingFront, final boolean couplingBack) { this.type = type; this.series = series; this.name = name; @@ -38,18 +38,17 @@ public class CreateEngine extends Command { public void apply(final ModelRailwaySimulation simulation) { final Engine engine; switch (type) { - case "electrical": + case ELECTRICAL: engine = new ElectricalEngine(series, name, length, couplingFront, couplingBack); break; - case "steam": + case STEAM: engine = new SteamEngine(series, name, length, couplingFront, couplingBack); break; - case "diesel": + case DIESEL: engine = new DieselEngine(series, name, length, couplingFront, couplingBack); break; default: - Terminal.printError("invalid engine type"); - return; + throw new IllegalStateException("engine type is null!"); } if (simulation.createEngine(engine)) { Terminal.printLine(engine.getIdentifier());