Skip to content

Commit

Permalink
C-sort
Browse files Browse the repository at this point in the history
  • Loading branch information
nicktohzyu committed Sep 14, 2020
1 parent 9132476 commit 1467d91
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 :-(");
}
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/Task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Represents a task.
*/
public class Task implements java.io.Serializable {
public class Task implements java.io.Serializable, Comparable<Task> {
public String text;
public boolean isDone;
LocalDate date;
Expand All @@ -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);
}
}
}
21 changes: 17 additions & 4 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Task.Task;

import java.util.ArrayList;
import java.util.Collections;

/**
* Stores tasks and supports various operations on them.
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 1467d91

Please sign in to comment.