Skip to content

Commit

Permalink
Merge pull request #4 from yilinzyl/branch-C-Sort
Browse files Browse the repository at this point in the history
Add sort functionality
  • Loading branch information
yilinzyl authored Sep 18, 2022
2 parents 48b47ed + 860ed49 commit 9fbfafd
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/main/java/duke/CommandWords.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* Commands that the user can use in the Duke Application.
*/
public enum CommandWords {
BYE, LIST, UNMARK, MARK, TODO, EVENT, DEADLINE, DELETE, FIND
BYE, LIST, UNMARK, MARK, TODO, EVENT, DEADLINE, DELETE, FIND, SORT
}
50 changes: 50 additions & 0 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import duke.exception.DukeException;
import duke.exception.DukeRuntimeException;
import duke.task.DatedTask;
import duke.task.Deadline;
import duke.task.Event;
import duke.task.Task;
Expand All @@ -10,6 +11,7 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;

/**
* Represents the list of tasks that have been created in the Duke application.
Expand Down Expand Up @@ -98,6 +100,54 @@ public TaskList find(String keyword) {
return foundTasks;
}

/**
* Returns TaskList containing all tasks in sorted order.
* @param taskType Type of tasks to be sorted - Deadline, event or both.
* @return TaskList containing matching tasks in sorted order.
*/
public TaskList sort(String taskType) {
ArrayList<DatedTask> filteredTasks = filterByDatedTask(taskType);
filteredTasks.sort((x, y) -> x.compareTo(y));
TaskList sortedTasks = new TaskList();
for (Task sortedTask : filteredTasks) {
sortedTasks.addTask(sortedTask);
}
return sortedTasks;
}

private ArrayList<DatedTask> filterByDatedTask(String taskType) {
ArrayList<DatedTask> filteredList = new ArrayList<>();
switch (taskType) {
case "deadline":
for (Task task : tasks) {
if (task instanceof Deadline) {
DatedTask datedTask = (DatedTask) task;
filteredList.add(datedTask);
}
}
break;
case "event":
for (Task task : tasks) {
if (task instanceof Event) {
DatedTask datedTask = (DatedTask) task;
filteredList.add(datedTask);
}
}
break;
case "dated":
for (Task task : tasks) {
if (task instanceof DatedTask) {
DatedTask datedTask = (DatedTask) task;
filteredList.add(datedTask);
}
}
break;
default:
throw new DukeRuntimeException("TaskList::filter invalid task type entered.");
}
return filteredList;
}

/**
* Gets String representation of this task list.
* @return string representation of this task list.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/command/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public FindCommand(String keyword) {

/**
* Executes this command.
* @param tasks Task list to be listed.
* @param tasks Task list to find from.
* @param storage Storage used in application.
* @return The response of the execution.
*/
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/duke/command/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package duke.command;

import duke.exception.DukeException;
import duke.Response;
import duke.Storage;
import duke.TaskList;

import duke.task.Task;

/**
* Represents a command to sort dated tasks according to their dates.
*/
public class SortCommand extends Command {

/* Type of task to be shown - Event, Deadline or both */
private String taskType;

/**
* Initialises the SortCommand with the task type of tasks to be sorted.
* @param taskType Type of tasks to be displayed.
*/
public SortCommand(String taskType) {
this.taskType = taskType;
}

/**
* Initialises the SortCommand with all deadline and events to be sorted.
*/
public SortCommand() {
this.taskType = "dated";
}

/**
* Executes this command to sort all deadlines and events, or just deadlines or events.
* @param tasks Task list that contains tasks to be sorted.
* @param storage Storage used in application.
* @return The response of the execution.
*/
@Override
public Response execute(TaskList tasks, Storage storage) {
TaskList sortedTasks = tasks.sort(taskType);
String message = sortedTasks.toString();
return new Response(message, false);
};
}
14 changes: 14 additions & 0 deletions src/main/java/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import duke.command.FindCommand;
import duke.command.ListCommand;
import duke.command.MarkCommand;
import duke.command.SortCommand;
import duke.command.UnmarkCommand;
import duke.exception.DukeException;
import duke.task.Deadline;
Expand Down Expand Up @@ -65,6 +66,17 @@ public static Command parse(String input) throws DukeException {
throw new DukeException("The keyword to search for cannot be empty.");
}
return new FindCommand(input.substring(5));
case SORT:
int commandLength = 4;
if (input.length() == commandLength) {
return new SortCommand();
}
if (input.equals("sort deadline")) {
return new SortCommand("deadline");
}
if (input.equals("sort event")) {
return new SortCommand("event");
}
default:
throw new DukeException("I'm sorry, but I don't know what that means :-(");
}
Expand All @@ -89,6 +101,8 @@ private static CommandWords getCommand(String input) throws DukeException {
return CommandWords.DELETE;
} else if (input.length() > 3 && input.substring(0, 4).equals("find")) {
return CommandWords.FIND;
} else if (input.length() > 3 && input.substring(0, 4).equals("sort")) {
return CommandWords.SORT;
} else {
throw new DukeException("I'm sorry, but I don't know what that means :-(");
}
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/duke/task/DatedTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package duke.task;

import java.time.LocalDateTime;

/**
* Represents a task that is associated with a date.
*/
abstract public class DatedTask extends Task {

/**
* Initialises a Task with its description.
*
* @param description Description of the Task.
*/
public DatedTask(String description) {
super(description);
}

/**
* Checks if another DatedTask occurs later or earlier than self.
* @param task Task to compare to.
* @return returns 1 if self is later than task, -1 if task if it is earlier.
*/
public abstract int compareTo(DatedTask task);

/**
* Gets the date the task occurs/is due.
* @return LocalDateTime of date.
*/
public abstract LocalDateTime getDate();

}
16 changes: 11 additions & 5 deletions src/main/java/duke/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Represents a task that has a deadline.
*/
public class Deadline extends Task {
public class Deadline extends DatedTask {

private LocalDateTime by;

Expand All @@ -20,10 +20,16 @@ public Deadline(String description, LocalDateTime by) {
this.by = by;
}

/**
* Gets String representation of this deadline.
* @return String representation of this deadline.
*/
@Override
public LocalDateTime getDate() {
return by;
}

@Override
public int compareTo(DatedTask task) {
return this.by.isAfter(task.getDate()) ? 1 : -1;
}

@Override
public String toString() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm");
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/duke/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.util.Date;

/**
* Represents a task that occurs at a specific time.
*/
public class Event extends Task {
public class Event extends DatedTask {

private LocalDateTime at;

Expand All @@ -20,10 +21,16 @@ public Event(String description, LocalDateTime at) {
this.at = at;
}

/**
* Gets String representation of this event.
* @return String representation of this event.
*/
@Override
public LocalDateTime getDate() {
return at;
}

@Override
public int compareTo(DatedTask task) {
return this.at.isAfter(task.getDate()) ? 1 : -1;
}

@Override
public String toString() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm");
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/duke/task/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ public Todo(String description) {
super(description);
}

/**
* Gets String representation of this todo.
* @return String representation of this todo.
*/
@Override
public String toString() {
return "[T]" + super.toString();
Expand Down

0 comments on commit 9fbfafd

Please sign in to comment.