Checkstyle

This commit is contained in:
Arne Keller 2020-03-07 15:28:13 +01:00
parent 4fd72ebe43
commit 0a640cbabe
6 changed files with 78 additions and 40 deletions

View File

@ -21,10 +21,6 @@ public abstract class Coach extends RollingStock {
* Pattern to match a single coach identifier. * Pattern to match a single coach identifier.
*/ */
public static final Pattern IDENTIFIER_PATTERN = Pattern.compile(IDENTIFIER_PREFIX + NUMBER); 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. * The (unique) identifier of this coach.

View File

@ -149,19 +149,7 @@ public class ModelRailwaySimulation {
if (id < 0) { if (id < 0) {
return -1; return -1;
} }
switch (coachType) { coaches.put(id, coachType.createCoach(id, length, couplingFront, couplingBack));
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");
}
return id; 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 * @param id identifier of the rolling stock to remove
* @return whether the rolling was successfully removed * @return whether the rolling was successfully removed
@ -231,11 +219,12 @@ public class ModelRailwaySimulation {
public boolean deleteRollingStock(String id) { public boolean deleteRollingStock(String id) {
final Optional<RollingStock> rollingStock = getRollingStock(id); final Optional<RollingStock> rollingStock = getRollingStock(id);
if (!rollingStock.isPresent()) { if (!rollingStock.isPresent()) {
return false; return false; // can not remove imaginary rolling stock
} }
if (trainManager.getTrainContainingRollingStock(rollingStock.get()).isPresent()) { if (trainManager.getTrainContainingRollingStock(rollingStock.get()).isPresent()) {
return false; // can not delete rolling stock in use return false; // can not delete rolling stock in use
} }
// remove the rolling stock from one of the collections
return engines.remove(rollingStock.get()) return engines.remove(rollingStock.get())
|| trainSets.remove(rollingStock.get()) || trainSets.remove(rollingStock.get())
|| coaches.values().remove(rollingStock.get()); || coaches.values().remove(rollingStock.get());
@ -247,7 +236,7 @@ public class ModelRailwaySimulation {
* @param id identifier of the rolling stock to find * @param id identifier of the rolling stock to find
* @return the specified rolling stock * @return the specified rolling stock
*/ */
public Optional<RollingStock> getRollingStock(String id) { private Optional<RollingStock> getRollingStock(String id) {
if (Coach.IDENTIFIER_PATTERN.matcher(id).matches()) { if (Coach.IDENTIFIER_PATTERN.matcher(id).matches()) {
final int coachId = Integer.parseInt(id.substring(1)); final int coachId = Integer.parseInt(id.substring(1));
return Optional.ofNullable(coaches.get(coachId)); return Optional.ofNullable(coaches.get(coachId));

View File

@ -1,5 +1,10 @@
package edu.kit.informatik.ui; 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. * Type of a coach.
* *
@ -21,8 +26,36 @@ public enum CoachType {
SPECIAL; 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 * @param value coach type as text
* @return coach type as enum, or null if invalid * @return coach type as enum, or null if invalid
*/ */

View File

@ -1,5 +1,10 @@
package edu.kit.informatik.ui; 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. * Type of locomotive. Can be either diesel, steam or electrical.
* *
@ -20,6 +25,36 @@ public enum EngineType {
*/ */
ELECTRICAL; 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. * Parse the textual representation of a engine type into the correct enum value.
* *

View File

@ -8,7 +8,7 @@ import edu.kit.informatik.ui.InvalidInputException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; 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.BOOL;
import static edu.kit.informatik.ui.command.CommandFactory.CREATE_COACH; import static edu.kit.informatik.ui.command.CommandFactory.CREATE_COACH;
import static edu.kit.informatik.ui.command.CommandFactory.NUMBER; import static edu.kit.informatik.ui.command.CommandFactory.NUMBER;

View File

@ -2,17 +2,15 @@ package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal; import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.Coach; 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.Engine;
import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.model.ModelRailwaySimulation;
import edu.kit.informatik.model.SteamEngine;
import edu.kit.informatik.ui.EngineType; import edu.kit.informatik.ui.EngineType;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; 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.ALPHANUMERIC_WORD;
import static edu.kit.informatik.ui.command.CommandFactory.BOOL; import static edu.kit.informatik.ui.command.CommandFactory.BOOL;
import static edu.kit.informatik.ui.command.CommandFactory.CREATE_ENGINE; 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 { public class CreateEngine extends Command {
private static final Pattern CREATE_ENGINE_ARGUMENTS 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 + ")"); + NUMBER + ") (" + BOOL + ") (" + BOOL + ")");
/** /**
@ -59,20 +57,7 @@ public class CreateEngine extends Command {
if (type == null) { if (type == null) {
throw new IllegalStateException("command not initialized"); throw new IllegalStateException("command not initialized");
} }
final Engine engine; final Engine engine = type.createEngine(series, name, length, couplingFront, couplingBack);
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");
}
simulation.createEngine(engine); simulation.createEngine(engine);
Terminal.printLine(engine.getIdentifier()); Terminal.printLine(engine.getIdentifier());
} }