Use enums more/better

This commit is contained in:
Arne Keller 2020-02-15 15:50:55 +01:00
parent ac089109b2
commit 6ba920c82d
4 changed files with 55 additions and 19 deletions

View File

@ -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) {

View File

@ -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;
}
}
}

View File

@ -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");

View File

@ -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());