Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect duplicate tasks #2

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lite.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
D!0!return book!by 2020-10-15 18:30
E!0!project meeting!from 2020-08-10 15:30!to 2020-08-10 17:30
T!0!read a book
T!0!do a test
23 changes: 16 additions & 7 deletions src/main/java/lite/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public String deleteTask(String instruction[], TaskList tasks) {
tasks.remove(index);
assert tasks.size() == size - 1 : "size of array decreased by 1";
return output;
} catch (NullPointerException | ArrayIndexOutOfBoundsException e){
} catch (NullPointerException | IndexOutOfBoundsException e){
return LiteException.deleteException(tasks);
}
}
Expand All @@ -73,10 +73,13 @@ public String addToDoTask(String instruction[], TaskList tasks) {
String description = instruction[1];
Task todo = new Todo(description);
int size = tasks.size();
tasks.add(todo);
boolean isDuplicate = tasks.add(todo);
if (isDuplicate) {
return Printer.printDuplicateFound();
}
assert tasks.size() == size + 1 : "size of array increased by 1";
return Printer.printTask(tasks, todo);
} catch (ArrayIndexOutOfBoundsException e) {
} catch (IndexOutOfBoundsException e) {
return LiteException.toDoException();
}
}
Expand All @@ -93,10 +96,13 @@ public String addDeadlineTask(String instruction[], TaskList tasks) {
String due = splits[1];
Task deadline = new Deadline(description, due);
int size = tasks.size();
tasks.add(deadline);
boolean isDuplicate = tasks.add(deadline);
if (isDuplicate) {
return Printer.printDuplicateFound();
}
assert tasks.size() == size + 1 : "size of array increased by 1";
return Printer.printTask(tasks, deadline);
} catch (ArrayIndexOutOfBoundsException e) {
} catch (IndexOutOfBoundsException e) {
return LiteException.deadlineException();
}
}
Expand All @@ -114,10 +120,13 @@ public String addEventTask(String[] instruction, TaskList tasks) {
String end = splits[2];
Task event = new Event(description, start, end);
int size = tasks.size();
tasks.add(event);
boolean isDuplicate = tasks.add(event);
if (isDuplicate) {
return Printer.printDuplicateFound();
}
assert tasks.size() == size + 1 : "size of array increased by 1";
return Printer.printTask(tasks, event);
} catch (ArrayIndexOutOfBoundsException e) {
} catch (IndexOutOfBoundsException e) {
return LiteException.eventException();
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/lite/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ public String saveToFile() {
return (this.isDone ? "1" : "0")
+ "!" + this.description;
}

@Override
public boolean equals (Object obj) {
Task task = (Task) obj;
System.out.println(this.toString());
System.out.println(task.toString());
return this.toString().equals(task.toString());
}
}
8 changes: 7 additions & 1 deletion src/main/java/lite/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lite.util.Printer;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class TaskList {
private ArrayList<Task> tasks;
Expand Down Expand Up @@ -44,8 +46,12 @@ public Task get(int i) {
* Adds a task to the array list
* @param task Task to be added
*/
public void add (Task task) {
public boolean add (Task task) {
if (tasks.contains(task)) {
return true;
}
this.tasks.add(task);
return false;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/lite/util/Printer.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ public static String printFound(TaskList tasks) {
public static String printNotFound() {
return "There are no tasks that correspond with your input";
}

public static String printDuplicateFound() {
return "This task is already in your Task List.";
}
}