diff --git a/src/main/java/yappingbot/YappingBot.java b/src/main/java/yappingbot/YappingBot.java index 1ff509c251..d8fb19a95e 100644 --- a/src/main/java/yappingbot/YappingBot.java +++ b/src/main/java/yappingbot/YappingBot.java @@ -92,6 +92,14 @@ private void mainLoop() { case EXIT: // exits the function, ending the main loop return; + case ALIAS: + // creates new alias + // TODO: abstract this out to a Command object + parser.addAlias(userInputSlices[1], userInputSlices[2]); + ui.printf(ReplyTextMessages.ALIAS_ADDED_TEXT_2s, + userInputSlices[1], + userInputSlices[2]); + break; case RESET_LIST: // resets any filter on the list userList = new ResetViewCommand() diff --git a/src/main/java/yappingbot/commands/Parser.java b/src/main/java/yappingbot/commands/Parser.java index 6e36ab637f..97c274c7c2 100644 --- a/src/main/java/yappingbot/commands/Parser.java +++ b/src/main/java/yappingbot/commands/Parser.java @@ -2,6 +2,7 @@ import java.util.HashMap; +import yappingbot.exceptions.YappingBotIncorrectCommandException; import yappingbot.exceptions.YappingBotInvalidTaskNumberException; import yappingbot.exceptions.YappingBotOobException; import yappingbot.exceptions.YappingBotUnknownCommandException; @@ -18,7 +19,7 @@ public class Parser { * @see Uses for enum */ public enum CommandTypes { - FIND, LIST, MARK, UNMARK, DELETE, TODO, EVENT, DEADLINE, EXIT, RESET_LIST, UNKNOWN + FIND, LIST, MARK, UNMARK, DELETE, TODO, EVENT, DEADLINE, EXIT, RESET_LIST, ALIAS, UNKNOWN } /** @@ -63,6 +64,8 @@ public Parser() { commandsHashMap.put("exit", CommandTypes.EXIT); commandsHashMap.put(":q", CommandTypes.EXIT); + commandsHashMap.put("alias", CommandTypes.ALIAS); + } /** @@ -88,6 +91,25 @@ public CommandTypes parseCommand(String commandString) } } + /** + * Adds new aliases for commands. + * + * @param currentCommand String of the current command to execute + * @param newAlias New alias to launch same command + */ + public void addAlias(String currentCommand, String newAlias) { + // TODO: persistant settings + assert currentCommand != null; + assert newAlias != null; + if (!commandsHashMap.containsKey(currentCommand)) { + throw new YappingBotIncorrectCommandException( + ReplyTextMessages.ALIAS_USAGE, + currentCommand); + } + + commandsHashMap.putIfAbsent(newAlias, commandsHashMap.get(currentCommand)); + } + /** * Parses any text that must be interpreted as an integer. * diff --git a/src/main/java/yappingbot/stringconstants/ReplyTextMessages.java b/src/main/java/yappingbot/stringconstants/ReplyTextMessages.java index 613ca84b8e..be21333ef7 100644 --- a/src/main/java/yappingbot/stringconstants/ReplyTextMessages.java +++ b/src/main/java/yappingbot/stringconstants/ReplyTextMessages.java @@ -103,4 +103,6 @@ public final class ReplyTextMessages { // todo: reset usage text public static final String RESET_USAGE = ""; public static final String FIND_USAGE = ""; + public static final String ALIAS_ADDED_TEXT_2s = "Alias added! %s can now be executed with %s."; + public static final String ALIAS_USAGE = ""; }