From 0a640cbabef254b85472184bdaaf7efd5af61a8d Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Sat, 7 Mar 2020 15:28:13 +0100 Subject: [PATCH] Checkstyle --- src/edu/kit/informatik/model/Coach.java | 4 --- .../model/ModelRailwaySimulation.java | 21 +++-------- src/edu/kit/informatik/ui/CoachType.java | 35 ++++++++++++++++++- src/edu/kit/informatik/ui/EngineType.java | 35 +++++++++++++++++++ .../informatik/ui/command/CreateCoach.java | 2 +- .../informatik/ui/command/CreateEngine.java | 21 ++--------- 6 files changed, 78 insertions(+), 40 deletions(-) diff --git a/src/edu/kit/informatik/model/Coach.java b/src/edu/kit/informatik/model/Coach.java index 8064483..7618a76 100644 --- a/src/edu/kit/informatik/model/Coach.java +++ b/src/edu/kit/informatik/model/Coach.java @@ -21,10 +21,6 @@ public abstract class Coach extends RollingStock { * Pattern to match a single coach identifier. */ public static final Pattern IDENTIFIER_PATTERN = Pattern.compile(IDENTIFIER_PREFIX + NUMBER); - /** - * Pattern to match coach type (passenger, freight, or special). - */ - public static final String COACH_TYPE = "passenger|freight|special"; /** * The (unique) identifier of this coach. diff --git a/src/edu/kit/informatik/model/ModelRailwaySimulation.java b/src/edu/kit/informatik/model/ModelRailwaySimulation.java index 61f5a3a..ad90637 100644 --- a/src/edu/kit/informatik/model/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/model/ModelRailwaySimulation.java @@ -149,19 +149,7 @@ public class ModelRailwaySimulation { if (id < 0) { return -1; } - switch (coachType) { - case PASSENGER: - coaches.put(id, new PassengerCoach(id, length, couplingFront, couplingBack)); - break; - case FREIGHT: - coaches.put(id, new FreightCoach(id, length, couplingFront, couplingBack)); - break; - case SPECIAL: - coaches.put(id, new SpecialCoach(id, length, couplingFront, couplingBack)); - break; - default: - throw new IllegalArgumentException("coach type is null"); - } + coaches.put(id, coachType.createCoach(id, length, couplingFront, couplingBack)); return id; } @@ -223,7 +211,7 @@ public class ModelRailwaySimulation { } /** - * Delete a rolling stock. + * Delete a rolling stock. Will not return rolling stock used in a train, you have to delete the train first! * * @param id identifier of the rolling stock to remove * @return whether the rolling was successfully removed @@ -231,11 +219,12 @@ public class ModelRailwaySimulation { public boolean deleteRollingStock(String id) { final Optional rollingStock = getRollingStock(id); if (!rollingStock.isPresent()) { - return false; + return false; // can not remove imaginary rolling stock } if (trainManager.getTrainContainingRollingStock(rollingStock.get()).isPresent()) { return false; // can not delete rolling stock in use } + // remove the rolling stock from one of the collections return engines.remove(rollingStock.get()) || trainSets.remove(rollingStock.get()) || coaches.values().remove(rollingStock.get()); @@ -247,7 +236,7 @@ public class ModelRailwaySimulation { * @param id identifier of the rolling stock to find * @return the specified rolling stock */ - public Optional getRollingStock(String id) { + private Optional getRollingStock(String id) { if (Coach.IDENTIFIER_PATTERN.matcher(id).matches()) { final int coachId = Integer.parseInt(id.substring(1)); return Optional.ofNullable(coaches.get(coachId)); diff --git a/src/edu/kit/informatik/ui/CoachType.java b/src/edu/kit/informatik/ui/CoachType.java index 9c1d99e..f2184ae 100644 --- a/src/edu/kit/informatik/ui/CoachType.java +++ b/src/edu/kit/informatik/ui/CoachType.java @@ -1,5 +1,10 @@ package edu.kit.informatik.ui; +import edu.kit.informatik.model.Coach; +import edu.kit.informatik.model.FreightCoach; +import edu.kit.informatik.model.PassengerCoach; +import edu.kit.informatik.model.SpecialCoach; + /** * Type of a coach. * @@ -21,8 +26,36 @@ public enum CoachType { SPECIAL; /** - * Parse the textual representation of a coach type into the correct enum value. + * Regex to match one coach type. + */ + public static final String COACH_TYPE = "passenger|freight|special"; + + /** + * Create a coach of this type with the specified parameters. * + * @param id coach identifier + * @param length coach length + * @param couplingFront whether the coach should have a front coupling + * @param couplingBack whether the coach should have a back coupling + * @return new coach + * @throws InvalidInputException on invalid input (e.g. zero-sized coach) + */ + public Coach createCoach(int id, int length, boolean couplingFront, boolean couplingBack) + throws InvalidInputException { + switch (this) { + case PASSENGER: + return new PassengerCoach(id, length, couplingFront, couplingBack); + case FREIGHT: + return new FreightCoach(id, length, couplingFront, couplingBack); + case SPECIAL: + return new SpecialCoach(id, length, couplingFront, couplingBack); + default: + throw new IllegalArgumentException("this is null"); + } + } + + /** + * Parse the textual representation of a coach type into the correct enum value. * @param value coach type as text * @return coach type as enum, or null if invalid */ diff --git a/src/edu/kit/informatik/ui/EngineType.java b/src/edu/kit/informatik/ui/EngineType.java index 54ff399..4a103b2 100644 --- a/src/edu/kit/informatik/ui/EngineType.java +++ b/src/edu/kit/informatik/ui/EngineType.java @@ -1,5 +1,10 @@ package edu.kit.informatik.ui; +import edu.kit.informatik.model.DieselEngine; +import edu.kit.informatik.model.ElectricalEngine; +import edu.kit.informatik.model.Engine; +import edu.kit.informatik.model.SteamEngine; + /** * Type of locomotive. Can be either diesel, steam or electrical. * @@ -20,6 +25,36 @@ public enum EngineType { */ ELECTRICAL; + /** + * Regex to match one engine type. + */ + public static final String ENGINE_TYPE = "diesel|steam|electrical"; + + /** + * Create a new engine of this type and the specified parameters. + + * @param series engine series/class + * @param name engine name + * @param length engine length + * @param couplingFront whether the engine should have a front coupling + * @param couplingBack whether the engine should have a back coupling + * @return new engine + * @throws InvalidInputException on invalid input (e.g. zero-sized engine) + */ + public Engine createEngine(String series, String name, int length, boolean couplingFront, boolean couplingBack) + throws InvalidInputException { + switch (this) { + case ELECTRICAL: + return new ElectricalEngine(series, name, length, couplingFront, couplingBack); + case STEAM: + return new SteamEngine(series, name, length, couplingFront, couplingBack); + case DIESEL: + return new DieselEngine(series, name, length, couplingFront, couplingBack); + default: + throw new IllegalStateException("this is null"); + } + } + /** * Parse the textual representation of a engine type into the correct enum value. * diff --git a/src/edu/kit/informatik/ui/command/CreateCoach.java b/src/edu/kit/informatik/ui/command/CreateCoach.java index 4f45645..837a41c 100644 --- a/src/edu/kit/informatik/ui/command/CreateCoach.java +++ b/src/edu/kit/informatik/ui/command/CreateCoach.java @@ -8,7 +8,7 @@ import edu.kit.informatik.ui.InvalidInputException; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static edu.kit.informatik.model.Coach.COACH_TYPE; +import static edu.kit.informatik.ui.CoachType.COACH_TYPE; import static edu.kit.informatik.ui.command.CommandFactory.BOOL; import static edu.kit.informatik.ui.command.CommandFactory.CREATE_COACH; import static edu.kit.informatik.ui.command.CommandFactory.NUMBER; diff --git a/src/edu/kit/informatik/ui/command/CreateEngine.java b/src/edu/kit/informatik/ui/command/CreateEngine.java index 9a43ae8..86854b8 100644 --- a/src/edu/kit/informatik/ui/command/CreateEngine.java +++ b/src/edu/kit/informatik/ui/command/CreateEngine.java @@ -2,17 +2,15 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.Terminal; import edu.kit.informatik.model.Coach; -import edu.kit.informatik.model.DieselEngine; -import edu.kit.informatik.model.ElectricalEngine; import edu.kit.informatik.model.Engine; import edu.kit.informatik.model.ModelRailwaySimulation; -import edu.kit.informatik.model.SteamEngine; import edu.kit.informatik.ui.EngineType; import edu.kit.informatik.ui.InvalidInputException; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static edu.kit.informatik.ui.EngineType.ENGINE_TYPE; import static edu.kit.informatik.ui.command.CommandFactory.ALPHANUMERIC_WORD; import static edu.kit.informatik.ui.command.CommandFactory.BOOL; import static edu.kit.informatik.ui.command.CommandFactory.CREATE_ENGINE; @@ -26,7 +24,7 @@ import static edu.kit.informatik.ui.command.CommandFactory.NUMBER; */ public class CreateEngine extends Command { private static final Pattern CREATE_ENGINE_ARGUMENTS - = Pattern.compile(" (electrical|diesel|steam) (" + ALPHANUMERIC_WORD + ") (" + ALPHANUMERIC_WORD + ") (" + = Pattern.compile(" (" + ENGINE_TYPE + ") (" + ALPHANUMERIC_WORD + ") (" + ALPHANUMERIC_WORD + ") (" + NUMBER + ") (" + BOOL + ") (" + BOOL + ")"); /** @@ -59,20 +57,7 @@ public class CreateEngine extends Command { if (type == null) { throw new IllegalStateException("command not initialized"); } - final Engine engine; - switch (type) { - case ELECTRICAL: - engine = new ElectricalEngine(series, name, length, couplingFront, couplingBack); - break; - case STEAM: - engine = new SteamEngine(series, name, length, couplingFront, couplingBack); - break; - case DIESEL: - engine = new DieselEngine(series, name, length, couplingFront, couplingBack); - break; - default: - throw new IllegalStateException("command not initialized"); - } + final Engine engine = type.createEngine(series, name, length, couplingFront, couplingBack); simulation.createEngine(engine); Terminal.printLine(engine.getIdentifier()); }