Skip to content

Commit

Permalink
Add sorting features
Browse files Browse the repository at this point in the history
Added sorting by completion status and date added, which uses
LocalDateTime to compare dates. Also implemented the date added to
Storage functions, and tweaked formatting of commands. Edited
help command and user guide.
  • Loading branch information
koh-jx committed Sep 16, 2021
1 parent a966916 commit 2224bd6
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 64 deletions.
38 changes: 34 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ Adds a deadline into your task list.

**Example usage:**

`deadline taskname /by DD/MM/YYYY xxxxH`
`deadline taskname /by DD/MM/YYYY xxxx`

* `taskname` - Name of deadline
* `DD/MM/YYYY` - Date and month can be 1-2 digits long
* `xxxxH` - Time in 24-hour format *[Optional]*
* `xxxx` - Time in 24-hour format *[Optional]*

**Expected outcome:**

Expand All @@ -70,11 +70,11 @@ Adds an event into your task list.

**Example usage:**

`event taskname /at DD/MM/YYYY xxxxH`
`event taskname /at DD/MM/YYYY xxxx`

* `taskname` - Name of deadline
* `DD/MM/YYYY` - Date and month can be 1-2 digits long
* `xxxxH` - Time in 24-hour format *[Optional]*
* `xxxx` - Time in 24-hour format *[Optional]*

A confirmation response would be sent by Duke:

Expand Down Expand Up @@ -161,6 +161,36 @@ There are ___ tasks in your list

<br/><br/>

### `Sort` - Sorts your tasks

Sorts your tasks based on the arguments provided.

**Example usage (with and without optional arguments):**

`sort` - Standard sort by name (no arguments)

`list /date` - Sort by date and time

`list /done` - Sort by completion status

`list /name` - Sort by name (Same as no arguments)

`list /added` - Sort by date in which task was added

Arguments can be stacked, list will be sorted by the first one first, followed by the second, etc.

`list /done /date` - Sort by completion status first. For those with the completion status, sort by date and time.

`list /done /date /name` - Same as above, but for those with the same date and time, sorted in alphabetical order.

**Expected outcome:**

This command will overwrite your current list with the new sorted task list.

A new list of all your tasks (if any) would be sent by Duke.

<br/><br/>

### `help` - Display list of commands
In-built command to display the list of possible commands and arguments within the app.

Expand Down
26 changes: 0 additions & 26 deletions src/main/java/duke/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,4 @@ public String toString() {
return commandName + " - " + description + '\n'
+ argString + '\n';
}

protected static String getCommand(String str)
throws IllegalArgumentException{
int index = getCommandArgumentSplitIndex(str);
return str.substring(0, index);
}

protected static String getArgument(String str)
throws IllegalArgumentException {

// Ensure validity; has a command and an argument
int index = getCommandArgumentSplitIndex(str);
return str.substring(index).trim();
}

private static int getCommandArgumentSplitIndex(String str)
throws IllegalArgumentException {
// Ensure validity; has a command and an argument
int index = str.indexOf(' ');

if (index == -1) {
throw new IllegalArgumentException(Ui.MESSAGE_INVALID_ARG);
}

return index;
}
}
4 changes: 3 additions & 1 deletion src/main/java/duke/command/CommandAddDeadline.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package duke.command;

import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;

