From 1467d91474a94cc54bb1af0aec12354dc0c53ed1 Mon Sep 17 00:00:00 2001 From: nicktohzyu Date: Mon, 14 Sep 2020 19:54:15 +0800 Subject: [PATCH] C-sort --- src/main/java/Parser.java | 3 +++ src/main/java/Task/Task.java | 24 +++++++++++++++++++++++- src/main/java/TaskList.java | 21 +++++++++++++++++---- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index c18d348609..9b1beea69d 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -94,6 +94,9 @@ void parse(String input) throws DukeException { } tasks.find(input.substring(5)); break; + case "sort": + tasks.sort(); + break; default: throw new DukeException("I'm sorry, but I don't know what that means :-("); } diff --git a/src/main/java/Task/Task.java b/src/main/java/Task/Task.java index 8651e39102..801341179a 100644 --- a/src/main/java/Task/Task.java +++ b/src/main/java/Task/Task.java @@ -5,7 +5,7 @@ /** * Represents a task. */ -public class Task implements java.io.Serializable { +public class Task implements java.io.Serializable, Comparable { public String text; public boolean isDone; LocalDate date; @@ -25,4 +25,26 @@ public Task(String text, LocalDate date) { public String toString() { return "[" + (isDone ? "✓" : "✗") + "] " + text; } + + /** + * Compares this task with the specified task for order based on date. The task with no date is considered greater. + * Otherwise, the task with a later date is considered greater. + * + * @param t the object to be compared. + * @return a negative integer, zero, or a positive integer as this object + * is less than, equal to, or greater than the specified object. + * @throws NullPointerException if the specified object is null + * @throws ClassCastException if the specified object's type prevents it + * from being compared to this object. + */ + @Override + public int compareTo(Task t) { + if(this.date == null){ + return 1; + } else if (t.date == null){ + return -1; + } else { + return this.date.compareTo(t.date); + } + } } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index faf0033eef..198c060de6 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -1,6 +1,7 @@ import Task.Task; import java.util.ArrayList; +import java.util.Collections; /** * Stores tasks and supports various operations on them. @@ -43,15 +44,19 @@ public String numTasks() { * Prints the list of tasks through the UI class */ public void print_tasks() { + UI.print(this.toString()); + } + + @Override + public String toString(){ if(tasks.size() == 0){ - UI.print("There are no tasks!\n"); - return; + return "There are no tasks!\n"; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < tasks.size(); i++) { - sb.append((i + 1) + "." + tasks.get(i).toString()); + sb.append(i + 1).append(".").append(tasks.get(i).toString()); } - UI.print(sb.toString()); + return sb.toString(); } /** @@ -111,4 +116,12 @@ public void find(String substring) { UI.print("No match found!\n"); } } + + /** + * Sorts tasks by date then prints the new list. + */ + public void sort(){ + Collections.sort(tasks); + UI.print("Tasks sorted by date.\n" + this.toString()); + } } \ No newline at end of file