From c62ccc64589ef6483f3153904595a0f4d38ae030 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Thu, 2 Nov 2023 02:03:03 +0800 Subject: [PATCH 1/9] Add interface for TaskListManager --- .../address/model/task/ITaskListManager.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/main/java/seedu/address/model/task/ITaskListManager.java diff --git a/src/main/java/seedu/address/model/task/ITaskListManager.java b/src/main/java/seedu/address/model/task/ITaskListManager.java new file mode 100644 index 00000000000..77d4a9abf7f --- /dev/null +++ b/src/main/java/seedu/address/model/task/ITaskListManager.java @@ -0,0 +1,51 @@ +package seedu.address.model.task; + +import java.util.List; + +import seedu.address.model.task.exceptions.NoSuchTaskException; + +/** + * API for task list manager + */ +public interface ITaskListManager extends ReadOnlyTaskList { + /** + * Adds a task to the task list. + * + * @param t The task to be added. + */ + void addTask(Task t); + /** + * Deletes a task from the task list. + * + * @param index The index of the task to be deleted. + * @return The deleted task. + * @throws NoSuchTaskException If there are no tasks at this level or taskNumber provided is too large. + */ + Task deleteTask(int index) throws NoSuchTaskException; + /** + * Marks a task as done. + * + * @param index The index of the task to be marked. + * @return The marked task. + * @throws NoSuchTaskException If there are no tasks at this level or taskNumber provided is too large. + */ + Task markTask(int index) throws NoSuchTaskException; + + /** + * Unmarks a task. + * + * @param index The number of the task to be unmarked. + * @return The unmarked task. + * @throws NoSuchTaskException If there are no tasks at this level or taskNumber provided is too large. + */ + Task unmarkTask(int index) throws NoSuchTaskException; + + /** + * Finds tasks that match the given query. + * + * @param query The query to match. + * @return A list of tasks that match the query. + * @throws NoSuchTaskException If there are no tasks at this level. + */ + List findTask(String query) throws NoSuchTaskException; +} From c62b4c70a04d6d9e14c0f28fc89a735d9ee39378 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Thu, 2 Nov 2023 02:03:21 +0800 Subject: [PATCH 2/9] Add interface for ChildrenManager --- .../model/profbook/IChildrenManager.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/main/java/seedu/address/model/profbook/IChildrenManager.java diff --git a/src/main/java/seedu/address/model/profbook/IChildrenManager.java b/src/main/java/seedu/address/model/profbook/IChildrenManager.java new file mode 100644 index 00000000000..1a798da1108 --- /dev/null +++ b/src/main/java/seedu/address/model/profbook/IChildrenManager.java @@ -0,0 +1,64 @@ +package seedu.address.model.profbook; + +import java.util.List; +import java.util.Map; + +import seedu.address.model.id.Id; +import seedu.address.model.profbook.exceptions.DuplicateChildException; +import seedu.address.model.profbook.exceptions.NoSuchChildException; + +/** + * API for Children Manager + */ +public interface IChildrenManager> { + /** + * Adds the child to list of children + * + * @param id - Unique identifier of the child + * @param child - The child in question + * @throws DuplicateChildException If attempting to add child with the same ID + */ + void addChild(Id id, T child) throws DuplicateChildException; + + /** + * Deletes the child specified by the id + * + * @param id - Unique identifier of the child + * @return The deleted Child + * @throws NoSuchChildException If there is no such Child found + */ + T deleteChild(Id id) throws NoSuchChildException; + + /** + * Checks if the child is present + * + * @param id - Unique identifier of the child + * @return true if the child is present + */ + boolean hasChild(Id id); + + /** + * Returns the child specified by the id + * + * @param id - Unique identifier of the child + * @return The specified child + * @throws NoSuchChildException If there is no such Child found + */ + T getChild(Id id) throws NoSuchChildException; + + /** + * Returns number of current children + */ + int numOfChildren(); + + /** + * Returns a list of all current children + */ + List getAllChildren(); + + /** + * Returns children map. + * + */ + Map getChildren(); +} From 6f3da6c3ccc381f4350b01ddd97710bd5bb79f0d Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Thu, 2 Nov 2023 02:03:56 +0800 Subject: [PATCH 3/9] Change ReadOnlyTaskList to interface --- .../address/model/task/ReadOnlyTaskList.java | 88 ++----------------- 1 file changed, 7 insertions(+), 81 deletions(-) diff --git a/src/main/java/seedu/address/model/task/ReadOnlyTaskList.java b/src/main/java/seedu/address/model/task/ReadOnlyTaskList.java index cc14a2942ed..cd6a8fd0f20 100644 --- a/src/main/java/seedu/address/model/task/ReadOnlyTaskList.java +++ b/src/main/java/seedu/address/model/task/ReadOnlyTaskList.java @@ -1,6 +1,5 @@ package seedu.address.model.task; -import java.util.ArrayList; import java.util.List; import seedu.address.model.task.exceptions.NoSuchTaskException; @@ -9,64 +8,21 @@ /** * Encapsulates logic of a TaskList */ -public class ReadOnlyTaskList { - - protected final List taskList; - - /** - * Constructs a new {@code ReadOnlyTaskList}. - */ - public ReadOnlyTaskList() { - this.taskList = new ArrayList<>(); - } - - /** - * Constructs a {@code ReadOnlyTaskList} using the given task list. - * - * @param taskList A valid task list. - */ - public ReadOnlyTaskList(List taskList) { - this.taskList = new ArrayList<>(taskList); - } - - /** - * Contructs a {@code ReadOnlyTaskList} using the data in {@code toBeCopied}. - */ - public ReadOnlyTaskList(ReadOnlyTaskList toBeCopied) { - this(toBeCopied.taskList); - } - +public interface ReadOnlyTaskList { /** * Checks if index is between 0 and task list size. */ - public boolean isValidIndex(int index) { - return index > 0 && index <= taskList.size(); - } - - /** - * Checks if index is vaild. - * @throws NoSuchTaskException if index given is invalid. - */ - protected void verifyIsValidIndex(int index) throws NoSuchTaskException { - if (!isValidIndex(index)) { - throw new NoSuchTaskException("Index " + index - + " is invalid for task list with size " + size()); - } - } + boolean isValidIndex(int index); /** * Returns task list size. */ - public int size() { - return taskList.size(); - } + int size(); /** * Returns true if task list is empty. */ - public boolean isEmpty() { - return taskList.isEmpty(); - } + boolean isEmpty(); /** * Retrieves a task from the task list. @@ -75,11 +31,7 @@ public boolean isEmpty() { * @return The retrieved task. * @throws NoSuchTaskException If the index is out of bounds. */ - public Task getTask(int index) throws NoSuchTaskException { - verifyIsValidIndex(index); - Task task = this.taskList.get(index - 1); - return task; - } + Task getTask(int index) throws NoSuchTaskException; /** * Checks if the task list contains a duplicate of the specified task. @@ -87,38 +39,12 @@ public Task getTask(int index) throws NoSuchTaskException { * @param t the task to be checked for duplication in the task list * @return true if a duplicate of the specified task is found in the task list, false otherwise */ - public boolean contains(Task t) { - for (Task check : this.taskList) { - if (check.equals(t)) { - return true; - } - } - return false; - } + boolean contains(Task t); /** * Retrieves all tasks from the task list. * * @return A list of all tasks. */ - public List getAllTasks() { - return new ArrayList<>(this.taskList); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - ReadOnlyTaskList taskList1 = (ReadOnlyTaskList) o; - return this.taskList.equals(taskList1.taskList); - } - - @Override - public String toString() { - return this.taskList.toString(); - } + List getAllTasks(); } From 1e435ae7cd59c7f5cb39bf3d0d527ac53a3de5b7 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Thu, 2 Nov 2023 02:04:30 +0800 Subject: [PATCH 4/9] Refactor with new interfaces --- .../profbook/ChildrenAndTaskListManager.java | 125 ++++++------------ 1 file changed, 38 insertions(+), 87 deletions(-) diff --git a/src/main/java/seedu/address/model/profbook/ChildrenAndTaskListManager.java b/src/main/java/seedu/address/model/profbook/ChildrenAndTaskListManager.java index d64f5a451d9..3edad5fda86 100644 --- a/src/main/java/seedu/address/model/profbook/ChildrenAndTaskListManager.java +++ b/src/main/java/seedu/address/model/profbook/ChildrenAndTaskListManager.java @@ -7,6 +7,7 @@ import seedu.address.model.id.Id; import seedu.address.model.profbook.exceptions.DuplicateChildException; import seedu.address.model.profbook.exceptions.NoSuchChildException; +import seedu.address.model.task.ITaskListManager; import seedu.address.model.task.ReadOnlyTaskList; import seedu.address.model.task.Task; import seedu.address.model.task.TaskListManager; @@ -15,7 +16,8 @@ /** * A child element that is both Children and TaskList Manager. */ -public abstract class ChildrenAndTaskListManager> implements IChildElement { +public abstract class ChildrenAndTaskListManager> + implements IChildElement, IChildrenManager, ITaskListManager { private ChildrenManager childrenManager; private TaskListManager taskListManager; @@ -32,7 +34,7 @@ public ChildrenAndTaskListManager() { */ public ChildrenAndTaskListManager(Map children, ReadOnlyTaskList taskList) { childrenManager = new ChildrenManager<>(children); - taskListManager = new TaskListManager(taskList); + taskListManager = new TaskListManager(taskList.getAllTasks()); } /** @@ -53,149 +55,98 @@ public TaskListManager getTaskListManager() { //=========== Children Manager ================================================================================== - /** - * Adds the child to list of children - * - * @param id - Unique identifier of the child - * @param child - The child in question - * @throws DuplicateChildException If attempting to add child with the same ID - */ + @Override public void addChild(Id id, T child) throws DuplicateChildException { childrenManager.addChild(id, child); } - /** - * Deletes the child specified by the id - * - * @param id - Unique identifier of the child - * @return The deleted Child - * @throws NoSuchChildException If there is no such Child found - */ + @Override public T deleteChild(Id id) throws NoSuchChildException { return childrenManager.deleteChild(id); } - /** - * Checks if the child is present - * - * @param id - Unique identifier of the child - * @return true if the child is present - */ + @Override public boolean hasChild(Id id) { return childrenManager.hasChild(id); } - /** - * Returns the child specified by the id - * - * @param id - Unique identifier of the child - * @return The specified Child - * @throws NoSuchChildException If there is no such Child found - */ + @Override public T getChild(Id id) throws NoSuchChildException { return childrenManager.getChild(id); } - /** - * Returns Number of current children - * - * @return The Number of current children - */ + @Override public int numOfChildren() { return childrenManager.numOfChildren(); } - /** - * Returns a list of all current children - * - * @return list of all current children - */ + @Override public List getAllChildren() { return childrenManager.getAllChildren(); } + @Override public Map getChildren() { return childrenManager.getChildren(); } //=========== TaskList Manager ================================================================================== - /** - * Adds a new tasks to the task list - * - * @param t - */ + @Override public void addTask(Task t) { taskListManager.addTask(t); } - /** - * Deletes the task at the specified index - * - * @param index - The index of the targeted class - * @return The deleted class - * @throws NoSuchTaskException if no task can be found by the index - */ + @Override public Task deleteTask(int index) throws NoSuchTaskException { return taskListManager.deleteTask(index); } - /** - * Marks the task at the specified index as completed - * - * @param index - The index of the targeted class - * @return The marked task - * @throws NoSuchTaskException if no task can be found by the index - */ + @Override public Task markTask(int index) throws NoSuchTaskException { return taskListManager.markTask(index); } - /** - * Marks the task at the specified index as not completed - * - * @param index - The index of the targeted class - * @return The un-marked task - * @throws NoSuchTaskException if no task can be found by the index - */ + @Override public Task unmarkTask(int index) throws NoSuchTaskException { return taskListManager.unmarkTask(index); } - /** - * Finds all matching task, compares by the task's description - * - * @param query - The String to match - * @return A list of all matching Tasks - */ + @Override public List findTask(String query) throws NoSuchTaskException { return taskListManager.findTask(query); } - /** - * Returns the task at the specified index - * - * @param index - The index of the targeted class - * @return The specified task - * @throws NoSuchTaskException if no task can be found by the index - */ - public Task getTask(int index) throws NoSuchTaskException { - return taskListManager.getTask(index); + @Override + public boolean isValidIndex(int index) { + return taskListManager.isValidIndex(index); } - /** - * Returns all current task - * - * @return A list of all Tasks - */ - public List getAllTask() { - return taskListManager.getAllTasks(); + @Override + public int size() { + return taskListManager.size(); + } + + @Override + public boolean isEmpty() { + return taskListManager.isEmpty(); + } + + @Override + public Task getTask(int index) throws NoSuchTaskException { + return taskListManager.getTask(index); } + @Override public boolean contains(Task t) { return taskListManager.contains(t); } + @Override + public List getAllTasks() { + return taskListManager.getAllTasks(); + } + @Override public String toString() { return new ToStringBuilder(this) From 02581d7ea0fb3bb0bfd004cca0ae021e7b384673 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Thu, 2 Nov 2023 02:05:33 +0800 Subject: [PATCH 5/9] Refactor with new ReadOnlyTaskList --- src/main/java/seedu/address/storage/JsonAdaptedGroup.java | 5 +++-- src/main/java/seedu/address/storage/JsonAdaptedStudent.java | 3 ++- .../logic/commands/MoveStudentToGroupCommandTest.java | 4 ++-- .../address/logic/parser/CreateStudentCommandParserTest.java | 4 ++-- src/test/java/seedu/address/testutil/StudentBuilder.java | 5 +++-- src/test/java/seedu/address/testutil/TypicalTasks.java | 5 +++-- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/main/java/seedu/address/storage/JsonAdaptedGroup.java b/src/main/java/seedu/address/storage/JsonAdaptedGroup.java index be4d9bc1702..1cce2a87943 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedGroup.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedGroup.java @@ -21,6 +21,7 @@ import seedu.address.model.task.Deadline; import seedu.address.model.task.ReadOnlyTaskList; import seedu.address.model.task.Task; +import seedu.address.model.task.TaskListManager; import seedu.address.model.task.ToDo; /** @@ -67,7 +68,7 @@ public JsonAdaptedGroup(Group source) { name = source.getName().fullName; id = source.getId().toString(); students.addAll(source.getAllChildren().stream().map(JsonAdaptedStudent::new).collect(Collectors.toList())); - tasks.addAll(source.getAllTask().stream() + tasks.addAll(source.getAllTasks().stream() .map(task -> (task instanceof ToDo) ? new JsonAdaptedToDo((ToDo) task) : new JsonAdaptedDeadline((Deadline) task)) @@ -110,7 +111,7 @@ public Group toModelType() throws IllegalValueException { } final GroupId grpId = new GroupId(id); - final ReadOnlyTaskList modelTList = new ReadOnlyTaskList(taskList); + final ReadOnlyTaskList modelTList = new TaskListManager(taskList); return new Group(modelTList, studentMap, modelName, grpId); } diff --git a/src/main/java/seedu/address/storage/JsonAdaptedStudent.java b/src/main/java/seedu/address/storage/JsonAdaptedStudent.java index c06b3d5f683..5749a5828c9 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedStudent.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedStudent.java @@ -19,6 +19,7 @@ import seedu.address.model.task.Deadline; import seedu.address.model.task.ReadOnlyTaskList; import seedu.address.model.task.Task; +import seedu.address.model.task.TaskListManager; import seedu.address.model.task.ToDo; /** @@ -131,7 +132,7 @@ public Student toModelType() throws IllegalValueException { } final StudentId studId = new StudentId(id); - final ReadOnlyTaskList modelTList = new ReadOnlyTaskList(taskList); + final ReadOnlyTaskList modelTList = new TaskListManager(taskList); return new Student(modelTList, modelName, modelEmail, modelPhone, modelAddress, studId); } diff --git a/src/test/java/seedu/address/logic/commands/MoveStudentToGroupCommandTest.java b/src/test/java/seedu/address/logic/commands/MoveStudentToGroupCommandTest.java index b9c415a7941..4f808f0d4dc 100644 --- a/src/test/java/seedu/address/logic/commands/MoveStudentToGroupCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/MoveStudentToGroupCommandTest.java @@ -20,7 +20,7 @@ import seedu.address.model.profbook.Name; import seedu.address.model.profbook.Root; import seedu.address.model.profbook.Student; -import seedu.address.model.task.ReadOnlyTaskList; +import seedu.address.model.task.TaskListManager; public class MoveStudentToGroupCommandTest { public static final String ERROR_MESSAGE_INCORRECT_DIRECTORY = "Directory is invalid"; @@ -35,7 +35,7 @@ public void execute_invalidPathForSourceGroup_throwCommandException() throws Inv Map children = new HashMap<>(); Root root = new Root(children); Map students = new HashMap<>(); - Group group = new Group(new ReadOnlyTaskList(), + Group group = new Group(new TaskListManager(), students, new Name("Group1"), new GroupId("grp-001")); root.addChild(group.getId(), group); AbsolutePath currPath = new AbsolutePath("~/"); diff --git a/src/test/java/seedu/address/logic/parser/CreateStudentCommandParserTest.java b/src/test/java/seedu/address/logic/parser/CreateStudentCommandParserTest.java index 569987c09af..7b3d8da8345 100644 --- a/src/test/java/seedu/address/logic/parser/CreateStudentCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/CreateStudentCommandParserTest.java @@ -22,7 +22,7 @@ import seedu.address.model.profbook.Name; import seedu.address.model.profbook.Phone; import seedu.address.model.profbook.Student; -import seedu.address.model.task.ReadOnlyTaskList; +import seedu.address.model.task.TaskListManager; public class CreateStudentCommandParserTest { private CreateStudentCommandParser parser = new CreateStudentCommandParser(); @@ -30,7 +30,7 @@ public class CreateStudentCommandParserTest { @Test public void parse_allFieldsPresent_success() { Student expectedStu = new Student( - new ReadOnlyTaskList(), + new TaskListManager(), new Name(VALID_NAME_AMY), new Email(VALID_EMAIL_AMY), new Phone(VALID_PHONE_AMY), diff --git a/src/test/java/seedu/address/testutil/StudentBuilder.java b/src/test/java/seedu/address/testutil/StudentBuilder.java index 4bd0a5e3b76..bab9069753e 100644 --- a/src/test/java/seedu/address/testutil/StudentBuilder.java +++ b/src/test/java/seedu/address/testutil/StudentBuilder.java @@ -13,6 +13,7 @@ import seedu.address.model.task.Deadline; import seedu.address.model.task.ReadOnlyTaskList; import seedu.address.model.task.Task; +import seedu.address.model.task.TaskListManager; /** * A utility class to help with building Student objects. @@ -44,7 +45,7 @@ public StudentBuilder() { address = new Address(DEFAULT_ADDRESS); List defaultTaskList = new ArrayList<>(); defaultTaskList.add(DEFAULT_TASK); - taskList = new ReadOnlyTaskList(defaultTaskList); + taskList = new TaskListManager(defaultTaskList); } /** @@ -56,7 +57,7 @@ public StudentBuilder(Student studentToCopy) { phone = studentToCopy.getPhone(); email = studentToCopy.getEmail(); address = studentToCopy.getAddress(); - taskList = new ReadOnlyTaskList(studentToCopy.getAllTasks()); + taskList = new TaskListManager(studentToCopy.getAllTasks()); } /** diff --git a/src/test/java/seedu/address/testutil/TypicalTasks.java b/src/test/java/seedu/address/testutil/TypicalTasks.java index f8e1cefcab0..3479573897d 100644 --- a/src/test/java/seedu/address/testutil/TypicalTasks.java +++ b/src/test/java/seedu/address/testutil/TypicalTasks.java @@ -6,6 +6,7 @@ import seedu.address.model.task.Deadline; import seedu.address.model.task.ReadOnlyTaskList; +import seedu.address.model.task.TaskListManager; import seedu.address.model.task.ToDo; /** @@ -31,9 +32,9 @@ public class TypicalTasks { public static final ToDo TODO_5 = new ToDo("Read Chapter 7"); public static final ToDo TODO_6 = new ToDo("Group Meeting Preparation"); - public static final ReadOnlyTaskList TASK_LIST_1 = new ReadOnlyTaskList( + public static final ReadOnlyTaskList TASK_LIST_1 = new TaskListManager( new ArrayList<>(Arrays.asList(DEADLINE_1, DEADLINE_2, DEADLINE_3, TODO_1, TODO_2, TODO_3))); - public static final ReadOnlyTaskList TASK_LIST_2 = new ReadOnlyTaskList( + public static final ReadOnlyTaskList TASK_LIST_2 = new TaskListManager( new ArrayList<>(Arrays.asList(DEADLINE_4, DEADLINE_5, DEADLINE_6, TODO_4, TODO_5, TODO_6))); private TypicalTasks() {} // prevents instantiation From f30bc32e6b23c89b136ee2ced10ed4c1a7085f62 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Thu, 2 Nov 2023 02:05:53 +0800 Subject: [PATCH 6/9] Implement ITaskListManager --- .../address/model/task/TaskListManager.java | 103 +++++++++++------- 1 file changed, 66 insertions(+), 37 deletions(-) diff --git a/src/main/java/seedu/address/model/task/TaskListManager.java b/src/main/java/seedu/address/model/task/TaskListManager.java index 1a5bdb6c3d4..ab730ed18d8 100644 --- a/src/main/java/seedu/address/model/task/TaskListManager.java +++ b/src/main/java/seedu/address/model/task/TaskListManager.java @@ -11,41 +11,40 @@ * Encapsulate the logic of a prof book model, mainly the need for a task list * at every level */ -public class TaskListManager extends ReadOnlyTaskList { +public class TaskListManager implements ITaskListManager { + protected final List taskList; /** * Constructs a new task list manager */ public TaskListManager() { - super(); + taskList = new ArrayList<>(); } /** - * Constructs a new {@code TaskListManager} with the data in {@code taskList}. + * Constructs a new {@code TaskListManager} using {@code taskList}. * @param taskList */ - public TaskListManager(ReadOnlyTaskList taskList) { - super(taskList); + public TaskListManager(List taskList) { + this.taskList = new ArrayList<>(taskList); } /** - * Adds a task to the task list. - * - * @param t The task to be added. + * Constructs a new {@code TaskListManager} with the data in {@code toBeCopied}. + * @param taskList */ + public TaskListManager(ReadOnlyTaskList toBeCopied) { + this.taskList = new ArrayList<>(toBeCopied.getAllTasks()); + } + + @Override public void addTask(Task t) { int initialSize = this.taskList.size(); taskList.add(t); assert this.taskList.size() == initialSize + 1 : "Task Deadline should be added to the list"; } - /** - * Deletes a task from the task list. - * - * @param index The index of the task to be deleted. - * @return The deleted task. - * @throws NoSuchTaskException If there are no tasks at this level or taskNumber provided is too large. - */ + @Override public Task deleteTask(int index) throws NoSuchTaskException { verifyIsValidIndex(index); int initialSize = this.taskList.size(); @@ -55,13 +54,7 @@ public Task deleteTask(int index) throws NoSuchTaskException { return task; } - /** - * Marks a task as done. - * - * @param index The index of the task to be marked. - * @return The marked task. - * @throws NoSuchTaskException If there are no tasks at this level or taskNumber provided is too large. - */ + @Override public Task markTask(int index) throws NoSuchTaskException { verifyIsValidIndex(index); Task task = this.taskList.get(index - 1); @@ -70,13 +63,7 @@ public Task markTask(int index) throws NoSuchTaskException { return markedTask; } - /** - * Unmarks a task. - * - * @param index The number of the task to be unmarked. - * @return The unmarked task. - * @throws NoSuchTaskException If there are no tasks at this level or taskNumber provided is too large. - */ + @Override public Task unmarkTask(int index) throws NoSuchTaskException { verifyIsValidIndex(index); Task task = this.taskList.get(index - 1); @@ -85,13 +72,7 @@ public Task unmarkTask(int index) throws NoSuchTaskException { return unmarkedTask; } - /** - * Finds tasks that match the given query. - * - * @param query The query to match. - * @return A list of tasks that match the query. - * @throws NoSuchTaskException If there are no tasks at this level. - */ + @Override public List findTask(String query) throws NoSuchTaskException { if (isEmpty()) { throw new NoSuchTaskException("No task in the task list."); @@ -105,6 +86,54 @@ public List findTask(String query) throws NoSuchTaskException { return list; } + @Override + public boolean isValidIndex(int index) { + return index > 0 && index <= taskList.size(); + } + + @Override + public int size() { + return taskList.size(); + } + + @Override + public boolean isEmpty() { + return taskList.isEmpty(); + } + + @Override + public Task getTask(int index) throws NoSuchTaskException { + verifyIsValidIndex(index); + Task task = this.taskList.get(index - 1); + return task; + } + + @Override + public boolean contains(Task t) { + for (Task check : this.taskList) { + if (check.equals(t)) { + return true; + } + } + return false; + } + + @Override + public List getAllTasks() { + return new ArrayList<>(this.taskList); + } + + /** + * Checks if index is vaild. + * @throws NoSuchTaskException if index given is invalid. + */ + protected void verifyIsValidIndex(int index) throws NoSuchTaskException { + if (!isValidIndex(index)) { + throw new NoSuchTaskException("Index " + index + + " is invalid for task list with size " + size()); + } + } + @Override public String toString() { return new ToStringBuilder(this) @@ -124,6 +153,6 @@ public boolean equals(Object other) { } TaskListManager otherTaskListManager = (TaskListManager) other; - return super.equals(otherTaskListManager); + return this.taskList.equals(otherTaskListManager.taskList); } } From f6245a12922a9243f66c424d38eebcad27c2b22e Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Thu, 2 Nov 2023 02:06:05 +0800 Subject: [PATCH 7/9] Implement IChildrenManager --- .../model/profbook/ChildrenManager.java | 45 +++---------------- 1 file changed, 7 insertions(+), 38 deletions(-) diff --git a/src/main/java/seedu/address/model/profbook/ChildrenManager.java b/src/main/java/seedu/address/model/profbook/ChildrenManager.java index 286cdf85874..8c4faa06f11 100644 --- a/src/main/java/seedu/address/model/profbook/ChildrenManager.java +++ b/src/main/java/seedu/address/model/profbook/ChildrenManager.java @@ -18,7 +18,7 @@ * * @param to represent the children type, as of v1.2 only student and group */ -public class ChildrenManager> { +public class ChildrenManager> implements IChildrenManager { /** * Maps the id to the children */ @@ -54,13 +54,7 @@ public ChildrenManager(ChildrenManager toBeCopied) { this(toBeCopied.children); } - /** - * Adds the child to list of children - * - * @param id - Unique identifier of the child - * @param child - The child in question - * @throws DuplicateChildException If attempting to add child with the same ID - */ + @Override public void addChild(Id id, T child) throws DuplicateChildException { T currChild = this.children.get(id); if (currChild != null) { @@ -69,36 +63,19 @@ public void addChild(Id id, T child) throws DuplicateChildException { this.children.put(id, child); } - /** - * Deletes the child specified by the id - * - * @param id - Unique identifier of the child - * @return The deleted Child - * @throws NoSuchChildException If there is no such Child found - */ + @Override public T deleteChild(Id id) throws NoSuchChildException { T child = this.getChild(id); this.children.remove(id); return child; } - /** - * Checks if the child is present - * - * @param id - Unique identifier of the child - * @return true if the child is present - */ + @Override public boolean hasChild(Id id) { return this.children.containsKey(id); } - /** - * Returns the child specified by the id - * - * @param id - Unique identifier of the child - * @return The specified Child - * @throws NoSuchChildException If there is no such Child found - */ + @Override public T getChild(Id id) throws NoSuchChildException { T child = this.children.get(id); if (child == null) { @@ -107,20 +84,12 @@ public T getChild(Id id) throws NoSuchChildException { return child; } - /** - * Returns Number of current children - * - * @return The Number of current children - */ + @Override public int numOfChildren() { return this.children.size(); } - /** - * Returns a list of all current children - * - * @return list of all current children - */ + @Override public List getAllChildren() { return new ArrayList<>(this.children.values()); } From 35a89d86182c334f633eb195b2de63a85782c82d Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Thu, 2 Nov 2023 02:07:36 +0800 Subject: [PATCH 8/9] Refactor with new ReadOnlyTaskList --- src/main/java/seedu/address/logic/commands/EditCommand.java | 5 +++-- .../address/logic/parser/CreateStudentCommandParser.java | 4 ++-- .../java/seedu/address/statemanager/TaskOperationTest.java | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index 8584fcaa855..03468ac56f6 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -30,6 +30,7 @@ import seedu.address.model.profbook.Phone; import seedu.address.model.profbook.Student; import seedu.address.model.task.ReadOnlyTaskList; +import seedu.address.model.task.TaskListManager; /** * EditCommand is a class representing a command to edit the details of a person (either a student or a group) in @@ -238,7 +239,7 @@ private static Student createEditedStudent(Student studentToEdit, EditStudentDes Email updatedEmail = editStudentDescriptor.getEmail().orElse(studentToEdit.getEmail()); Address updatedAddress = editStudentDescriptor.getAddress().orElse(studentToEdit.getAddress()); StudentId updatedId = editStudentDescriptor.getId().orElse(studentToEdit.getId()); - ReadOnlyTaskList taskList = new ReadOnlyTaskList(studentToEdit.getAllTasks()); + ReadOnlyTaskList taskList = new TaskListManager(studentToEdit.getAllTasks()); return new Student(taskList, updatedName, updatedEmail, updatedPhone, updatedAddress, updatedId); } @@ -250,7 +251,7 @@ private static Group createEditedGroup(Group groupToEdit, EditGroupDescriptor ed assert groupToEdit != null; Name updatedName = editGroupDescriptor.getName().orElse(groupToEdit.getName()); GroupId updatedId = editGroupDescriptor.getId().orElse(groupToEdit.getId()); - ReadOnlyTaskList taskList = new ReadOnlyTaskList(groupToEdit.getAllTask()); + ReadOnlyTaskList taskList = new TaskListManager(groupToEdit.getAllTasks()); Map students = groupToEdit.getChildren(); return new Group(taskList, students, updatedName, updatedId); } diff --git a/src/main/java/seedu/address/logic/parser/CreateStudentCommandParser.java b/src/main/java/seedu/address/logic/parser/CreateStudentCommandParser.java index 5c79bcbace0..636b8a59cab 100644 --- a/src/main/java/seedu/address/logic/parser/CreateStudentCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/CreateStudentCommandParser.java @@ -20,7 +20,7 @@ import seedu.address.model.profbook.Name; import seedu.address.model.profbook.Phone; import seedu.address.model.profbook.Student; -import seedu.address.model.task.ReadOnlyTaskList; +import seedu.address.model.task.TaskListManager; /** * Parses input arguments and creates a new CreateStudentCommand object @@ -78,7 +78,7 @@ public CreateStudentCommand parse(String args, AbsolutePath currPath) throws Par ? ParserUtil.parseAddress(argMultimap.getValue(OPTION_ADDRESS).get()) : Address.PLACEHOLDER; - Student student = new Student(new ReadOnlyTaskList(new ArrayList<>()), name, email, phone, address, id); + Student student = new Student(new TaskListManager(new ArrayList<>()), name, email, phone, address, id); return new CreateStudentCommand(targetPath, student); } diff --git a/src/test/java/seedu/address/statemanager/TaskOperationTest.java b/src/test/java/seedu/address/statemanager/TaskOperationTest.java index 86e1fd4bb28..de97184eabd 100644 --- a/src/test/java/seedu/address/statemanager/TaskOperationTest.java +++ b/src/test/java/seedu/address/statemanager/TaskOperationTest.java @@ -28,8 +28,8 @@ import seedu.address.model.profbook.Root; import seedu.address.model.profbook.Student; import seedu.address.model.task.Deadline; -import seedu.address.model.task.ReadOnlyTaskList; import seedu.address.model.task.Task; +import seedu.address.model.task.TaskListManager; import seedu.address.model.task.exceptions.NoSuchTaskException; import seedu.address.testutil.StudentBuilder; @@ -64,7 +64,7 @@ public void init() { .withId("0001Y").build(); Map studentMap = new HashMap<>(); studentMap.put(new StudentId("0001Y"), this.student); - this.group = new Group(new ReadOnlyTaskList(), studentMap, new Name("gary"), new GroupId("grp-001")); + this.group = new Group(new TaskListManager(), studentMap, new Name("gary"), new GroupId("grp-001")); Map groups = new HashMap<>(); groups.put(new GroupId("grp-001"), this.group); this.root = new Root(groups); @@ -73,7 +73,7 @@ public void init() { @Test public void getTaskOperation_noErrorReturn() { - assertEquals(new TaskOperation(this.group.getTaskListManager()), model.taskOperation(grpPath)); + assertEquals(new TaskOperation(this.group), model.taskOperation(grpPath)); assertEquals(new TaskOperation(this.student), model.taskOperation(stuPath)); } From 93d11eadb4dd2121ee8d314d04d9a0a8f01a245f Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Thu, 2 Nov 2023 02:07:56 +0800 Subject: [PATCH 9/9] Refactor with new interface --- .../seedu/address/model/ChildOperation.java | 60 ++++++------------- .../seedu/address/model/ModelManager.java | 4 +- .../seedu/address/model/TaskOperation.java | 6 +- .../statemanager/ChildOperationTest.java | 8 +-- 4 files changed, 28 insertions(+), 50 deletions(-) diff --git a/src/main/java/seedu/address/model/ChildOperation.java b/src/main/java/seedu/address/model/ChildOperation.java index 5f4b91ec50a..64c62b90baa 100644 --- a/src/main/java/seedu/address/model/ChildOperation.java +++ b/src/main/java/seedu/address/model/ChildOperation.java @@ -8,12 +8,12 @@ import seedu.address.commons.core.LogsCenter; import seedu.address.model.id.Id; import seedu.address.model.profbook.ChildrenAndTaskListManager; -import seedu.address.model.profbook.ChildrenManager; import seedu.address.model.profbook.IChildElement; +import seedu.address.model.profbook.IChildrenManager; import seedu.address.model.profbook.exceptions.DuplicateChildException; import seedu.address.model.profbook.exceptions.NoSuchChildException; +import seedu.address.model.task.ITaskListManager; import seedu.address.model.task.Task; -import seedu.address.model.task.TaskListManager; /** * Encapsulates the logic to perform a generic child operation for child manager @@ -22,11 +22,11 @@ */ public class ChildOperation> implements IChildOperation { - private final ChildrenManager baseDir; + private final IChildrenManager baseDir; private final Logger logger = LogsCenter.getLogger(ChildOperation.class); - public ChildOperation(ChildrenManager baseDir) { + public ChildOperation(IChildrenManager baseDir) { this.baseDir = baseDir; } @@ -124,25 +124,15 @@ public void addTaskToAllChildren(Task task, int level) { for (IChildElement child : children) { Task clonedTask = task.clone(); - if (!(child instanceof TaskListManager) && !(child instanceof ChildrenAndTaskListManager)) { + if (!(child instanceof ITaskListManager)) { 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); + ITaskListManager tlm = (ITaskListManager) child; + if (tlm.contains(task)) { + continue; } + tlm.addTask(clonedTask); } } @@ -151,19 +141,13 @@ 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 { + if (!(child instanceof ITaskListManager)) { throw new IllegalArgumentException("All children must be task list manager."); } + ITaskListManager tlm = (ITaskListManager) child; + if (!tlm.contains(task)) { + return false; + } } return true; @@ -174,19 +158,13 @@ public boolean checkIfAnyChildHasTask(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 true; - } - } else if (child instanceof ChildrenAndTaskListManager) { - ChildrenAndTaskListManager ctlm = (ChildrenAndTaskListManager) child; - if (ctlm.contains(task)) { - return true; - } - } else { + if (!(child instanceof ITaskListManager)) { throw new IllegalArgumentException("All children must be task list manager."); } + ITaskListManager tlm = (ITaskListManager) child; + if (tlm.contains(task)) { + return true; + } } return false; diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 230e04b80dc..944d9c1040b 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -295,7 +295,7 @@ public ChildOperation groupChildOperation(AbsolutePath path) { String.format(MESSAGE_INTERNAL_ERROR, "Path must have group information")); checkArgument(hasGroup(path), String.format(MESSAGE_INTERNAL_ERROR, "Group must exist in ProfBook")); - return new ChildOperation<>(getGroupFromPath(path).getChildrenManger()); + return new ChildOperation<>(getGroupFromPath(path)); } @Override @@ -307,7 +307,7 @@ public TaskOperation taskOperation(AbsolutePath path) { String.format(MESSAGE_INTERNAL_ERROR, "Path must exist in ProfBook")); if (path.isGroupDirectory()) { - return new TaskOperation(getGroupFromPath(path).getTaskListManager()); + return new TaskOperation(getGroupFromPath(path)); } return new TaskOperation(getStudentFromPath(path)); diff --git a/src/main/java/seedu/address/model/TaskOperation.java b/src/main/java/seedu/address/model/TaskOperation.java index a4df511dd55..ec6aeac72bf 100644 --- a/src/main/java/seedu/address/model/TaskOperation.java +++ b/src/main/java/seedu/address/model/TaskOperation.java @@ -8,8 +8,8 @@ import seedu.address.commons.core.LogsCenter; import seedu.address.commons.util.JsonUtil; +import seedu.address.model.task.ITaskListManager; import seedu.address.model.task.Task; -import seedu.address.model.task.TaskListManager; /** * Encapsulates the required logic for task operation @@ -17,10 +17,10 @@ public class TaskOperation implements ITaskOperations { private static final String MESSAGE_DUPLICATE_TASK = "Task must not exist in task list."; private static final String MESSAGE_TASK_NOT_FOUND = "Task not found in task list."; - private final TaskListManager baseDir; + private final ITaskListManager baseDir; private final Logger logger = LogsCenter.getLogger(JsonUtil.class); - public TaskOperation(TaskListManager baseDir) { + public TaskOperation(ITaskListManager baseDir) { this.baseDir = baseDir; } diff --git a/src/test/java/seedu/address/statemanager/ChildOperationTest.java b/src/test/java/seedu/address/statemanager/ChildOperationTest.java index 7ee21a094de..7b4fabda553 100644 --- a/src/test/java/seedu/address/statemanager/ChildOperationTest.java +++ b/src/test/java/seedu/address/statemanager/ChildOperationTest.java @@ -27,7 +27,7 @@ import seedu.address.model.profbook.Root; import seedu.address.model.profbook.Student; import seedu.address.model.profbook.exceptions.DuplicateChildException; -import seedu.address.model.task.ReadOnlyTaskList; +import seedu.address.model.task.TaskListManager; import seedu.address.testutil.StudentBuilder; public class ChildOperationTest { @@ -59,7 +59,7 @@ public void init() { .withId("0001Y").build(); Map studentMap = new HashMap<>(); studentMap.put(new StudentId("0001Y"), this.student); - this.group = new Group(new ReadOnlyTaskList(), studentMap, new Name("gary"), new GroupId("grp-001")); + this.group = new Group(new TaskListManager(), studentMap, new Name("gary"), new GroupId("grp-001")); Map groups = new HashMap<>(); groups.put(new GroupId("grp-001"), this.group); this.root = new Root(groups); @@ -69,8 +69,8 @@ public void init() { @Test public void getChildOperation_noErrorReturn() { assertEquals(new ChildOperation<>(this.root), model.rootChildOperation()); - assertEquals(new ChildOperation<>(this.group.getChildrenManger()), model.groupChildOperation(grpPath)); - assertEquals(new ChildOperation<>(this.group.getChildrenManger()), model.groupChildOperation(stuPath)); + assertEquals(new ChildOperation<>(this.group), model.groupChildOperation(grpPath)); + assertEquals(new ChildOperation<>(this.group), model.groupChildOperation(stuPath)); } @Test