Skip to content

Commit

Permalink
Add friendlier syntax
Browse files Browse the repository at this point in the history
Supports shorthand alias
Support case-insensitive commands
  • Loading branch information
PyromancerBoom committed Feb 29, 2024
1 parent 816655d commit 57a5c3b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
6 changes: 3 additions & 3 deletions data/taskStorage.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
E | X | description | from: 01/01/2022 1200 to: 01/01/2022 1300
D | | this | by: Jan 01 2022 13:00
T | X | read book
T | | read book
D | | return book | by: Jun 05 2024 15:00
T | X | new Task LESGGOOo
E | | smth | from: 03/04/2024 0050 to: 04/04/2024 2359
E | X | smth | from: 03/04/2024 0050 to: 04/04/2024 2359
E | X | meetings | from: Feb 27 2024 15:00 to: Feb 29 2024 19:00
T | | Contact OpenAI
T | X | test everything
D | X | iP | by: Feb 28 2025 23:59
T | X | add docs for friendlier syntax
11 changes: 8 additions & 3 deletions src/main/java/duke/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public static String getBotExitMsg() {
exitMsg.append("\n");
exitMsg.append("I'll be back.");
exitMsg.append("\n");
exitMsg.append("You're supposed to press the exit button or type \"exit\" \n Don't make me do it for you.");
exitMsg.append(
"You're supposed to press the exit button or type \"exit/quit/q\" \n\n Don't make me do it for you.");
return exitMsg.toString();
}

Expand All @@ -76,9 +77,13 @@ public static String getBotExitMsg() {
public static String getBotHelpMsg() {
StringBuilder helpMsg = new StringBuilder();
helpMsg.append("Wasn't I clear earlier? I'm an extremely intelligent AI. But anyways...\n");
helpMsg.append("You were probably looking for this:\n");
helpMsg.append("You were probably looking for this:\n\n");
helpMsg.append(
"Commands: \n\t- todo \n\t- deadline \n\t- event \n\t- list \n\t- mark \n\t- unmark \n\t- bye \n\t- help\n");
"Commands: \n\t- todo (t) \n\t- deadline (d) \n\t- event (e) \n\t- list (l)"
+ "\n\t- mark (m) \n\t- unmark (um) \n\t- bye (b) \n\t- help (h) \n"
+ "\t- find (f) \n\t- delete (del) \n\t- exit/quit/q\n");
helpMsg.append("\n");
helpMsg.append("All commands are case-insensitive.");
return helpMsg.toString();
}

Expand Down
55 changes: 50 additions & 5 deletions src/main/java/duke/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,73 @@
import java.util.HashMap;
import java.util.Map;

/**
* This class is used to map string commands to their corresponding Command enum
* values.
*/
public class Commands {
// Enumerated type for the different commands
/**
* Enumerated type for the different commands.
*/
public enum Command {
BYE, LIST, HELP, MARK, UNMARK, DELETE, TODO, DEADLINE, EVENT, FIND, EMPTY, INVALID
BYE, LIST, HELP, MARK, UNMARK, DELETE, TODO, DEADLINE, EVENT, FIND, EMPTY, INVALID, EXIT
}

public static final Map<String, Command> commandMap = new HashMap<>();
/**
* A map to associate string commands with their corresponding Command enum
* values.
*/
private static final Map<String, Command> commandMap = new HashMap<>();

/*
* Static block to initialize the commandMap.
* Populates the commandMap with their alias and shorthand alias.
*/
static {
commandMap.put("b", Command.BYE);
commandMap.put("bye", Command.BYE);

commandMap.put("l", Command.LIST);
commandMap.put("list", Command.LIST);

commandMap.put("h", Command.HELP);
commandMap.put("help", Command.HELP);

commandMap.put("m", Command.MARK);
commandMap.put("mark", Command.MARK);

commandMap.put("um", Command.UNMARK);
commandMap.put("unmark", Command.UNMARK);

commandMap.put("del", Command.DELETE);
commandMap.put("delete", Command.DELETE);
commandMap.put("todo", Command.TODO);

commandMap.put("t", Command.TODO);

commandMap.put("d", Command.DEADLINE);
commandMap.put("deadline", Command.DEADLINE);

commandMap.put("e", Command.EVENT);
commandMap.put("event", Command.EVENT);

commandMap.put("f", Command.FIND);
commandMap.put("find", Command.FIND);

commandMap.put("", Command.EMPTY);

commandMap.put("exit", Command.EXIT);
commandMap.put("quit", Command.EXIT);
commandMap.put("q", Command.EXIT);
}

/**
* Returns the Command enum value associated with the given string command.
* If the command is not recognized, returns Command.INVALID.
*
* @param command The string command to look up.
* @return The Command enum value associated with the given string command.
*/
public static Command getCommand(String command) {
return commandMap.getOrDefault(command, Command.INVALID);
return commandMap.getOrDefault(command.toLowerCase(), Command.INVALID);
}
}
4 changes: 3 additions & 1 deletion src/main/java/duke/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ private void handleUserInput() {
DialogBox.getUserDialog(input, userImage),
DialogBox.getDukeDialog(response, dukeImage));
userInput.clear();
if ("exit".equals(input.trim())) {

// exit the application if the user input is exit enum
if (Commands.getCommand(input) == Commands.Command.EXIT) {
// sleep for 5 seconds before exiting
Platform.exit();
}
Expand Down

0 comments on commit 57a5c3b

Please sign in to comment.