From ca1597e9136eb4324cc2d59a65927e7363cbcf3d Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 18 Sep 2023 21:19:53 +0800 Subject: [PATCH 1/7] Edit command format for easier validation --- src/main/java/corgi/commands/CommandType.java | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/main/java/corgi/commands/CommandType.java b/src/main/java/corgi/commands/CommandType.java index 704d9f5e73..27b1210a02 100644 --- a/src/main/java/corgi/commands/CommandType.java +++ b/src/main/java/corgi/commands/CommandType.java @@ -1,30 +1,36 @@ package corgi.commands; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + /** * Types of commands that can be given to the Corgi chat bot. */ public enum CommandType { - MARK("mark [task no.]"), - UNMARK("unmark [task no.]"), - TODO("todo [task]"), - DEADLINE("deadline [task] /by [yyyy-mm-dd]"), - EVENT("event [task] /from [yyyy-mm-dd] /to [yyyy-mm-dd]"), - BYE("bye"), - LIST("list"), - DELETE("delete [task no.]"), - DATE("date [yyyy-mm-dd]"), - FIND("find [keyword]"), - UNDO("undo"); + TODO("todo /desc [task]", new HashSet<>(Arrays.asList("/desc"))), + DEADLINE("deadline /desc [task] /by [yyyy-mm-dd]", new HashSet<>(Arrays.asList("/desc", "/by"))), + EVENT("event /desc [task] /from [yyyy-mm-dd] /to [yyyy-mm-dd]", new HashSet<>(Arrays.asList("/desc", "/from", "/to"))), + MARK("mark /target [task no.]", new HashSet<>(Arrays.asList("/target"))), + UNMARK("unmark /target [task no.]", new HashSet<>(Arrays.asList("/target"))), + DELETE("delete /target [task no.]", new HashSet<>(Arrays.asList("/target"))), + DATE("date /target [yyyy-mm-dd]", new HashSet<>(Arrays.asList("/target"))), + FIND("find /target [keyword]", new HashSet<>(Arrays.asList("/target"))), + BYE("bye", new HashSet<>()), + LIST("list", new HashSet<>()), + UNDO("undo", new HashSet<>()); private final String commandFormat; + private final Set arguments; /** * Constructs a new CommandType with the given command format. * * @param commandFormat The command format */ - CommandType(String commandFormat) { + CommandType(String commandFormat, Set arguments) { this.commandFormat = commandFormat; + this.arguments = arguments; } /** @@ -66,10 +72,19 @@ public static CommandType getCommandType(String commandStr) throws InvalidComman /** * Retrieves the command format string for this CommandType. * - * @return The command format string + * @return The command format string. */ public String getCommandFormat() { return this.commandFormat; } + /** + * Retrieves the arguments list for this CommandType. + * + * @return The set of arguments. + */ + public Set getArgumentsList() { + return this.arguments; + } + } From fb27cff503486705e104f8941a8710d6fc31db28 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 18 Sep 2023 23:42:41 +0800 Subject: [PATCH 2/7] Edit getter format --- src/main/java/corgi/commands/CommandType.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/corgi/commands/CommandType.java b/src/main/java/corgi/commands/CommandType.java index 27b1210a02..aa6109f682 100644 --- a/src/main/java/corgi/commands/CommandType.java +++ b/src/main/java/corgi/commands/CommandType.java @@ -75,7 +75,7 @@ public static CommandType getCommandType(String commandStr) throws InvalidComman * @return The command format string. */ public String getCommandFormat() { - return this.commandFormat; + return "Format: \n" + this.commandFormat; } /** @@ -83,7 +83,7 @@ public String getCommandFormat() { * * @return The set of arguments. */ - public Set getArgumentsList() { + public Set getArgumentsSet() { return this.arguments; } From 3c7db7ce68a097f40fa77acd06465ecf1aa668d0 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 18 Sep 2023 23:45:08 +0800 Subject: [PATCH 3/7] Create Validator class for command validation --- .../java/corgi/parsers/CommandValidator.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/main/java/corgi/parsers/CommandValidator.java diff --git a/src/main/java/corgi/parsers/CommandValidator.java b/src/main/java/corgi/parsers/CommandValidator.java new file mode 100644 index 0000000000..603bfd0529 --- /dev/null +++ b/src/main/java/corgi/parsers/CommandValidator.java @@ -0,0 +1,60 @@ +package corgi.parsers; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; + +/** + * The CommandValidator class provides methods for validating command arguments. + */ +public class CommandValidator { + + /** + * Validates the presence and number of expected arguments in a command. + * + * @param command The input command string to be validated. + * @param arguments A set of expected arguments that should be present in the command. + * @throws InvalidCommandFormatException If the command does not contain all the expected arguments + * or contains duplicate arguments. + */ + public void validateArguments(String command, Set arguments) + throws InvalidCommandFormatException { + List errorMsg = new ArrayList<>(); + boolean isValid = true; + for (String argument : arguments) { + long numOfArg = new ArrayList(Arrays.asList(command.split(" "))) + .stream() + .filter(x -> x.equals(argument)) + .count(); + + if (numOfArg == 1) { + continue; + } + + if (numOfArg == 0) { + isValid = false; + errorMsg.add("Missing argument " + "\"" + argument + "\" !"); + } else { + errorMsg.add("Invalid number of argument " + "\"" + argument + "\" !"); + } + } + + String fullErrorMsg = String.join("\n", errorMsg); + + if (!isValid) { + throw new InvalidCommandFormatException(fullErrorMsg); + } + } + + /** + * Checks if a command has no arguments. + * + * @param command The input command string to be checked for the absence of arguments. + * @return true if the command has no arguments, false otherwise. + */ + public boolean hasNoArgument(String command) { + String[] splitWithSpace = command.split(" "); + return splitWithSpace.length == 1; + } +} From 329239f2a2e859820bf5b80dd61b6b099517e7e6 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 18 Sep 2023 23:45:42 +0800 Subject: [PATCH 4/7] Edit text format --- src/main/java/corgi/ui/TextRenderer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/corgi/ui/TextRenderer.java b/src/main/java/corgi/ui/TextRenderer.java index f1b33ef9bb..5c40e1da4f 100644 --- a/src/main/java/corgi/ui/TextRenderer.java +++ b/src/main/java/corgi/ui/TextRenderer.java @@ -69,7 +69,7 @@ public String showError(String exception) { public String showError(String exception, String extraMsg) { return returnMessage( this.showError(exception), - "Error: " + extraMsg); + "Error: \n" + extraMsg); } /** From ec242ff26d744c3e430b5bdbcbd7bed663f2acd6 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 18 Sep 2023 23:47:20 +0800 Subject: [PATCH 5/7] Refactor command parsing logic and error handling --- .../java/corgi/parsers/CommandParser.java | 289 ++++++++++++------ 1 file changed, 189 insertions(+), 100 deletions(-) diff --git a/src/main/java/corgi/parsers/CommandParser.java b/src/main/java/corgi/parsers/CommandParser.java index 2fda5820ff..274e8f5be1 100644 --- a/src/main/java/corgi/parsers/CommandParser.java +++ b/src/main/java/corgi/parsers/CommandParser.java @@ -2,6 +2,10 @@ import java.time.LocalDate; import java.time.format.DateTimeParseException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; import corgi.commands.AddTaskCommand; import corgi.commands.Command; @@ -24,6 +28,12 @@ */ public class CommandParser extends Parser { + private final CommandValidator validator; + + public CommandParser() { + this.validator = new CommandValidator(); + } + /** * Parses the given full command string and generates the corresponding Command object. * @@ -49,37 +59,37 @@ public Command parse(String fullCommand) throws InvalidCommandFormatException, I switch (cmd) { case UNDO: - command = newUndoCommand(inputs); + command = newUndoCommand(fullCommand); break; case BYE: - command = newExitCommand(inputs); + command = newExitCommand(fullCommand); break; case LIST: - command = newListCommand(inputs); + command = newListCommand(fullCommand); break; case MARK: - command = newMarkCommand(inputs); + command = newMarkCommand(fullCommand); break; case UNMARK: - command = newUnMarkCommand(inputs); + command = newUnMarkCommand(fullCommand); break; case TODO: - command = newAddTodoCommand(inputs); + command = newAddTodoCommand(fullCommand); break; case DEADLINE: - command = newAddDeadlineCommand(inputs); + command = newAddDeadlineCommand(fullCommand); break; case EVENT: - command = newAddEventCommand(inputs); + command = newAddEventCommand(fullCommand); break; case DELETE: - command = newDeleteCommand(inputs); + command = newDeleteCommand(fullCommand); break; case DATE: - command = newDateCommand(inputs); + command = newDateCommand(fullCommand); break; case FIND: - command = newFindCommand(inputs); + command = newFindCommand(fullCommand); break; default: throw new InvalidCommandTypeException("Invalid Command!"); @@ -88,93 +98,129 @@ public Command parse(String fullCommand) throws InvalidCommandFormatException, I return command; } - private Command newUndoCommand(String[] inputs) throws InvalidCommandFormatException { - if (inputs.length > 1) { - throw new InvalidCommandFormatException("No argument is needed!" + "\nFormat: " + private Command newUndoCommand(String fullCommand) throws InvalidCommandFormatException { + if (!validator.hasNoArgument(fullCommand)) { + throw new InvalidCommandFormatException("No argument is needed!" + "\n\n" + CommandType.UNDO.getCommandFormat()); } return new UndoCommand(); } - private Command newExitCommand(String[] inputs) throws InvalidCommandFormatException { - if (inputs.length > 1) { - throw new InvalidCommandFormatException("No argument is needed!" + "\nFormat: " + private Command newExitCommand(String fullCommand) throws InvalidCommandFormatException { + if (!validator.hasNoArgument(fullCommand)) { + throw new InvalidCommandFormatException("No argument is needed!" + "\n\n" + CommandType.BYE.getCommandFormat()); } return new ExitCommand(); } - private Command newListCommand(String[] inputs) throws InvalidCommandFormatException { - if (inputs.length > 1) { - throw new InvalidCommandFormatException("No argument is needed!" + "\nFormat: " + private Command newListCommand(String fullCommand) throws InvalidCommandFormatException { + if (!validator.hasNoArgument(fullCommand)) { + throw new InvalidCommandFormatException("No argument is needed!" + "\n\n" + CommandType.LIST.getCommandFormat()); } return new ListTasksCommand(); } - private Command newMarkCommand(String[] inputs) throws InvalidCommandFormatException { + private Command newMarkCommand(String fullCommand) throws InvalidCommandFormatException { CommandType commandType = CommandType.MARK; String commandFormat = commandType.getCommandFormat(); + Set arguments = commandType.getArgumentsSet(); - if (inputs.length == 1) { - throw new InvalidCommandFormatException("No argument is provided!" + "\nFormat: " + if (validator.hasNoArgument(fullCommand)) { + throw new InvalidCommandFormatException("No argument is provided!" + "\n\n" + commandFormat); } + // Validate whether all arguments are given + this.validator.validateArguments(fullCommand, arguments); + + // Parse arguments + Map labelToValue = parseCommandArgs(fullCommand, arguments); + String targetTaskNumber = labelToValue.get("/target"); + try { - int index = Integer.parseInt(inputs[1]) - 1; + int index = Integer.parseInt(targetTaskNumber) - 1; return new MarkTaskCommand(index, true); } catch (NumberFormatException e) { - throw new InvalidCommandFormatException("Please provide a valid task number!" + "\nFormat: " + throw new InvalidCommandFormatException("Please provide a valid task number!" + "\n\n" + commandFormat); } } - private Command newUnMarkCommand(String[] inputs) throws InvalidCommandFormatException { + private Command newUnMarkCommand(String fullCommand) throws InvalidCommandFormatException { CommandType commandType = CommandType.UNMARK; String commandFormat = commandType.getCommandFormat(); + Set arguments = commandType.getArgumentsSet(); - if (inputs.length == 1) { - throw new InvalidCommandFormatException("No argument is provided!" + "\nFormat: " + if (validator.hasNoArgument(fullCommand)) { + throw new InvalidCommandFormatException("No argument is provided!" + "\n\n" + commandFormat); } + // Validate whether all arguments are given + this.validator.validateArguments(fullCommand, arguments); + + // Parse arguments + Map labelToValue = parseCommandArgs(fullCommand, arguments); + String targetTaskNumber = labelToValue.get("/target"); + try { - int index = Integer.parseInt(inputs[1]) - 1; + int index = Integer.parseInt(targetTaskNumber) - 1; return new MarkTaskCommand(index, true); } catch (NumberFormatException e) { - throw new InvalidCommandFormatException("Please provide a valid task number!" + "\nFormat: " + throw new InvalidCommandFormatException("Please provide a valid task number!" + "\n\n" + commandFormat); } } - private Command newDeleteCommand(String[] inputs) throws InvalidCommandFormatException { - if (inputs.length == 1) { - throw new InvalidCommandFormatException("No argument is provided!" + "\nFormat: " - + CommandType.DELETE.getCommandFormat()); + private Command newDeleteCommand(String fullCommand) throws InvalidCommandFormatException { + CommandType commandType = CommandType.DELETE; + String commandFormat = commandType.getCommandFormat(); + Set arguments = commandType.getArgumentsSet(); + + if (validator.hasNoArgument(fullCommand)) { + throw new InvalidCommandFormatException("No argument is provided!" + "\n\n" + + commandFormat); } + // Validate whether all arguments are given + this.validator.validateArguments(fullCommand, arguments); + + // Parse arguments + Map labelToValue = parseCommandArgs(fullCommand, arguments); + String targetTaskNumber = labelToValue.get("/target"); + try { - int index = Integer.parseInt(inputs[1]) - 1; + int index = Integer.parseInt(targetTaskNumber) - 1; return new DeleteTaskCommand(index); } catch (NumberFormatException e) { - throw new InvalidCommandFormatException("Please provide a valid task number!" + "\nFormat: " - + CommandType.DELETE.getCommandFormat()); + throw new InvalidCommandFormatException("Please provide a valid task number!" + "\n\n" + + commandFormat); } } - private Command newDateCommand(String[] inputs) throws InvalidCommandFormatException { - if (inputs.length == 1) { - throw new InvalidCommandFormatException("No argument is provided!" + "\nFormat: " - + CommandType.DATE.getCommandFormat()); + private Command newDateCommand(String fullCommand) throws InvalidCommandFormatException { + CommandType commandType = CommandType.DATE; + String commandFormat = commandType.getCommandFormat(); + Set arguments = commandType.getArgumentsSet(); + + if (validator.hasNoArgument(fullCommand)) { + throw new InvalidCommandFormatException("No argument is provided!" + "\n\n" + + commandFormat); } - LocalDate target = null; + // Validate whether all arguments are given + this.validator.validateArguments(fullCommand, arguments); - String dateStr = inputs[1]; + // Parse arguments + Map labelToValue = parseCommandArgs(fullCommand, arguments); + String targetDate = labelToValue.get("/target"); + + LocalDate target = null; try { - target = LocalDate.parse(dateStr, Task.DATE_INPUT_FORMATTER); + target = LocalDate.parse(targetDate, Task.DATE_INPUT_FORMATTER); } catch (DateTimeParseException e) { throw new InvalidCommandFormatException("Invalid date format!"); } @@ -184,57 +230,74 @@ private Command newDateCommand(String[] inputs) throws InvalidCommandFormatExcep return new FindTasksOnDateCommand(target); } - private Command newFindCommand(String[] inputs) throws InvalidCommandFormatException { - if (inputs.length == 1) { - throw new InvalidCommandFormatException("No argument is provided!" + "\nFormat: " - + CommandType.FIND.getCommandFormat()); + private Command newFindCommand(String fullCommand) throws InvalidCommandFormatException { + CommandType commandType = CommandType.FIND; + String commandFormat = commandType.getCommandFormat(); + Set arguments = commandType.getArgumentsSet(); + + if (validator.hasNoArgument(fullCommand)) { + throw new InvalidCommandFormatException("No argument is provided!" + "\n\n" + + commandFormat); } - String keyword = inputs[1]; + // Validate whether all arguments are given + this.validator.validateArguments(fullCommand, arguments); + + // Parse arguments + Map labelToValue = parseCommandArgs(fullCommand, arguments); + String targetKeyword = labelToValue.get("/target"); - return new FindTasksContainKeywordCommand(keyword); + return new FindTasksContainKeywordCommand(targetKeyword); } - private Command newAddTodoCommand(String[] inputs) throws InvalidCommandFormatException { - if (inputs.length == 1) { - throw new InvalidCommandFormatException("No argument is provided!" + "\nFormat: " - + CommandType.TODO.getCommandFormat()); + private Command newAddTodoCommand(String fullCommand) throws InvalidCommandFormatException { + CommandType commandType = CommandType.TODO; + String commandFormat = commandType.getCommandFormat(); + Set arguments = commandType.getArgumentsSet(); + + if (validator.hasNoArgument(fullCommand)) { + throw new InvalidCommandFormatException("No argument is provided!" + "\n\n" + + commandFormat); } - String taskInfo = inputs[1]; + // Validate whether all arguments are given + this.validator.validateArguments(fullCommand, arguments); + + // Parse arguments + Map labelToValue = parseCommandArgs(fullCommand, arguments); + String todoDesc = labelToValue.get("/desc"); - Task target = new ToDo(taskInfo); + Task target = new ToDo(todoDesc); assert target != null : "New Todo task cannot be null."; return new AddTaskCommand(target); } - private Command newAddDeadlineCommand(String[] inputs) throws InvalidCommandFormatException { - String commandFormat = CommandType.DEADLINE.getCommandFormat(); + private Command newAddDeadlineCommand(String fullCommand) throws InvalidCommandFormatException { + CommandType commandType = CommandType.DEADLINE; + String commandFormat = commandType.getCommandFormat(); + Set arguments = commandType.getArgumentsSet(); - if (inputs.length == 1) { - throw new InvalidCommandFormatException("No argument is provided!" + "\nFormat: " + if (validator.hasNoArgument(fullCommand)) { + throw new InvalidCommandFormatException("No argument is provided!" + "\n\n" + commandFormat); } - String taskInfo = inputs[1]; - - // todo: check number of /by - String[] deadlineInfos = taskInfo.split(" /by "); + // Validate whether all arguments are given + this.validator.validateArguments(fullCommand, arguments); - if (deadlineInfos.length == 1) { - throw new InvalidCommandFormatException("Missing deadline!" + "\nFormat: " - + commandFormat); - } + // Parse arguments + Map labelToValue = parseCommandArgs(fullCommand, arguments); + String deadlineDesc = labelToValue.get("/desc"); + String deadlineStr = labelToValue.get("/by"); - String deadlineDesc = deadlineInfos[0]; LocalDate by = null; try { - by = LocalDate.parse(deadlineInfos[1], Task.DATE_INPUT_FORMATTER); + by = LocalDate.parse(deadlineStr, Task.DATE_INPUT_FORMATTER); } catch (DateTimeParseException e) { - throw new InvalidCommandFormatException("Invalid date format!" + "\nFormat: " + throw new InvalidCommandFormatException("Invalid date format!" + "\n\n" + commandFormat); } @@ -247,46 +310,33 @@ private Command newAddDeadlineCommand(String[] inputs) throws InvalidCommandForm return new AddTaskCommand(target); } - private Command newAddEventCommand(String[] inputs) throws InvalidCommandFormatException { - String commandFormat = CommandType.EVENT.getCommandFormat(); + private Command newAddEventCommand(String fullCommand) throws InvalidCommandFormatException { + CommandType commandType = CommandType.EVENT; + String commandFormat = commandType.getCommandFormat(); + Set arguments = commandType.getArgumentsSet(); - if (inputs.length == 1) { - throw new InvalidCommandFormatException("No argument is provided!" + "\nFormat: " + if (validator.hasNoArgument(fullCommand)) { + throw new InvalidCommandFormatException("No argument is provided!" + "\n" + commandFormat); } - String taskInfo = inputs[1]; + // Validate whether all arguments are given + this.validator.validateArguments(fullCommand, arguments); - // todo: check number of /from, /to, check order - String[] eventInfos = taskInfo.split(" /from "); - - if (eventInfos.length < 2) { - throw new InvalidCommandFormatException("Missing /from argument." + "\nFormat: " - + commandFormat); - } else if (eventInfos.length > 2) { - throw new InvalidCommandFormatException("Only one /from argument is needed." + "\nFormat: " - + commandFormat); - } - - String eventDesc = eventInfos[0]; - String[] eventDuration = eventInfos[1].split(" /to "); - - if (eventDuration.length < 2) { - throw new InvalidCommandFormatException("Missing /to argument!" + "\nFormat: " - + commandFormat); - } else if (eventDuration.length > 2) { - throw new InvalidCommandFormatException("Only one /to argument is needed." + "\nFormat: " - + commandFormat); - } + // Parse arguments + Map labelToValue = parseCommandArgs(fullCommand, arguments); + String eventDesc = labelToValue.get("/desc"); + String startDateStr = labelToValue.get("/from"); + String endDateStr = labelToValue.get("/to"); LocalDate from = null; LocalDate to = null; try { - from = LocalDate.parse(eventDuration[0], Task.DATE_INPUT_FORMATTER); - to = LocalDate.parse(eventDuration[1], Task.DATE_INPUT_FORMATTER); + from = LocalDate.parse(startDateStr, Task.DATE_INPUT_FORMATTER); + to = LocalDate.parse(endDateStr, Task.DATE_INPUT_FORMATTER); } catch (DateTimeParseException e) { - throw new InvalidCommandFormatException("Invalid date format!" + "\nFormat: " + throw new InvalidCommandFormatException("Invalid date format!" + "\n\n" + commandFormat); } @@ -299,4 +349,43 @@ private Command newAddEventCommand(String[] inputs) throws InvalidCommandFormatE return new AddTaskCommand(target); } + + private Map parseCommandArgs(String command, Set arguments) throws InvalidCommandFormatException { + String[] splitWithSpace = command.split(" "); + Map argToValue = new HashMap<>(); + + String currArg = null; + int indexOfCurrArg = 0; + + for (int i = 0; i < splitWithSpace.length; i++) { + String currWord = splitWithSpace[i]; + + if (!arguments.contains(currWord)) { + continue; + } + + if (currArg != null) { + if (indexOfCurrArg + 1 == i) { + throw new InvalidCommandFormatException("Missing value for argument \"" + currArg + "\""); + } + String[] valueList = Arrays.copyOfRange(splitWithSpace, indexOfCurrArg + 1, i); + String value = String.join(" ", valueList); + argToValue.put(currArg, value); + } + + currArg = currWord; + indexOfCurrArg = i; + } + + if (currArg != null) { + if (indexOfCurrArg + 1 == splitWithSpace.length) { + throw new InvalidCommandFormatException("Missing value for argument \"" + currArg + "\" !"); + } + String[] valueList = Arrays.copyOfRange(splitWithSpace, indexOfCurrArg + 1, splitWithSpace.length); + String value = String.join(" ", valueList); + argToValue.put(currArg, value); + } + + return argToValue; + } } From 59d66b6c4e2b090d567580e08cc8d4a0b8ed9d71 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 18 Sep 2023 23:57:48 +0800 Subject: [PATCH 6/7] Check start date is before end date --- src/main/java/corgi/parsers/CommandParser.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/corgi/parsers/CommandParser.java b/src/main/java/corgi/parsers/CommandParser.java index 274e8f5be1..b67afcabae 100644 --- a/src/main/java/corgi/parsers/CommandParser.java +++ b/src/main/java/corgi/parsers/CommandParser.java @@ -340,6 +340,11 @@ private Command newAddEventCommand(String fullCommand) throws InvalidCommandForm + commandFormat); } + // Validate that start date is before end date + if (!from.isBefore(to)) { + throw new InvalidCommandFormatException("The start date should be before the end date!"); + } + assert from != null : "Date cannot be null."; assert to != null : "Date cannot be null."; From 004ecf2d516b91af7e71efc594559cd991b91be7 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Tue, 19 Sep 2023 00:02:53 +0800 Subject: [PATCH 7/7] Fix checkstyle error --- src/main/java/corgi/commands/CommandType.java | 33 ++++++++++++------- .../java/corgi/parsers/CommandParser.java | 7 ++-- .../java/corgi/parsers/CommandValidator.java | 6 ++-- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/main/java/corgi/commands/CommandType.java b/src/main/java/corgi/commands/CommandType.java index aa6109f682..07c797ef14 100644 --- a/src/main/java/corgi/commands/CommandType.java +++ b/src/main/java/corgi/commands/CommandType.java @@ -8,17 +8,28 @@ * Types of commands that can be given to the Corgi chat bot. */ public enum CommandType { - TODO("todo /desc [task]", new HashSet<>(Arrays.asList("/desc"))), - DEADLINE("deadline /desc [task] /by [yyyy-mm-dd]", new HashSet<>(Arrays.asList("/desc", "/by"))), - EVENT("event /desc [task] /from [yyyy-mm-dd] /to [yyyy-mm-dd]", new HashSet<>(Arrays.asList("/desc", "/from", "/to"))), - MARK("mark /target [task no.]", new HashSet<>(Arrays.asList("/target"))), - UNMARK("unmark /target [task no.]", new HashSet<>(Arrays.asList("/target"))), - DELETE("delete /target [task no.]", new HashSet<>(Arrays.asList("/target"))), - DATE("date /target [yyyy-mm-dd]", new HashSet<>(Arrays.asList("/target"))), - FIND("find /target [keyword]", new HashSet<>(Arrays.asList("/target"))), - BYE("bye", new HashSet<>()), - LIST("list", new HashSet<>()), - UNDO("undo", new HashSet<>()); + TODO("todo /desc [task]", + new HashSet<>(Arrays.asList("/desc"))), + DEADLINE("deadline /desc [task] /by [yyyy-mm-dd]", + new HashSet<>(Arrays.asList("/desc", "/by"))), + EVENT("event /desc [task] /from [yyyy-mm-dd] /to [yyyy-mm-dd]", + new HashSet<>(Arrays.asList("/desc", "/from", "/to"))), + MARK("mark /target [task no.]", + new HashSet<>(Arrays.asList("/target"))), + UNMARK("unmark /target [task no.]", + new HashSet<>(Arrays.asList("/target"))), + DELETE("delete /target [task no.]", + new HashSet<>(Arrays.asList("/target"))), + DATE("date /target [yyyy-mm-dd]", + new HashSet<>(Arrays.asList("/target"))), + FIND("find /target [keyword]", + new HashSet<>(Arrays.asList("/target"))), + BYE("bye", + new HashSet<>()), + LIST("list", + new HashSet<>()), + UNDO("undo", + new HashSet<>()); private final String commandFormat; private final Set arguments; diff --git a/src/main/java/corgi/parsers/CommandParser.java b/src/main/java/corgi/parsers/CommandParser.java index b67afcabae..c48c6f6ad7 100644 --- a/src/main/java/corgi/parsers/CommandParser.java +++ b/src/main/java/corgi/parsers/CommandParser.java @@ -160,7 +160,7 @@ private Command newUnMarkCommand(String fullCommand) throws InvalidCommandFormat // Validate whether all arguments are given this.validator.validateArguments(fullCommand, arguments); - + // Parse arguments Map labelToValue = parseCommandArgs(fullCommand, arguments); String targetTaskNumber = labelToValue.get("/target"); @@ -355,7 +355,8 @@ private Command newAddEventCommand(String fullCommand) throws InvalidCommandForm return new AddTaskCommand(target); } - private Map parseCommandArgs(String command, Set arguments) throws InvalidCommandFormatException { + private Map parseCommandArgs(String command, Set arguments) + throws InvalidCommandFormatException { String[] splitWithSpace = command.split(" "); Map argToValue = new HashMap<>(); @@ -379,7 +380,7 @@ private Map parseCommandArgs(String command, Set argumen } currArg = currWord; - indexOfCurrArg = i; + indexOfCurrArg = i; } if (currArg != null) { diff --git a/src/main/java/corgi/parsers/CommandValidator.java b/src/main/java/corgi/parsers/CommandValidator.java index 603bfd0529..501f1c206e 100644 --- a/src/main/java/corgi/parsers/CommandValidator.java +++ b/src/main/java/corgi/parsers/CommandValidator.java @@ -18,7 +18,7 @@ public class CommandValidator { * @throws InvalidCommandFormatException If the command does not contain all the expected arguments * or contains duplicate arguments. */ - public void validateArguments(String command, Set arguments) + public void validateArguments(String command, Set arguments) throws InvalidCommandFormatException { List errorMsg = new ArrayList<>(); boolean isValid = true; @@ -31,7 +31,7 @@ public void validateArguments(String command, Set arguments) if (numOfArg == 1) { continue; } - + if (numOfArg == 0) { isValid = false; errorMsg.add("Missing argument " + "\"" + argument + "\" !"); @@ -39,7 +39,7 @@ public void validateArguments(String command, Set arguments) errorMsg.add("Invalid number of argument " + "\"" + argument + "\" !"); } } - + String fullErrorMsg = String.join("\n", errorMsg); if (!isValid) {