import duke.ui.Ui;
Expand Down Expand Up @@ -43,7 +44,8 @@ public String execute() {
input[0],
DukeParser.getDate(input[1]),
input[2],
false));
false,
LocalDateTime.now()));
} catch (DateTimeParseException e) {
return Ui.MESSAGE_INVALID_DATE;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/duke/command/CommandAddEvent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package duke.command;

import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;

import duke.ui.Ui;
Expand Down Expand Up @@ -43,7 +44,8 @@ public String execute() {
input[0],
DukeParser.getDate(input[1]),
input[2],
false));
false,
LocalDateTime.now()));
} catch (DateTimeParseException e) {
return Ui.MESSAGE_INVALID_DATE;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/duke/command/CommandAddTodo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import task.TaskList;
import task.TaskTodo;

import java.time.LocalDateTime;

/**
* Command to add a to-do.
*/
Expand Down Expand Up @@ -33,6 +35,6 @@ public CommandAddTodo(TaskList taskList, String desc) {
*/
@Override
public String execute() {
return taskList.add(new TaskTodo(desc, false));
return taskList.add(new TaskTodo(desc, false, LocalDateTime.now()));
}
}
1 change: 1 addition & 0 deletions src/main/java/duke/command/CommandHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private String displayHelp() {
+ new CommandDone(null, 0) + '\n'
+ new CommandDelete(null, 0) + '\n'
+ new CommandList(null, null) + '\n'
+ new CommandSort(null, null) + '\n'
+ new CommandHelp() + '\n'
+ new CommandExit();
}
Expand Down
28 changes: 27 additions & 1 deletion src/main/java/duke/command/CommandList.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ private ArrayList<Predicate<Task>> listStringToFilter(String stringToParse)

switch (command) {
case ("name"):
results.add(task -> task.getDescription().contains(arg));
results.add(task -> task.getDescription()
.toLowerCase().contains(arg.toLowerCase()));
break;
case ("date"):
LocalDate date = DukeParser.getDate(arg);
Expand All @@ -102,4 +103,29 @@ private ArrayList<Predicate<Task>> listStringToFilter(String stringToParse)

return results;
}

private String getCommand(String str)
throws IllegalArgumentException{
int index = getCommandArgumentSplitIndex(str);
return str.substring(0, index);
}

private String getArgument(String str)
throws IllegalArgumentException {

// Ensure validity; has a command and an argument
int index = getCommandArgumentSplitIndex(str);
return str.substring(index).trim();
}

private int getCommandArgumentSplitIndex(String str)
throws IllegalArgumentException {
// Ensure validity; has a command and an argument
int index = str.indexOf(' ');
if (index == -1) {
throw new IllegalArgumentException(Ui.MESSAGE_INVALID_ARG);
}

return index;
}
}
36 changes: 26 additions & 10 deletions src/main/java/duke/command/CommandSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ public class CommandSort extends Command {
* @param sortFilters Un-parsed list of filters.
*/
public CommandSort(TaskList taskList, String sortFilters) {
this.commandName = "list /name <name> /date DD/MM/YYYY";
this.description = "Toggles completion of task. Order of arguments does not matter";
this.commandName = "sort /name /date";
this.description = "Sorts list based on arguments provided. Having no arguments would default to" +
" sorting by /name. If there is more than 1 argument, the list will be sorted by the first." +
"followed by the second, etc...";
this.arguments = new String[]{
"/name Optional argument to search for particular name",
"/date Optional date argument in DAY/MONTH/YEAR, "
+ "to search for tasks on a particular date"
"/name Optional argument to sort by name",
"/date Optional argument to sort by date",
"/done Optional argument to sort by completion status"
};

this.taskList = taskList;
Expand Down Expand Up @@ -87,6 +89,12 @@ private ArrayList<Comparator<Task>> sortStringToFilter(String stringToParse)
case ("date"):
results.add(createDateTimeComparator());
break;
case ("done"):
results.add(createDoneComparator());
break;
case ("added"):
results.add(createAddedComparator());
break;
default:
throw new IllegalArgumentException(Ui.MESSAGE_INVALID_ARG);
}
Expand All @@ -96,15 +104,23 @@ private ArrayList<Comparator<Task>> sortStringToFilter(String stringToParse)
}

private Comparator<Task> createNameComparator() {
return Comparator.comparing(Task::getDescription);
return Comparator.comparing(t -> t.getDescription().toLowerCase());
}

private Comparator<Task> createDateTimeComparator() {
return (o1, o2) -> {
int compareDate = o1.getDate().compareTo(o2.getDate());
return (t1, t2) -> {
int compareDate = t1.getDate().compareTo(t2.getDate());
return compareDate == 0
? o1.getTime().compareTo(o2.getTime())
: o1.getDate().compareTo(o2.getDate());
? t1.getTime().compareTo(t2.getTime())
: t1.getDate().compareTo(t2.getDate());
};
}

private Comparator<Task> createDoneComparator() {
return Comparator.comparing(Task::getDone);
}

private Comparator<Task> createAddedComparator() {
return Comparator.comparing(Task::getAdded);
}
}
27 changes: 18 additions & 9 deletions src/main/java/duke/util/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Scanner;

