From f1debb3374130ba4e0333f90dc60409bf3d78030 Mon Sep 17 00:00:00 2001 From: Nihirraa Date: Fri, 20 Sep 2024 23:33:15 +0800 Subject: [PATCH] Ensure conflict detection between EventTask and DeadlineTask - Enhanced conflict detection to prevent overlaps between EventTask and DeadlineTask. - Conflict detection checks if a deadline occurs within the time span of an event. - Throws an when tasks with scheduling conflicts are detected. --- data/Espresso.txt | 2 + src/main/java/espresso/parser/Parser.java | 4 +- src/main/java/espresso/task/DeadlineTask.java | 4 ++ src/main/java/espresso/task/EventTask.java | 6 +++ src/main/java/espresso/task/TaskList.java | 49 +++++++++++++++++++ 5 files changed, 63 insertions(+), 2 deletions(-) diff --git a/data/Espresso.txt b/data/Espresso.txt index deb3924bd1..f8c6aca09a 100644 --- a/data/Espresso.txt +++ b/data/Espresso.txt @@ -7,3 +7,5 @@ T | 0 | task T | 0 | task T | 0 | task T | 0 | task +T | 0 | homework +D | 0 | submit report | 21-09-2024 diff --git a/src/main/java/espresso/parser/Parser.java b/src/main/java/espresso/parser/Parser.java index 4c74614ef8..2377b79cdb 100644 --- a/src/main/java/espresso/parser/Parser.java +++ b/src/main/java/espresso/parser/Parser.java @@ -42,13 +42,13 @@ public static String parse(String input, TaskList taskList, Ui ui) throws Invali if (split.length != 2) { throw new InvalidCommandException("Invalid deadline format."); } - return ui.printTaskAdded(taskList.addTask(new DeadlineTask(split[0], split[1])), "deadline"); + return ui.printTaskAdded(taskList.addTaskWithAnomalyCheck(new DeadlineTask(split[0], split[1])), "deadline"); } else if (checkInput(input, "event ")) { String[] split = input.substring(6).split(" /from | /to "); if (split.length != 3) { throw new InvalidCommandException("Invalid event format."); } - return ui.printTaskAdded(taskList.addTask(new EventTask(split[0], split[1], split[2])), "event"); + return ui.printTaskAdded(taskList.addTaskWithAnomalyCheck(new EventTask(split[0], split[1], split[2])), "event"); } else if (checkInput(input, "delete ")) { int i = Integer.parseInt(input.substring(7)) - 1; String res = ui.printTaskRemoved(taskList.getTask(i)); diff --git a/src/main/java/espresso/task/DeadlineTask.java b/src/main/java/espresso/task/DeadlineTask.java index c1884d8727..152b1ca2b9 100644 --- a/src/main/java/espresso/task/DeadlineTask.java +++ b/src/main/java/espresso/task/DeadlineTask.java @@ -18,6 +18,10 @@ public DeadlineTask(String description, String dl) throws ParseException { this.dl = inputFormat.parse(dl); } + public Date getDeadline() { + return this.dl; + } + @Override public String toString() { return "[D]" + super.toString() + " (by: " + outputFormat.format(dl) + ")"; diff --git a/src/main/java/espresso/task/EventTask.java b/src/main/java/espresso/task/EventTask.java index c6e5116222..bfde11de87 100644 --- a/src/main/java/espresso/task/EventTask.java +++ b/src/main/java/espresso/task/EventTask.java @@ -24,6 +24,12 @@ public EventTask(String description, String starts, String ends) throws ParseExc throw new InvalidCommandException("Invalid event time."); } } + public Date getStarts() { + return this.starts; + } + public Date getEnds() { + return this.ends; + } @Override public String toString() { diff --git a/src/main/java/espresso/task/TaskList.java b/src/main/java/espresso/task/TaskList.java index c47e493b33..eb1c7c17c1 100644 --- a/src/main/java/espresso/task/TaskList.java +++ b/src/main/java/espresso/task/TaskList.java @@ -101,6 +101,55 @@ public TaskList find(String str) { return res; } + public boolean detectAnomaly(Task newTask) { + for (Task task : tasks) { + if (task instanceof EventTask && newTask instanceof EventTask) { + EventTask existingTask = (EventTask) task; + EventTask eventTask = (EventTask) newTask; + + // Check for overlapping event times + if (eventTask.getEnds().after(existingTask.getStarts()) && eventTask.getStarts().before(existingTask.getEnds())) { + return true; // Conflict found + } + + } else if (task instanceof DeadlineTask && newTask instanceof DeadlineTask) { + DeadlineTask existingTask = (DeadlineTask) task; + DeadlineTask deadlineTask = (DeadlineTask) newTask; + + // Check if two DeadlineTasks have the same date + if (existingTask.getDeadline().equals(deadlineTask.getDeadline())) { + return true; // Conflict found + } + + } else if (task instanceof EventTask && newTask instanceof DeadlineTask) { + EventTask eventTask = (EventTask) task; + DeadlineTask deadlineTask = (DeadlineTask) newTask; + + // Check if the deadline falls within the event's time range + if (deadlineTask.getDeadline().after(eventTask.getStarts()) && deadlineTask.getDeadline().before(eventTask.getEnds())) { + return true; // Conflict found + } + + } else if (task instanceof DeadlineTask && newTask instanceof EventTask) { + DeadlineTask deadlineTask = (DeadlineTask) task; + EventTask eventTask = (EventTask) newTask; + + // Check if the deadline falls within the event's time range + if (deadlineTask.getDeadline().after(eventTask.getStarts()) && deadlineTask.getDeadline().before(eventTask.getEnds())) { + return true; // Conflict found + } + } + } + return false; // No conflict + } + + public Task addTaskWithAnomalyCheck(Task task) throws InvalidCommandException { + if (detectAnomaly(task)) { + throw new InvalidCommandException("Scheduling conflict detected."); + } + return addTask(task); + } + /** * Returns a string representation of the TaskList, where each task is preceded * by its position in the list.