From 29866e100a22e435c13dc7dd8c95d062b1c12e9d Mon Sep 17 00:00:00 2001 From: A1WAYSD Date: Wed, 13 Sep 2023 16:24:16 +0800 Subject: [PATCH] Add reminder --- data/duke.txt | 6 +++- src/main/java/duke/Parser.java | 4 ++- src/main/java/duke/task/Deadline.java | 4 +++ src/main/java/duke/task/TaskList.java | 44 +++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/data/duke.txt b/data/duke.txt index 19b34ef450..5c0a00dab4 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1,4 +1,8 @@ TODO | 0 | hihi TODO | 0 | test TODO | 0 | test -TODO | 0 | test?? \ No newline at end of file +TODO | 0 | test?? +DEADLINE | 0 | test1 | hi +DEADLINE | 0 | test2 | yes +DEADLINE | 0 | test3 | 2023-12-12date +DEADLINE | 0 | test4 | 2004-01-20date \ No newline at end of file diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index c13de63777..6c95ccb318 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -59,8 +59,10 @@ public String parse(String... inputs) throws DukeException { return this.tasks.markTask(taskIndex, isDone); } else if (taskMatcher.matches()) { return this.tasks.addTask(TaskType.valueOf(taskMatcher.group(1).toUpperCase()), taskMatcher.group(2)); - } else if (repeat.contains("list") || repeat.contains("List")) { + } else if (repeat.equals("list") || repeat.equals("List")) { return this.tasks.getTasks(); + } else if (repeat.equals("reminder")) { + return this.tasks.getReminder(); } else { throw new DukeException("undefined"); } diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index df3641f3da..c7d4e3c522 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -66,4 +66,8 @@ public String toTxt() { return this.by != null ? super.toTxt() + this.description + " | " + this.by + this.byDescription : super.toTxt() + this.description + " | " + this.byDescription; } + + public LocalDate getByDate() { + return this.by; + } } diff --git a/src/main/java/duke/task/TaskList.java b/src/main/java/duke/task/TaskList.java index 3a732ec3d6..a2db85ccac 100644 --- a/src/main/java/duke/task/TaskList.java +++ b/src/main/java/duke/task/TaskList.java @@ -3,6 +3,8 @@ import duke.DukeException; import java.time.format.DateTimeParseException; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -147,4 +149,46 @@ public String deleteTask(int taskIndex) throws DukeException { return "I've successfully deleted this task:\n" + task + "\nNow you have " + size + taskInTotal + "\n\"Ride the waves.\""; } + + /** + * Returns the reminder of upcoming deadlines. + * @return + */ + public String getReminder() { + String result = "Upcoming deadlines:\n"; + ArrayList deadlines = new ArrayList<>(); + System.out.println(tasks); + for (int i = 0; i < tasks.size(); i++) { + if (tasks.get(i).taskType == TaskType.DEADLINE && !tasks.get(i).isDone) { + System.out.println("yes"); + System.out.println(tasks.get(i)); + Deadline task = (Deadline) tasks.get(i); + deadlines.add(task); + } + } + + // Sort the deadlines list based on byDate + Collections.sort(deadlines, new Comparator() { + @Override + public int compare(Deadline d1, Deadline d2) { + if (d1.getByDate() == null && d2.getByDate() == null) { + return 0; + } else if (d1.getByDate() == null) { + return 1; + } else if (d2.getByDate() == null) { + return -1; + } else { + return d1.getByDate().compareTo(d2.getByDate()); + } + } + }); + + System.out.println(deadlines); + System.out.println(deadlines.size()); + + for (int i = 0; i < deadlines.size(); i++) { + result += (i + 1) + " " + deadlines.get(i) + "\n"; + } + return result + "\"One thing at a time.\""; + } }