Skip to content

Commit

Permalink
Merge pull request #5 from HugeNoob/branch-C-FriendlierSyntax
Browse files Browse the repository at this point in the history
feat: Friendlier syntax for commands
  • Loading branch information
HugeNoob authored Sep 10, 2023
2 parents f3141e1 + 8d5828d commit 7ee74c8
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/duke/commands/CommandType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package duke.commands;

import java.util.HashMap;
import java.util.Map;

import duke.exceptions.DukeInvalidCommandException;

/**
Expand All @@ -17,6 +20,19 @@ public enum CommandType {
DELETE_TASK("delete"),
FIND_TASK("find");

/** Shorter aliases for ease of use */
private static final Map<String, CommandType> commandMap = new HashMap<>(Map.of(
"exit", EXIT,
"q", EXIT,
"ls", LIST_TASKS,
"td", ADD_TODO,
"dl", ADD_DEADLINE,
"ev", ADD_EVENT,
"m", MARK_TASK,
"u", UNMARK_TASK,
"rm", DELETE_TASK,
"f", FIND_TASK));

/** The string representation of the command. */
private final String value;

Expand All @@ -37,6 +53,10 @@ private CommandType(String value) {
* @throws DukeInvalidCommandException If the command does not exist.
*/
public static CommandType fromString(String value) throws DukeInvalidCommandException {
if (commandMap.containsKey(value)) {
return commandMap.get(value);
}

for (CommandType command : CommandType.values()) {
if (command.value.equalsIgnoreCase(value)) {
return command;
Expand Down

0 comments on commit 7ee74c8

Please sign in to comment.