mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-24 09:24:58 +00:00
Use enums more/better
This commit is contained in:
parent
ac089109b2
commit
6ba920c82d
@ -5,6 +5,19 @@ public enum CoachType {
|
|||||||
FREIGHT,
|
FREIGHT,
|
||||||
SPECIAL;
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
switch (this) {
|
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;
|
package edu.kit.informatik.command;
|
||||||
|
|
||||||
import edu.kit.informatik.CoachType;
|
import edu.kit.informatik.CoachType;
|
||||||
|
import edu.kit.informatik.EngineType;
|
||||||
import edu.kit.informatik.Point;
|
import edu.kit.informatik.Point;
|
||||||
import edu.kit.informatik.Terminal;
|
import edu.kit.informatik.Terminal;
|
||||||
|
|
||||||
@ -120,7 +121,7 @@ public class CommandFactory {
|
|||||||
Terminal.printError("invalid create engine argument syntax");
|
Terminal.printError("invalid create engine argument syntax");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String type = matcher.group(1);
|
EngineType type = EngineType.parse(matcher.group(1));
|
||||||
String series = matcher.group(2);
|
String series = matcher.group(2);
|
||||||
if (series.startsWith("W")) {
|
if (series.startsWith("W")) {
|
||||||
Terminal.printError("invalid engine class/series");
|
Terminal.printError("invalid engine class/series");
|
||||||
@ -144,20 +145,11 @@ public class CommandFactory {
|
|||||||
Terminal.printError("invalid create coach arguments");
|
Terminal.printError("invalid create coach arguments");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
CoachType type = CoachType.parse(matcher.group(1));
|
||||||
int length = Integer.parseInt(matcher.group(2));
|
int length = Integer.parseInt(matcher.group(2));
|
||||||
boolean couplingFront = Boolean.parseBoolean(matcher.group(3));
|
boolean couplingFront = Boolean.parseBoolean(matcher.group(3));
|
||||||
boolean couplingBack = Boolean.parseBoolean(matcher.group(4));
|
boolean couplingBack = Boolean.parseBoolean(matcher.group(4));
|
||||||
switch (matcher.group(1)) {
|
return new CreateCoach(type, length, couplingFront, couplingBack);
|
||||||
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");
|
|
||||||
|
|
||||||
}
|
|
||||||
} else if (command.startsWith(LIST_COACHES)) {
|
} else if (command.startsWith(LIST_COACHES)) {
|
||||||
if (command.length() > LIST_COACHES.length()) {
|
if (command.length() > LIST_COACHES.length()) {
|
||||||
Terminal.printError("too many list coaches arguments");
|
Terminal.printError("too many list coaches arguments");
|
||||||
|
@ -9,7 +9,7 @@ import edu.kit.informatik.*;
|
|||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class CreateEngine extends Command {
|
public class CreateEngine extends Command {
|
||||||
private final String type;
|
private final EngineType type;
|
||||||
private final String series;
|
private final String series;
|
||||||
private final String name;
|
private final String name;
|
||||||
private final int length;
|
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 couplingFront whether the new engine should have a front coupling
|
||||||
* @param couplingBack whether the new engine should have a back 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.type = type;
|
||||||
this.series = series;
|
this.series = series;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@ -38,18 +38,17 @@ public class CreateEngine extends Command {
|
|||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
final Engine engine;
|
final Engine engine;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "electrical":
|
case ELECTRICAL:
|
||||||
engine = new ElectricalEngine(series, name, length, couplingFront, couplingBack);
|
engine = new ElectricalEngine(series, name, length, couplingFront, couplingBack);
|
||||||
break;
|
break;
|
||||||
case "steam":
|
case STEAM:
|
||||||
engine = new SteamEngine(series, name, length, couplingFront, couplingBack);
|
engine = new SteamEngine(series, name, length, couplingFront, couplingBack);
|
||||||
break;
|
break;
|
||||||
case "diesel":
|
case DIESEL:
|
||||||
engine = new DieselEngine(series, name, length, couplingFront, couplingBack);
|
engine = new DieselEngine(series, name, length, couplingFront, couplingBack);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Terminal.printError("invalid engine type");
|
throw new IllegalStateException("engine type is null!");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (simulation.createEngine(engine)) {
|
if (simulation.createEngine(engine)) {
|
||||||
Terminal.printLine(engine.getIdentifier());
|
Terminal.printLine(engine.getIdentifier());
|
||||||
|
Loading…
Reference in New Issue
Block a user