mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-09 02:10:40 +00:00
Use enums more/better
This commit is contained in:
parent
ac089109b2
commit
6ba920c82d
@ -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) {
|
||||
|
32
src/edu/kit/informatik/EngineType.java
Normal file
32
src/edu/kit/informatik/EngineType.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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");
|
||||
|
@ -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());
|
||||
|
Loading…
Reference in New Issue
Block a user