forked from nus-cs2103-AY2324S2/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow user to sort the list base on the task's completion status. Let's * add SortCommand.java * update Parser.java to include "sort"
- Loading branch information
shunjieee
committed
Feb 29, 2024
1 parent
3f8ae17
commit 9ef7366
Showing
2 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package command; | ||
|
||
import java.util.Comparator; | ||
|
||
import task.Task; | ||
import task.TaskList; | ||
|
||
/** | ||
* {@inheritDocs} | ||
* Sort the tasks in the task list. | ||
*/ | ||
public class SortCommand extends Command { | ||
private TaskList taskList; | ||
|
||
/** | ||
* Creates an instance of SortCommand. | ||
*/ | ||
public SortCommand(TaskList taskList) { | ||
this.taskList = taskList; | ||
} | ||
|
||
/** | ||
* {@inheritDocs} | ||
* List all the tasks in the task list. | ||
*/ | ||
@Override | ||
public String execute() { | ||
taskList.getList().sort(new StatusComparator()); | ||
String message = "I have sorted the task list according to the tasks' statuses."; | ||
return message + "\n\n" + new ListCommand(taskList).execute(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @return True if program will exit. | ||
*/ | ||
@Override | ||
public boolean isExit() { | ||
return false; | ||
} | ||
|
||
static class StatusComparator implements Comparator<Task> { | ||
// undone status are on top of the list | ||
@Override | ||
public int compare(Task task1, Task task2) { | ||
if (!task1.checkStatus() && task2.checkStatus()) { | ||
return -1; | ||
|
||
} else if (task1.checkStatus() && !task2.checkStatus()) { | ||
return 1; | ||
|
||
} else { | ||
return task1.getTaskName().compareToIgnoreCase(task2.getTaskName()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters