mirror of
https://github.com/DerTyp7/lf05-java.git
synced 2025-10-30 04:47:13 +01:00
nogard oder so
This commit is contained in:
38
src/nogard/schritt5/BefehlFactory.java
Normal file
38
src/nogard/schritt5/BefehlFactory.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package nogard.schritt5;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BefehlFactory {
|
||||
|
||||
public static Befehl createBefehl(String input) throws BefehlUnbekanntException {
|
||||
Befehl b;
|
||||
if (input.length() == 0) {
|
||||
throw new BefehlUnbekanntException("Keine Argumente übergeben");
|
||||
} else {
|
||||
String[] befehl_str = input.toLowerCase().split(" ");
|
||||
String wort = befehl_str[0];
|
||||
b = switch (wort) {
|
||||
case "go" -> createGoBefehl(befehl_str);
|
||||
case "help", "quit" -> new Befehl(wort);
|
||||
default -> throw new BefehlUnbekanntException(wort + " ist kein definierter Befehl.");
|
||||
};
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
private static Befehl createGoBefehl(String[] befehl_str) throws BefehlUnbekanntException {
|
||||
if (befehl_str.length != 2) {
|
||||
throw new BefehlUnbekanntException("Befehl 'go' benötigt 2 parameter");
|
||||
}
|
||||
|
||||
String befehl_wort = befehl_str[0];
|
||||
String befehl_zusatz = befehl_str[1];
|
||||
|
||||
for (Richtungen r: Richtungen.values()) {
|
||||
if (r.name().equalsIgnoreCase(befehl_zusatz)) {
|
||||
return new Befehl(befehl_wort, befehl_zusatz);
|
||||
}
|
||||
}
|
||||
throw new BefehlUnbekanntException(befehl_zusatz + " ist keine valide Richtung.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user