Skip to content

Commit

Permalink
Implement duplication check when adding tasks
Browse files Browse the repository at this point in the history
There is a possibility where users add repeated tasks with the same
description. This check is to prevent users from accidentally adding
duplicate tasks.

Let's
* Add duplication check in TaskList
* Check if tasks are equal based on their description
  • Loading branch information
wrjgold committed Sep 9, 2024
1 parent 7ce9475 commit 3f1f157
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/main/java/tick/storage/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ public TaskList(Task... tasks) {
*
* @param task Task to be added.
*/
public void addTask(Task task) {
public void addTask(Task task) throws TickException {
assert task != null : "Task cannot be null.";
boolean isDuplicate = this.tasks.contains(task);
if (isDuplicate) {
throw new TickException("Task is already in the list!");
}
this.tasks.add(task);
}

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/tick/tasks/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,23 @@ public void markAsUndone() {
public String toString() {
return String.format("[%s] %s", this.getStatusIcon(), this.description);
}

/**
* Checks if the task is equal to another object.
* Two tasks are considered equal if they have the same description.
*
* @param obj The object to compare with.
* @return True if the tasks are equal; false otherwise.
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof Task) {
Task other = (Task) obj;
if (this.description == null || other.description == null) {
return false;
}
return this.description.equals(other.description);
}
return false;
}
}
2 changes: 1 addition & 1 deletion src/test/java/tick/storage/TaskListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void markTaskAsUndoneTest() throws TickException {
}

@Test
public void addTaskTest() {
public void addTaskTest() throws TickException {
TaskList taskList = new TaskList();
taskList.addTask(new ToDo("homework"));
assertEquals(1, taskList.getSize());
Expand Down

0 comments on commit 3f1f157

Please sign in to comment.