From 931ff805a9496c03c22887bed1aa4b4b5cf826fc Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Wed, 19 Feb 2020 23:09:43 +0100 Subject: [PATCH] Store available commands in map --- .../informatik/ui/command/CommandFactory.java | 79 +++++++++---------- 1 file changed, 36 insertions(+), 43 deletions(-) diff --git a/src/edu/kit/informatik/ui/command/CommandFactory.java b/src/edu/kit/informatik/ui/command/CommandFactory.java index 631a2b0..293b944 100644 --- a/src/edu/kit/informatik/ui/command/CommandFactory.java +++ b/src/edu/kit/informatik/ui/command/CommandFactory.java @@ -2,6 +2,10 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.ui.InvalidInputException; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Supplier; + /** * Factory used to parse user input into commands. * @@ -99,6 +103,29 @@ public final class CommandFactory { * Name of the step command. */ public static final String STEP = "step"; + + private static final Map> COMMANDS = new HashMap<>(); + + static { + COMMANDS.put(ADD_TRACK, AddTrack::new); + COMMANDS.put(ADD_SWITCH, AddSwitch::new); + COMMANDS.put(DELETE_TRACK, DeleteTrack::new); + COMMANDS.put(LIST_TRACKS, ListTracks::new); + COMMANDS.put(SET_SWITCH, SetSwitch::new); + COMMANDS.put(CREATE_ENGINE, CreateEngine::new); + COMMANDS.put(LIST_ENGINES, ListEngines::new); + COMMANDS.put(CREATE_COACH, CreateCoach::new); + COMMANDS.put(LIST_COACHES, ListCoaches::new); + COMMANDS.put(CREATE_TRAIN_SET, CreateTrainSet::new); + COMMANDS.put(LIST_TRAIN_SETS, ListTrainSets::new); + COMMANDS.put(DELETE_ROLLING_STOCK, DeleteRollingStock::new); + COMMANDS.put(ADD_TRAIN, AddTrain::new); + COMMANDS.put(DELETE_TRAIN, DeleteTrain::new); + COMMANDS.put(LIST_TRAINS, ListTrains::new); + COMMANDS.put(SHOW_TRAIN, ShowTrain::new); + COMMANDS.put(PUT_TRAIN, PutTrain::new); + COMMANDS.put(STEP, Step::new); + } /** * Utility class -> private constructor. @@ -109,52 +136,18 @@ public final class CommandFactory { /** * Parse a single line of user input into one command. - * @param command user input line + * @param input user input line * @return a fully specified command object * @throws InvalidInputException if user input is invalid */ - public static Command getCommand(final String command) throws InvalidInputException { - Command commandObject; - if (command.startsWith(ADD_TRACK)) { - commandObject = new AddTrack(); - } else if (command.startsWith(ADD_SWITCH)) { - commandObject = new AddSwitch(); - } else if (command.startsWith(DELETE_TRACK)) { - commandObject = new DeleteTrack(); - } else if (command.startsWith(LIST_TRACKS)) { - commandObject = new ListTracks(); - } else if (command.startsWith(SET_SWITCH)) { - commandObject = new SetSwitch(); - } else if (command.startsWith(CREATE_ENGINE)) { - commandObject = new CreateEngine(); - } else if (command.startsWith(LIST_ENGINES)) { - commandObject = new ListEngines(); - } else if (command.startsWith(CREATE_COACH)) { - commandObject = new CreateCoach(); - } else if (command.startsWith(LIST_COACHES)) { - commandObject = new ListCoaches(); - } else if (command.startsWith(CREATE_TRAIN_SET)) { - commandObject = new CreateTrainSet(); - } else if (command.startsWith(LIST_TRAIN_SETS)) { - commandObject = new ListTrainSets(); - } else if (command.startsWith(DELETE_ROLLING_STOCK)) { - commandObject = new DeleteRollingStock(); - } else if (command.startsWith(ADD_TRAIN)) { - commandObject = new AddTrain(); - } else if (command.startsWith(DELETE_TRAIN)) { - commandObject = new DeleteTrain(); - } else if (command.startsWith(LIST_TRAINS)) { - commandObject = new ListTrains(); - } else if (command.startsWith(SHOW_TRAIN)) { - commandObject = new ShowTrain(); - } else if (command.startsWith(PUT_TRAIN)) { - commandObject = new PutTrain(); - } else if (command.startsWith(STEP)) { - commandObject = new Step(); - } else { - throw new InvalidInputException("unknown command"); + public static Command getCommand(String input) throws InvalidInputException { + for (Map.Entry> entry : COMMANDS.entrySet()) { + if (input.startsWith(entry.getKey())) { + Command command = entry.getValue().get(); + command.parse(input); + return command; + } } - commandObject.parse(command); - return commandObject; + throw new InvalidInputException("unknown command"); } }