Expand Down Expand Up @@ -95,18 +96,25 @@ private static Task stringToTask(String task) throws ParseException {
try {
switch (taskType) {
case "T":
// Create new To-do
assert args.length == 3: "Invalid loaded task";
return new TaskTodo(args[2], args[1].equals("1"));
return new TaskTodo(args[2], args[1].equals("1"), LocalDateTime.parse(args[3]));
case "D":
assert args.length == 4 || args.length == 5: "Invalid loaded task";
return args.length == 4
? new TaskDeadline(args[2], LocalDate.parse(args[3]), null, !args[1].equals("0"))
: new TaskDeadline(args[2], LocalDate.parse(args[3]), args[4], !args[1].equals("0"));
// Create new Deadline
assert args.length == 5 || args.length == 6: "Invalid loaded task";
return args.length == 5
? new TaskDeadline(args[2], LocalDate.parse(args[3]), null,
!args[1].equals("0"), LocalDateTime.parse(args[4]))
: new TaskDeadline(args[2], LocalDate.parse(args[3]), args[4],
!args[1].equals("0"), LocalDateTime.parse(args[5]));
case "E":
assert args.length == 4 || args.length == 5: "Invalid loaded task";
return args.length == 4
? new TaskEvent(args[2], LocalDate.parse(args[3]), null, !args[1].equals("0"))
: new TaskEvent(args[2], LocalDate.parse(args[3]), args[4], !args[1].equals("0"));
// Create new Event
assert args.length == 5 || args.length == 6: "Invalid loaded task";
return args.length == 5
? new TaskEvent(args[2], LocalDate.parse(args[3]), null,
!args[1].equals("0"), LocalDateTime.parse(args[4]))
: new TaskEvent(args[2], LocalDate.parse(args[3]), args[4],
!args[1].equals("0"), LocalDateTime.parse(args[5]));
default:
throw new ParseException(Ui.MESSAGE_INVALID_ARG + Ui.MESSAGE_FILE_NOT_READ, 0);
}
Expand All @@ -115,3 +123,4 @@ private static Task stringToTask(String task) throws ParseException {
}
}
}

16 changes: 14 additions & 2 deletions src/main/java/task/Task.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package task;

import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.time.LocalDateTime;

import duke.ui.Ui;

Expand All @@ -11,16 +11,18 @@
public abstract class Task {
protected final String description;
protected boolean isDone;
protected LocalDateTime dateTimeAdded;

/**
* Constructor.
*
* @param input Description of task.
* @param isDone Whether the task is complete.
*/
public Task(String input, boolean isDone) {
public Task(String input, boolean isDone, LocalDateTime dateTimeAdded) {
description = input;
this.isDone = isDone;
this.dateTimeAdded = dateTimeAdded;
}

/**
Expand All @@ -42,6 +44,16 @@ public String getDescription() {
return description;
}

public boolean getDone() {
return isDone;
}

public LocalDateTime getAdded() {
return dateTimeAdded;
}



public abstract LocalDate getDate();

public abstract String getTime();
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/task/TaskDeadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import duke.ui.Ui;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.util.Optional;

Expand All @@ -21,9 +22,9 @@ public class TaskDeadline extends Task {
* @param time Time of task (Optional argument), saved as "" otherwise.
* @param done Completion status.
*/
public TaskDeadline(String description, LocalDate date, String time, boolean done)
public TaskDeadline(String description, LocalDate date, String time, boolean done, LocalDateTime dateTimeAdded)
throws DateTimeParseException {
super(description, done);
super(description, done, dateTimeAdded);
this.by = date;
this.time = Optional.ofNullable(time)
.map(String::strip)
Expand Down Expand Up @@ -60,7 +61,8 @@ public String saveString() {
+ (this.isDone ? "1" : "0") + '\t'
+ this.description + '\t'
+ this.by + '\t'
+ this.time;
+ this.time + '\t'
+ this.dateTimeAdded;
}

/**
Expand Down
Loading

0 comments on commit 2224bd6

Please sign in to comment.