From a077d89d4678c341944fab7ad45adf95d6437512 Mon Sep 17 00:00:00 2001 From: harriuscai Date: Thu, 1 Nov 2018 00:43:56 +0800 Subject: [PATCH 1/2] Enhanced module delete to validate module code and check if the module exists --- config.json | 2 +- .../address/logic/commands/ModuleDeleteCommand.java | 12 +++++++++--- .../logic/parser/ModuleDeleteCommandParser.java | 3 ++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/config.json b/config.json index 83099b9889c0..34da19926bb5 100644 --- a/config.json +++ b/config.json @@ -2,4 +2,4 @@ "appTitle" : "Trajectory", "logLevel" : "INFO", "userPrefsFilePath" : "preferences.json" -} +} \ No newline at end of file diff --git a/src/main/java/seedu/address/logic/commands/ModuleDeleteCommand.java b/src/main/java/seedu/address/logic/commands/ModuleDeleteCommand.java index 807ffaad5d1e..3fcd326b044d 100644 --- a/src/main/java/seedu/address/logic/commands/ModuleDeleteCommand.java +++ b/src/main/java/seedu/address/logic/commands/ModuleDeleteCommand.java @@ -7,6 +7,7 @@ import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; import seedu.address.model.module.Module; +import seedu.address.model.module.ModuleCode; import seedu.address.model.module.ModuleManager; /** @@ -24,10 +25,11 @@ public class ModuleDeleteCommand extends Command { + PREFIX_MODULE_CODE + "CS2113"; public static final String MESSAGE_DELETE_MODULE_SUCCESS = "Deleted module: %1$s"; + public static final String MESSAGE_MODULE_NOT_FOUND = "Module with code %s doesn't exist in Trajectory!"; - private final String targetModuleCode; + private final ModuleCode targetModuleCode; - public ModuleDeleteCommand(String moduleCode) { + public ModuleDeleteCommand(ModuleCode moduleCode) { this.targetModuleCode = moduleCode; } @@ -36,7 +38,11 @@ public CommandResult execute(Model model, CommandHistory history) throws Command requireNonNull(model); ModuleManager moduleManager = ModuleManager.getInstance(); - Module moduleToDelete = moduleManager.getModuleByModuleCode(targetModuleCode); + if (!moduleManager.doesModuleExist(targetModuleCode.moduleCode)) { + throw new CommandException(String.format(MESSAGE_MODULE_NOT_FOUND, targetModuleCode.moduleCode)); + } + + Module moduleToDelete = moduleManager.getModuleByModuleCode(targetModuleCode.moduleCode); moduleManager.deleteModule(moduleToDelete); moduleManager.saveModuleList(); diff --git a/src/main/java/seedu/address/logic/parser/ModuleDeleteCommandParser.java b/src/main/java/seedu/address/logic/parser/ModuleDeleteCommandParser.java index ade875f25679..b2491052bd40 100644 --- a/src/main/java/seedu/address/logic/parser/ModuleDeleteCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/ModuleDeleteCommandParser.java @@ -8,6 +8,7 @@ import seedu.address.logic.commands.ModuleDeleteCommand; import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.module.ModuleCode; /** * Parses input arguments and creates a new ModuleDeleteCommand object @@ -29,7 +30,7 @@ public ModuleDeleteCommand parse(String userInput) throws ParseException { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ModuleDeleteCommand.MESSAGE_USAGE)); } - String moduleCode = argMultimap.getValue(PREFIX_MODULE_CODE).get(); + ModuleCode moduleCode = ParserUtil.parseModuleCode(argMultimap.getValue(PREFIX_MODULE_CODE).get()); return new ModuleDeleteCommand(moduleCode); } From cabad2422d1fce21db4f3dc02e94f11cd8aca89e Mon Sep 17 00:00:00 2001 From: Tan Yuan Cai Date: Thu, 1 Nov 2018 00:46:45 +0800 Subject: [PATCH 2/2] Update config.json --- config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.json b/config.json index 34da19926bb5..83099b9889c0 100644 --- a/config.json +++ b/config.json @@ -2,4 +2,4 @@ "appTitle" : "Trajectory", "logLevel" : "INFO", "userPrefsFilePath" : "preferences.json" -} \ No newline at end of file +}