From ea39c3a9eb4e4bf65eceafe4954ca10065d32475 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 30 Oct 2023 18:44:11 +0800 Subject: [PATCH 1/7] Check duplicate task for all children --- .../seedu/address/model/ChildOperation.java | 90 +++++++++++++++---- .../seedu/address/model/IChildOperation.java | 14 +++ 2 files changed, 86 insertions(+), 18 deletions(-) diff --git a/src/main/java/seedu/address/model/ChildOperation.java b/src/main/java/seedu/address/model/ChildOperation.java index 87b7fe82886..5f4b91ec50a 100644 --- a/src/main/java/seedu/address/model/ChildOperation.java +++ b/src/main/java/seedu/address/model/ChildOperation.java @@ -120,40 +120,76 @@ public int numOfChildren() { @Override public void addTaskToAllChildren(Task task, int level) { - List> children = new ArrayList<>(getAllChildren()); - level--; + List> children = getAllTaskListManagerChildrenAtLevel(level); - while (level > 0) { - List> list = new ArrayList<>(); - for (IChildElement child : children) { - if (child instanceof ChildrenAndTaskListManager) { - ChildrenAndTaskListManager ctlm = (ChildrenAndTaskListManager) child; - list.addAll(ctlm.getAllChildren()); - } else { - throw new IllegalArgumentException("Invalid level."); + for (IChildElement child : children) { + Task clonedTask = task.clone(); + if (!(child instanceof TaskListManager) && !(child instanceof ChildrenAndTaskListManager)) { + throw new IllegalArgumentException("All children must be task list manager."); + } + + if (child instanceof TaskListManager) { + TaskListManager tlm = (TaskListManager) child; + if (tlm.contains(task)) { + continue; } + tlm.addTask(clonedTask); + } + + if (child instanceof ChildrenAndTaskListManager) { + ChildrenAndTaskListManager ctlm = (ChildrenAndTaskListManager) child; + if (ctlm.contains(task)) { + continue; + } + ctlm.addTask(clonedTask); } - children = new ArrayList<>(list); - level--; } + } - addAllTaskToAllChildren(task, children); + @Override + public boolean checkIfAllChildrenHaveTask(Task task, int level) { + List> children = getAllTaskListManagerChildrenAtLevel(level); + + for (IChildElement child : children) { + if (child instanceof TaskListManager) { + TaskListManager tlm = (TaskListManager) child; + if (!tlm.contains(task)) { + return false; + } + } else if (child instanceof ChildrenAndTaskListManager) { + ChildrenAndTaskListManager ctlm = (ChildrenAndTaskListManager) child; + if (!ctlm.contains(task)) { + return false; + } + } else { + throw new IllegalArgumentException("All children must be task list manager."); + } + } + + return true; } - private void addAllTaskToAllChildren(Task task, List> children) { + @Override + public boolean checkIfAnyChildHasTask(Task task, int level) { + List> children = getAllTaskListManagerChildrenAtLevel(level); + for (IChildElement child : children) { - logger.info("Adding task to: " + child.toString()); - Task clonedTask = task.clone(); if (child instanceof TaskListManager) { TaskListManager tlm = (TaskListManager) child; - tlm.addTask(clonedTask); + if (tlm.contains(task)) { + return true; + } } else if (child instanceof ChildrenAndTaskListManager) { ChildrenAndTaskListManager ctlm = (ChildrenAndTaskListManager) child; - ctlm.addTask(clonedTask); + if (ctlm.contains(task)) { + return true; + } } else { throw new IllegalArgumentException("All children must be task list manager."); } } + + return false; } @Override @@ -172,4 +208,22 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(baseDir); } + + private List> getAllTaskListManagerChildrenAtLevel(int level) { + List> children = new ArrayList<>(getAllChildren()); + while (--level > 0) { + List> list = new ArrayList<>(); + for (IChildElement child : children) { + if (child instanceof ChildrenAndTaskListManager) { + ChildrenAndTaskListManager ctlm = (ChildrenAndTaskListManager) child; + list.addAll(ctlm.getAllChildren()); + } else { + throw new IllegalArgumentException("Invalid level."); + } + } + children = new ArrayList<>(list); + level--; + } + return children; + } } diff --git a/src/main/java/seedu/address/model/IChildOperation.java b/src/main/java/seedu/address/model/IChildOperation.java index 0d6a60c734e..a231057c680 100644 --- a/src/main/java/seedu/address/model/IChildOperation.java +++ b/src/main/java/seedu/address/model/IChildOperation.java @@ -72,6 +72,20 @@ public interface IChildOperation> { */ void addTaskToAllChildren(Task task, int level); + /** + * Returns {@code true} if all children at {@code level} have the task. + * The {@code level} must be level of task list manager. + * e.g. Group level and student level. + */ + boolean checkIfAllChildrenHaveTask(Task task, int level); + + /** + * Returns {@code true} if at least one child at {@code level} has the task. + * The {@code level} must be level of task list manager. + * e.g. Group level and student level. + */ + boolean checkIfAnyChildHasTask(Task task, int level); + /** * Returns Number of current children * From efdd32a75e5418fce15bd59c4497e0b0e5dc83c6 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 30 Oct 2023 18:44:41 +0800 Subject: [PATCH 2/7] Check duplicate task for bulk task assignment close Fix bug: never check duplicate task for bulk task assignment #191 --- .../java/seedu/address/logic/Messages.java | 2 - .../logic/commands/CreateDeadlineCommand.java | 37 ++++++++++++++++-- .../logic/commands/CreateTodoCommand.java | 39 ++++++++++++++++--- 3 files changed, 68 insertions(+), 10 deletions(-) diff --git a/src/main/java/seedu/address/logic/Messages.java b/src/main/java/seedu/address/logic/Messages.java index 2ce60c6fdba..630003b16f0 100644 --- a/src/main/java/seedu/address/logic/Messages.java +++ b/src/main/java/seedu/address/logic/Messages.java @@ -15,8 +15,6 @@ public class Messages { public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command"; public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s"; - public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid"; - public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!"; public static final String MESSAGE_DUPLICATE_FIELDS = "Multiple values specified for the following single-valued field(s): "; diff --git a/src/main/java/seedu/address/logic/commands/CreateDeadlineCommand.java b/src/main/java/seedu/address/logic/commands/CreateDeadlineCommand.java index ba432803a55..7921a88dd52 100644 --- a/src/main/java/seedu/address/logic/commands/CreateDeadlineCommand.java +++ b/src/main/java/seedu/address/logic/commands/CreateDeadlineCommand.java @@ -36,14 +36,21 @@ public class CreateDeadlineCommand extends Command { public static final String MESSAGE_SUCCESS_ALL_STUDENTS = "New Deadline task added to all students in group: %1$s"; + public static final String MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING = + "Warning: Some student(s) already have the task. \n" + + "New Deadline task has been added to the rest."; public static final String MESSAGE_SUCCESS_ALL_GROUPS = "New Deadline task added to all groups in root: %1$s"; + public static final String MESSAGE_SUCCESS_ALL_GROUPS_WITH_WARNING = + "Warning: Some group(s) already have the task. \n" + + "New Deadline task has been added to the rest."; public static final String MESSAGE_DUPLICATE_DEADLINE_TASK = "This Deadline task has already been allocated"; public static final String MESSAGE_PATH_NOT_FOUND = "Path does not exist in ProfBook."; public static final String MESSAGE_NOT_TASK_MANAGER = "Cannot create task for this path."; public static final String MESSAGE_INVALID_PATH_FOR_ALL_STU = "All stu flag is only allowed for group path"; public static final String MESSAGE_INVALID_PATH_FOR_ALL_GROUP = "All Group flag is only allowed for root path"; + public static final String MESSAGE_ALL_CHILDREN_HAVE_TASK = "All %1$ss already have the task."; private final AbsolutePath path; private final Deadline deadline; @@ -106,9 +113,22 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_STU); } ChildOperation groupOper = model.groupChildOperation(path); + + // Check whether all children already have the task + if (groupOper.checkIfAllChildrenHaveTask(deadline, 1)) { + throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "group")); + } + + // Check whether at least one of the children has the task + boolean warning = false; + if (groupOper.checkIfAnyChildHasTask(deadline, 1)) { + warning = true; + } + groupOper.addTaskToAllChildren(deadline, 1); model.updateList(); - return new CommandResult(MESSAGE_SUCCESS_ALL_STUDENTS); + return new CommandResult( + warning ? MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING : MESSAGE_SUCCESS_ALL_STUDENTS); } if (!path.isRootDirectory()) { @@ -116,10 +136,21 @@ public CommandResult execute(Model model) throws CommandException { } ChildOperation rootOper = model.rootChildOperation(); + + // Check whether all children already have the task + if (rootOper.checkIfAllChildrenHaveTask(deadline, 1)) { + throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "student")); + } + + // Check whether at least one of the children has the task + boolean warning = false; + if (rootOper.checkIfAnyChildHasTask(deadline, 1)) { + warning = true; + } + rootOper.addTaskToAllChildren(deadline, 1); model.updateList(); - - return new CommandResult(MESSAGE_SUCCESS_ALL_GROUPS); + return new CommandResult(warning ? MESSAGE_SUCCESS_ALL_GROUPS_WITH_WARNING : MESSAGE_SUCCESS_ALL_GROUPS); } /** diff --git a/src/main/java/seedu/address/logic/commands/CreateTodoCommand.java b/src/main/java/seedu/address/logic/commands/CreateTodoCommand.java index a5615a7ee1d..32c42e45023 100644 --- a/src/main/java/seedu/address/logic/commands/CreateTodoCommand.java +++ b/src/main/java/seedu/address/logic/commands/CreateTodoCommand.java @@ -29,15 +29,21 @@ public class CreateTodoCommand extends Command { "This ToDo task has already been allocated to this group in ProfBook"; public static final String MESSAGE_SUCCESS_ALL_STUDENTS = "New ToDo task added to all students in group: %1$s"; + public static final String MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING = + "Warning: Some student(s) already have the task. \n" + + "New ToDo task has been added to the rest."; public static final String MESSAGE_SUCCESS_ALL_GROUPS = "New ToDo task added to all groups in root: %1$s"; - + public static final String MESSAGE_SUCCESS_ALL_GROUPS_WITH_WARNING = + "Warning: Some group(s) already have the task. \n" + + "New ToDo task has been added to the rest."; public static final String MESSAGE_ERROR = "Invalid target encountered while creating this todo task"; public static final String MESSAGE_SUCCESS = "New ToDo task has been added to: %1$s"; public static final String MESSAGE_PATH_NOT_FOUND = "Path does not exist in ProfBook."; public static final String MESSAGE_NOT_TASK_MANAGER = "Cannot create task for this path."; public static final String MESSAGE_INVALID_PATH_FOR_ALL_STU = "All stu flag is only allowed for group path"; public static final String MESSAGE_INVALID_PATH_FOR_ALL_GROUP = "All Group flag is only allowed for root path"; + public static final String MESSAGE_ALL_CHILDREN_HAVE_TASK = "All %1$ss already have the task."; public static final String MESSAGE_USAGE = COMMAND_WORD + ": student"; private final AbsolutePath target; @@ -95,20 +101,43 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_STU); } ChildOperation groupOper = model.groupChildOperation(target); + + // Check whether all children already have the task + if (groupOper.checkIfAllChildrenHaveTask(todo, 1)) { + throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "student")); + } + + // Check whether at least one of the children has the task + boolean warning = false; + if (groupOper.checkIfAnyChildHasTask(todo, 1)) { + warning = true; + } + groupOper.addTaskToAllChildren(todo, 1); model.updateList(); - return new CommandResult(MESSAGE_SUCCESS_ALL_STUDENTS); + return new CommandResult( + warning ? MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING : MESSAGE_SUCCESS_ALL_STUDENTS); } if (!target.isRootDirectory()) { throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_GROUP); } ChildOperation rootOper = model.rootChildOperation(); - rootOper.addTaskToAllChildren(todo, 1); - model.updateList(); + // Check whether all children already have the task + if (rootOper.checkIfAllChildrenHaveTask(todo, 1)) { + throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "student")); + } + + // Check whether at least one of the children has the task + boolean warning = false; + if (rootOper.checkIfAnyChildHasTask(todo, 1)) { + warning = true; + } - return new CommandResult(MESSAGE_SUCCESS_ALL_GROUPS); + rootOper.addTaskToAllChildren(todo, 1); + model.updateList(); + return new CommandResult(warning ? MESSAGE_SUCCESS_ALL_GROUPS_WITH_WARNING : MESSAGE_SUCCESS_ALL_GROUPS); } /** From 09a4a31c1905058adb24b5e4cfa57b305f35475c Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 30 Oct 2023 19:32:13 +0800 Subject: [PATCH 3/7] Create Category enum --- src/main/java/seedu/address/logic/commands/Category.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/main/java/seedu/address/logic/commands/Category.java diff --git a/src/main/java/seedu/address/logic/commands/Category.java b/src/main/java/seedu/address/logic/commands/Category.java new file mode 100644 index 00000000000..556afcbb4da --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/Category.java @@ -0,0 +1,8 @@ +package seedu.address.logic.commands; + +/** + * Enum of create task categories. + */ +public enum Category { + NONE, ALLSTU, ALLGRP +} From cb1147e08c816390dd8ca4cefd87881a349b6d70 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 30 Oct 2023 19:32:43 +0800 Subject: [PATCH 4/7] Refactor to use enum Category --- .../logic/parser/CreateDeadlineCommandParser.java | 8 ++++---- .../logic/parser/CreateTodoCommandParser.java | 8 ++++---- .../seedu/address/logic/parser/ParserUtil.java | 15 +++++++++++---- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/CreateDeadlineCommandParser.java b/src/main/java/seedu/address/logic/parser/CreateDeadlineCommandParser.java index 9ab80714312..ae2b1395da9 100644 --- a/src/main/java/seedu/address/logic/parser/CreateDeadlineCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/CreateDeadlineCommandParser.java @@ -7,6 +7,7 @@ import java.time.LocalDateTime; +import seedu.address.logic.commands.Category; import seedu.address.logic.commands.CreateDeadlineCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.path.AbsolutePath; @@ -50,10 +51,9 @@ public CreateDeadlineCommand parse(String args, AbsolutePath currPath) throws Pa Deadline deadline = new Deadline(argMultimap.getValue(OPTION_DESC).get(), by); if (argMultimap.getValue(OPTION_ALL).isEmpty()) { - return new CreateDeadlineCommand(targetPath, deadline); - } else { - String category = ParserUtil.parseCategory(argMultimap.getValue(OPTION_ALL).get()); - return new CreateDeadlineCommand(targetPath, deadline, category); + return new CreateDeadlineCommand(targetPath, deadline, Category.NONE); } + Category category = ParserUtil.parseCategory(argMultimap.getValue(OPTION_ALL).get()); + return new CreateDeadlineCommand(targetPath, deadline, category); } } diff --git a/src/main/java/seedu/address/logic/parser/CreateTodoCommandParser.java b/src/main/java/seedu/address/logic/parser/CreateTodoCommandParser.java index fcb7d9fb71d..ddffdad4fa3 100644 --- a/src/main/java/seedu/address/logic/parser/CreateTodoCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/CreateTodoCommandParser.java @@ -4,6 +4,7 @@ import static seedu.address.logic.parser.CliSyntax.OPTION_ALL; import static seedu.address.logic.parser.CliSyntax.OPTION_DESC; +import seedu.address.logic.commands.Category; import seedu.address.logic.commands.CreateTodoCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.path.AbsolutePath; @@ -47,10 +48,9 @@ public CreateTodoCommand parse(String args, AbsolutePath currPath) throws ParseE ToDo todo = new ToDo(argMultimap.getValue(OPTION_DESC).get()); if (argMultimap.getValue(OPTION_ALL).isEmpty()) { - return new CreateTodoCommand(targetPath, todo); - } else { - String category = ParserUtil.parseCategory(argMultimap.getValue(OPTION_ALL).get()); - return new CreateTodoCommand(targetPath, todo, category); + return new CreateTodoCommand(targetPath, todo, Category.NONE); } + Category category = ParserUtil.parseCategory(argMultimap.getValue(OPTION_ALL).get()); + return new CreateTodoCommand(targetPath, todo, category); } } diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index d7e21718ff2..461f2c5d7c5 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -10,6 +10,7 @@ import seedu.address.commons.core.index.Index; import seedu.address.commons.util.StringUtil; +import seedu.address.logic.commands.Category; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.id.GroupId; import seedu.address.model.id.StudentId; @@ -226,12 +227,18 @@ public static boolean isOptionPresent(ArgumentMultimap argumentMultimap, Option * * @throws ParseException if the given {@code cat} is invalid. */ - public static String parseCategory(String cat) throws ParseException { + public static Category parseCategory(String cat) throws ParseException { requireNonNull(cat); String trimmedCat = cat.trim(); - if (!(cat.equals("allStu")) && !(cat.equals("allGrp"))) { - throw new ParseException("Format is invalid. Should be allStu or allGrp"); + + if (trimmedCat.equals("allStu")) { + return Category.ALLSTU; + } + + if (trimmedCat.equals("allGrp")) { + return Category.ALLGRP; } - return new String(trimmedCat); + + throw new ParseException("Format is invalid. Should be allStu or allGrp"); } } From 90442af0bad3c7f378205288292e53f1b79e59ca Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 30 Oct 2023 19:33:01 +0800 Subject: [PATCH 5/7] Allow create task to allStu for root directory --- .../logic/commands/CreateDeadlineCommand.java | 88 ++++++++++++------- .../logic/commands/CreateTodoCommand.java | 84 ++++++++++++------ 2 files changed, 111 insertions(+), 61 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/CreateDeadlineCommand.java b/src/main/java/seedu/address/logic/commands/CreateDeadlineCommand.java index 7921a88dd52..bfd69dc090c 100644 --- a/src/main/java/seedu/address/logic/commands/CreateDeadlineCommand.java +++ b/src/main/java/seedu/address/logic/commands/CreateDeadlineCommand.java @@ -48,28 +48,19 @@ public class CreateDeadlineCommand extends Command { "This Deadline task has already been allocated"; public static final String MESSAGE_PATH_NOT_FOUND = "Path does not exist in ProfBook."; public static final String MESSAGE_NOT_TASK_MANAGER = "Cannot create task for this path."; - public static final String MESSAGE_INVALID_PATH_FOR_ALL_STU = "All stu flag is only allowed for group path"; - public static final String MESSAGE_INVALID_PATH_FOR_ALL_GROUP = "All Group flag is only allowed for root path"; + public static final String MESSAGE_INVALID_PATH_FOR_ALL_STU = "AllStu flag is only allowed for root and group path"; + public static final String MESSAGE_INVALID_PATH_FOR_ALL_GROUP = "AllGrp flag is only allowed for root path"; public static final String MESSAGE_ALL_CHILDREN_HAVE_TASK = "All %1$ss already have the task."; private final AbsolutePath path; private final Deadline deadline; - private String category = null; - - /** - * Creates an CreateDeadlineCommand to add the Deadline Task for a specified {@code Student} or {@code Group} - */ - public CreateDeadlineCommand(AbsolutePath path, Deadline deadline) { - requireAllNonNull(path, deadline); - this.path = path; - this.deadline = deadline; - } + private Category category; /** * Creates an CreateDeadlineCommand to add the Deadline Task for a specified {@code Student} or {@code Group} * User has input a category as well. */ - public CreateDeadlineCommand(AbsolutePath path, Deadline deadline, String category) { + public CreateDeadlineCommand(AbsolutePath path, Deadline deadline, Category category) { requireAllNonNull(path, deadline, category); this.path = path; this.deadline = deadline; @@ -89,34 +80,46 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_PATH_NOT_FOUND); } - if (this.category == null) { - // Check target path is task manager - if (!model.hasTaskListInPath(path)) { - throw new CommandException(MESSAGE_NOT_TASK_MANAGER); - } + switch(category) { + case NONE: + return handleNone(model); + case ALLSTU: + return handleAllStu(model); + default: + return handleAllGrp(model); + } + } - TaskOperation target = model.taskOperation(path); + private CommandResult handleNone(Model model) throws CommandException { + // Check target path is task manager + if (!model.hasTaskListInPath(path)) { + throw new CommandException(MESSAGE_NOT_TASK_MANAGER); + } - // Check duplicate deadline - if (target.hasTask(this.deadline)) { - throw new CommandException(MESSAGE_DUPLICATE_DEADLINE_TASK); - } + TaskOperation target = model.taskOperation(path); - target.addTask(this.deadline); - model.updateList(); + // Check duplicate deadline + if (target.hasTask(this.deadline)) { + throw new CommandException(MESSAGE_DUPLICATE_DEADLINE_TASK); + } + + target.addTask(this.deadline); + model.updateList(); - return new CommandResult(String.format(MESSAGE_SUCCESS, this.deadline)); + return new CommandResult(String.format(MESSAGE_SUCCESS, this.deadline)); + } + + private CommandResult handleAllStu(Model model) throws CommandException { + if (path.isStudentDirectory()) { + throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_STU); } - if (this.category.equals("allStu")) { - if (!path.isGroupDirectory()) { - throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_STU); - } + if (path.isGroupDirectory()) { ChildOperation groupOper = model.groupChildOperation(path); // Check whether all children already have the task if (groupOper.checkIfAllChildrenHaveTask(deadline, 1)) { - throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "group")); + throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "student")); } // Check whether at least one of the children has the task @@ -131,6 +134,27 @@ public CommandResult execute(Model model) throws CommandException { warning ? MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING : MESSAGE_SUCCESS_ALL_STUDENTS); } + ChildOperation operation = model.rootChildOperation(); + + // Check whether all children already have the task + if (operation.checkIfAllChildrenHaveTask(deadline, 2)) { + throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "student")); + } + + // Check whether at least one of the children has the task + boolean warning = false; + if (operation.checkIfAnyChildHasTask(deadline, 2)) { + warning = true; + } + + operation.addTaskToAllChildren(deadline, 2); + model.updateList(); + return new CommandResult( + warning ? MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING : MESSAGE_SUCCESS_ALL_STUDENTS); + + } + + private CommandResult handleAllGrp(Model model) throws CommandException { if (!path.isRootDirectory()) { throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_GROUP); } @@ -139,7 +163,7 @@ public CommandResult execute(Model model) throws CommandException { // Check whether all children already have the task if (rootOper.checkIfAllChildrenHaveTask(deadline, 1)) { - throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "student")); + throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "group")); } // Check whether at least one of the children has the task diff --git a/src/main/java/seedu/address/logic/commands/CreateTodoCommand.java b/src/main/java/seedu/address/logic/commands/CreateTodoCommand.java index 32c42e45023..099f6757df0 100644 --- a/src/main/java/seedu/address/logic/commands/CreateTodoCommand.java +++ b/src/main/java/seedu/address/logic/commands/CreateTodoCommand.java @@ -41,26 +41,14 @@ public class CreateTodoCommand extends Command { public static final String MESSAGE_SUCCESS = "New ToDo task has been added to: %1$s"; public static final String MESSAGE_PATH_NOT_FOUND = "Path does not exist in ProfBook."; public static final String MESSAGE_NOT_TASK_MANAGER = "Cannot create task for this path."; - public static final String MESSAGE_INVALID_PATH_FOR_ALL_STU = "All stu flag is only allowed for group path"; - public static final String MESSAGE_INVALID_PATH_FOR_ALL_GROUP = "All Group flag is only allowed for root path"; + public static final String MESSAGE_INVALID_PATH_FOR_ALL_STU = "AllStu flag is only allowed for root and group path"; + public static final String MESSAGE_INVALID_PATH_FOR_ALL_GROUP = "AllGrp flag is only allowed for root path"; public static final String MESSAGE_ALL_CHILDREN_HAVE_TASK = "All %1$ss already have the task."; public static final String MESSAGE_USAGE = COMMAND_WORD + ": student"; private final AbsolutePath target; private final ToDo todo; - private String category = null; - - /** - * Constructs a {@code CreateTodoCommand} with the specified relative path and "ToDo" task details. - * - * @param relativePath The relative path to the group where the "ToDo" task will be added. - * @param todo The details of the "ToDo" task to be created. - */ - public CreateTodoCommand(AbsolutePath target, ToDo todo) { - requireAllNonNull(target, todo); - this.target = target; - this.todo = todo; - } + private Category category; /** * Constructs a {@code CreateTodoCommand} with the specified absolute path and "ToDo" task details. @@ -69,7 +57,7 @@ public CreateTodoCommand(AbsolutePath target, ToDo todo) { * @param todo The details of the "ToDo" task to be created. * @param category The specific category of people to add ToDo task to each. */ - public CreateTodoCommand(AbsolutePath target, ToDo todo, String category) { + public CreateTodoCommand(AbsolutePath target, ToDo todo, Category category) { requireAllNonNull(target, todo, category); this.target = target; this.todo = todo; @@ -86,20 +74,38 @@ public CreateTodoCommand(AbsolutePath target, ToDo todo, String category) { @Override public CommandResult execute(Model model) throws CommandException { requireNonNull(model); - if (this.category == null) { - TaskOperation taskOperation = model.taskOperation(target); - if (taskOperation.hasTask(this.todo)) { - throw new CommandException(MESSAGE_DUPLICATE_TODO_TASK_STUDENT); - } - taskOperation.addTask(this.todo); - model.updateList(); - return new CommandResult(String.format(MESSAGE_SUCCESS, target)); + + switch(category) { + case NONE: + return handleNone(model); + case ALLSTU: + return handleAllStu(model); + default: + return handleAllGrp(model); } + } - if (this.category.equals("allStu")) { - if (!target.isGroupDirectory()) { - throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_STU); - } + private CommandResult handleNone(Model model) throws CommandException { + // Check path exists in ProfBook + if (!model.hasPath(target)) { + throw new CommandException(MESSAGE_PATH_NOT_FOUND); + } + + TaskOperation taskOperation = model.taskOperation(target); + if (taskOperation.hasTask(this.todo)) { + throw new CommandException(MESSAGE_DUPLICATE_TODO_TASK_STUDENT); + } + taskOperation.addTask(this.todo); + model.updateList(); + return new CommandResult(String.format(MESSAGE_SUCCESS, target)); + } + + private CommandResult handleAllStu(Model model) throws CommandException { + if (target.isStudentDirectory()) { + throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_STU); + } + + if (target.isGroupDirectory()) { ChildOperation groupOper = model.groupChildOperation(target); // Check whether all children already have the task @@ -119,6 +125,26 @@ public CommandResult execute(Model model) throws CommandException { warning ? MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING : MESSAGE_SUCCESS_ALL_STUDENTS); } + ChildOperation operation = model.rootChildOperation(); + + // Check whether all children already have the task + if (operation.checkIfAllChildrenHaveTask(todo, 2)) { + throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "student")); + } + + // Check whether at least one of the children has the task + boolean warning = false; + if (operation.checkIfAnyChildHasTask(todo, 2)) { + warning = true; + } + + operation.addTaskToAllChildren(todo, 2); + model.updateList(); + return new CommandResult( + warning ? MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING : MESSAGE_SUCCESS_ALL_STUDENTS); + } + + private CommandResult handleAllGrp(Model model) throws CommandException { if (!target.isRootDirectory()) { throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_GROUP); } @@ -126,7 +152,7 @@ public CommandResult execute(Model model) throws CommandException { // Check whether all children already have the task if (rootOper.checkIfAllChildrenHaveTask(todo, 1)) { - throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "student")); + throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "group")); } // Check whether at least one of the children has the task From fe50a330bbf58e740164ad1269b69b4c7888b740 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 30 Oct 2023 19:39:21 +0800 Subject: [PATCH 6/7] Fix checkstyle error --- .../java/seedu/address/logic/commands/CreateDeadlineCommand.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/seedu/address/logic/commands/CreateDeadlineCommand.java b/src/main/java/seedu/address/logic/commands/CreateDeadlineCommand.java index bfd69dc090c..142e4f69479 100644 --- a/src/main/java/seedu/address/logic/commands/CreateDeadlineCommand.java +++ b/src/main/java/seedu/address/logic/commands/CreateDeadlineCommand.java @@ -151,7 +151,6 @@ private CommandResult handleAllStu(Model model) throws CommandException { model.updateList(); return new CommandResult( warning ? MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING : MESSAGE_SUCCESS_ALL_STUDENTS); - } private CommandResult handleAllGrp(Model model) throws CommandException { From b4aed85ff09e8d3daee171d48b99d207b03c1096 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 30 Oct 2023 19:39:26 +0800 Subject: [PATCH 7/7] Fix test cases --- .../commands/CreateDeadlineCommandTest.java | 22 +++++++++++-------- .../logic/commands/CreateTodoCommandTest.java | 18 +++++++-------- .../CreateDeadlineCommandParserTest.java | 7 ++++-- .../parser/CreateTodoCommandParserTest.java | 7 ++++-- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/test/java/seedu/address/logic/commands/CreateDeadlineCommandTest.java b/src/test/java/seedu/address/logic/commands/CreateDeadlineCommandTest.java index b53cd333884..e3847b909e7 100644 --- a/src/test/java/seedu/address/logic/commands/CreateDeadlineCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/CreateDeadlineCommandTest.java @@ -43,7 +43,7 @@ public void setup() { @Test public void constructor_nullArgs_throwsNullPointerException() { - assertThrows(NullPointerException.class, () -> new CreateDeadlineCommand(null, null)); + assertThrows(NullPointerException.class, () -> new CreateDeadlineCommand(null, null, null)); } @Test @@ -60,7 +60,7 @@ public void execute_deadlineForStudentAccepted_addSuccessful() throws InvalidPat TaskOperation operation = expectedModel.taskOperation(absoluteTargetPath); operation.addTask(toBeAdded); - CreateDeadlineCommand command = new CreateDeadlineCommand(absoluteTargetPath, toBeAdded); + CreateDeadlineCommand command = new CreateDeadlineCommand(absoluteTargetPath, toBeAdded, Category.NONE); String expectedMessage = String.format(CreateDeadlineCommand.MESSAGE_SUCCESS, toBeAdded); @@ -78,7 +78,7 @@ public void execute_deadlineForAllStudentsInGroupAccepted_addSuccessful() ChildOperation operation = expectedModel.groupChildOperation(absoluteTargetPath); operation.addTaskToAllChildren(toBeAdded, 1); - CreateDeadlineCommand command = new CreateDeadlineCommand(absoluteTargetPath, toBeAdded, "allStu"); + CreateDeadlineCommand command = new CreateDeadlineCommand(absoluteTargetPath, toBeAdded, Category.ALLSTU); String expectedMessage = CreateDeadlineCommand.MESSAGE_SUCCESS_ALL_STUDENTS; assertCommandSuccess(command, model, expectedMessage, expectedModel); @@ -90,7 +90,7 @@ public void execute_deadlineForAllGroupsInRootAccepted_addSuccessful() ChildOperation operation = expectedModel.rootChildOperation(); operation.addTaskToAllChildren(toBeAdded, 1); - CreateDeadlineCommand command = new CreateDeadlineCommand(rootPath, toBeAdded, "allGrp"); + CreateDeadlineCommand command = new CreateDeadlineCommand(rootPath, toBeAdded, Category.ALLGRP); String expectedMessage = CreateDeadlineCommand.MESSAGE_SUCCESS_ALL_GROUPS; assertCommandSuccess(command, model, expectedMessage, expectedModel); @@ -109,7 +109,7 @@ public void execute_duplicateDeadline_throwsCommandException() throws InvalidPat TaskOperation operation = model.taskOperation(absoluteTargetPath); operation.addTask(toBeAdded); - CreateDeadlineCommand command = new CreateDeadlineCommand(absoluteTargetPath, toBeAdded); + CreateDeadlineCommand command = new CreateDeadlineCommand(absoluteTargetPath, toBeAdded, Category.NONE); String expectedMessage = CreateDeadlineCommand.MESSAGE_DUPLICATE_DEADLINE_TASK; assertCommandFailure(command, model, expectedMessage); @@ -117,14 +117,17 @@ public void execute_duplicateDeadline_throwsCommandException() throws InvalidPat @Test public void equals() { - CreateDeadlineCommand createDeadlineCommand1 = new CreateDeadlineCommand(rootPath, TypicalTasks.DEADLINE_1); - CreateDeadlineCommand createDeadlineCommand2 = new CreateDeadlineCommand(rootPath, TypicalTasks.DEADLINE_2); + CreateDeadlineCommand createDeadlineCommand1 = new CreateDeadlineCommand( + rootPath, TypicalTasks.DEADLINE_1, Category.NONE); + CreateDeadlineCommand createDeadlineCommand2 = new CreateDeadlineCommand( + rootPath, TypicalTasks.DEADLINE_2, Category.NONE); // same object -> returns true assertEquals(createDeadlineCommand1, createDeadlineCommand1); // same values -> returns true - CreateDeadlineCommand createDeadlineCommand1Copy = new CreateDeadlineCommand(rootPath, TypicalTasks.DEADLINE_1); + CreateDeadlineCommand createDeadlineCommand1Copy = new CreateDeadlineCommand( + rootPath, TypicalTasks.DEADLINE_1, Category.NONE); assertEquals(createDeadlineCommand1, createDeadlineCommand1Copy); // different types -> returns false @@ -139,7 +142,8 @@ public void equals() { @Test public void toStringMethod() { - CreateDeadlineCommand createDeadlineCommand = new CreateDeadlineCommand(rootPath, TypicalTasks.DEADLINE_1); + CreateDeadlineCommand createDeadlineCommand = new CreateDeadlineCommand( + rootPath, TypicalTasks.DEADLINE_1, Category.NONE); String expected = CreateDeadlineCommand.class.getCanonicalName() + "{toCreateDeadline=" + TypicalTasks.DEADLINE_1 + "}"; assertEquals(expected, createDeadlineCommand.toString()); diff --git a/src/test/java/seedu/address/logic/commands/CreateTodoCommandTest.java b/src/test/java/seedu/address/logic/commands/CreateTodoCommandTest.java index 3d943ddfa46..f5f02a0be33 100644 --- a/src/test/java/seedu/address/logic/commands/CreateTodoCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/CreateTodoCommandTest.java @@ -48,7 +48,7 @@ public void setup() { @Test public void constructor_nullRelativePathAndTodo_throwsNullPointerException() { - assertThrows(NullPointerException.class, () -> new CreateTodoCommand(null, null)); + assertThrows(NullPointerException.class, () -> new CreateTodoCommand(null, null, null)); } @Test @@ -62,7 +62,7 @@ public void execute_todoForAllStudentsInGroupAccepted_addSuccessful() ChildOperation operation = expectedModel.groupChildOperation(absoluteTargetPath); operation.addTaskToAllChildren(toBeAdded, 1); - CreateTodoCommand command = new CreateTodoCommand(absoluteTargetPath, toBeAdded, "allStu"); + CreateTodoCommand command = new CreateTodoCommand(absoluteTargetPath, toBeAdded, Category.ALLSTU); String expectedMessage = MESSAGE_SUCCESS_ALL_STUDENTS; assertCommandSuccess(command, model, expectedMessage, expectedModel); @@ -74,7 +74,7 @@ public void execute_deadlineForAllGroupsInRootAccepted_addSuccessful() ChildOperation operation = expectedModel.rootChildOperation(); operation.addTaskToAllChildren(toBeAdded, 1); - CreateTodoCommand command = new CreateTodoCommand(rootPath, toBeAdded, "allGrp"); + CreateTodoCommand command = new CreateTodoCommand(rootPath, toBeAdded, Category.ALLGRP); String expectedMessage = MESSAGE_SUCCESS_ALL_GROUPS; assertCommandSuccess(command, model, expectedMessage, expectedModel); @@ -94,7 +94,7 @@ public void execute_createTodoTask_success() throws CommandException, InvalidPat TaskOperation operation = expectedModel.taskOperation(absoluteTargetPath); operation.addTask(toBeAdded); - CreateTodoCommand command = new CreateTodoCommand(absoluteTargetPath, toBeAdded); + CreateTodoCommand command = new CreateTodoCommand(absoluteTargetPath, toBeAdded, Category.NONE); String expectedMessage = String.format(MESSAGE_SUCCESS, absoluteTargetPath); assertCommandSuccess(command, model, expectedMessage, expectedModel); @@ -114,7 +114,7 @@ public void execute_duplicateTodoTask_throwCommandException() throws InvalidPath TaskOperation operation = model.taskOperation(absoluteTargetPath); operation.addTask(toBeAdded); - CreateTodoCommand command = new CreateTodoCommand(absoluteTargetPath, toBeAdded); + CreateTodoCommand command = new CreateTodoCommand(absoluteTargetPath, toBeAdded, Category.NONE); String expectedMessage = MESSAGE_DUPLICATE_TODO_TASK_STUDENT; assertCommandFailure(command, model, expectedMessage); @@ -122,14 +122,14 @@ public void execute_duplicateTodoTask_throwCommandException() throws InvalidPath @Test public void equals() { - CreateTodoCommand createTodoCommand1 = new CreateTodoCommand(rootPath, TODO_1); - CreateTodoCommand createTodoCommand2 = new CreateTodoCommand(rootPath, TODO_2); + CreateTodoCommand createTodoCommand1 = new CreateTodoCommand(rootPath, TODO_1, Category.NONE); + CreateTodoCommand createTodoCommand2 = new CreateTodoCommand(rootPath, TODO_2, Category.NONE); // same object -> returns true assertEquals(createTodoCommand1, createTodoCommand1); // same values -> returns true - CreateTodoCommand createDeadlineCommand1Copy = new CreateTodoCommand(rootPath, TODO_1); + CreateTodoCommand createDeadlineCommand1Copy = new CreateTodoCommand(rootPath, TODO_1, Category.NONE); assertEquals(createTodoCommand1, createDeadlineCommand1Copy); // different types -> returns false @@ -144,7 +144,7 @@ public void equals() { @Test public void toStringMethod() { - CreateTodoCommand createDeadlineCommand = new CreateTodoCommand(rootPath, TODO_1); + CreateTodoCommand createDeadlineCommand = new CreateTodoCommand(rootPath, TODO_1, Category.NONE); String expected = CreateTodoCommand.class.getCanonicalName() + "{toCreateTodo=" + TODO_1 + "}"; assertEquals(expected, createDeadlineCommand.toString()); diff --git a/src/test/java/seedu/address/logic/parser/CreateDeadlineCommandParserTest.java b/src/test/java/seedu/address/logic/parser/CreateDeadlineCommandParserTest.java index f62a021b0e1..99e668779ed 100644 --- a/src/test/java/seedu/address/logic/parser/CreateDeadlineCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/CreateDeadlineCommandParserTest.java @@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test; +import seedu.address.logic.commands.Category; import seedu.address.logic.commands.CommandTestUtil; import seedu.address.logic.commands.CreateDeadlineCommand; import seedu.address.model.task.Deadline; @@ -23,7 +24,8 @@ public void parse_allFieldsPresent_success() { CommandTestUtil.getValidRootAbsolutePath(), new CreateDeadlineCommand( CommandTestUtil.getValidGroupAbsolutePath(), - new Deadline(VALID_TASK_DESC, CommandTestUtil.getValidDateTime()))); + new Deadline(VALID_TASK_DESC, CommandTestUtil.getValidDateTime()), + Category.NONE)); } @Test @@ -33,6 +35,7 @@ public void parse_allFieldsPresentWithCategory_success() { CommandTestUtil.getValidRootAbsolutePath(), new CreateDeadlineCommand( CommandTestUtil.getValidGroupAbsolutePath(), - new Deadline(VALID_TASK_DESC, CommandTestUtil.getValidDateTime()), "allStu")); + new Deadline(VALID_TASK_DESC, CommandTestUtil.getValidDateTime()), + Category.ALLSTU)); } } diff --git a/src/test/java/seedu/address/logic/parser/CreateTodoCommandParserTest.java b/src/test/java/seedu/address/logic/parser/CreateTodoCommandParserTest.java index 1eecaad24c5..3964336b11c 100644 --- a/src/test/java/seedu/address/logic/parser/CreateTodoCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/CreateTodoCommandParserTest.java @@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test; +import seedu.address.logic.commands.Category; import seedu.address.logic.commands.CommandTestUtil; import seedu.address.logic.commands.CreateTodoCommand; import seedu.address.model.task.ToDo; @@ -20,7 +21,8 @@ public void parse_allFieldsPresent_success() { assertParseSuccess(parser, VALID_STUDENT_DIR_PREAMBLE + TASK_DESC_DESC, CommandTestUtil.getValidRootAbsolutePath(), - new CreateTodoCommand(CommandTestUtil.getValidStudentAbsolutePath(), new ToDo(VALID_TASK_DESC))); + new CreateTodoCommand(CommandTestUtil.getValidStudentAbsolutePath(), new ToDo(VALID_TASK_DESC), + Category.NONE)); } @Test @@ -29,6 +31,7 @@ public void parse_allFieldsPresentWithCatergory_success() { VALID_STUDENT_DIR_PREAMBLE + TASK_DESC_DESC + VALID_CATEGORY_STUDENT, CommandTestUtil.getValidRootAbsolutePath(), new CreateTodoCommand(CommandTestUtil.getValidStudentAbsolutePath(), - new ToDo(VALID_TASK_DESC), "allStu")); + new ToDo(VALID_TASK_DESC), + Category.ALLSTU)); } }