Skip to content

Commit

Permalink
Ensure conflict detection between EventTask and DeadlineTask
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Nihirraa committed Sep 20, 2024
1 parent b0c08d9 commit f1debb3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
2 changes: 2 additions & 0 deletions data/Espresso.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/main/java/espresso/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/espresso/task/DeadlineTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) + ")";
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/espresso/task/EventTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/espresso/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f1debb3

Please sign in to comment.