Skip to content

Commit

Permalink
Allow sorting by time and priority
Browse files Browse the repository at this point in the history
  • Loading branch information
varung97 committed Nov 4, 2016
1 parent b717267 commit f1276a6
Show file tree
Hide file tree
Showing 23 changed files with 244 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public CommandResult undoIt() {
return new CommandResult (UNDO_SUCCESS);
}

// @@author A0147924X
/**
* @@author A0147924X
* Marks the done property of a task
* @param taskToMark The task which should be marked
* @param isDone Whether it should be marked as done or not done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ListCommand() {}
@Override
public CommandResult execute() {
model.updateSortedFilteredTaskListToShowAll();
model.unSortSortedFilteredTaskList();
model.sortSortedFilteredTaskListByTime();
return new CommandResult(MESSAGE_SUCCESS);
}
}
6 changes: 2 additions & 4 deletions src/main/java/seedu/manager/logic/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package seedu.manager.logic.commands;

import seedu.manager.model.task.Task.TaskProperties;

// @@author A0147924X
/**
* Allows user to sort the displayed tasks by priority
Expand All @@ -21,14 +19,14 @@ public SortCommand() {}

@Override
public CommandResult execute() {
model.sortSortedFilteredTaskListByProperty(TaskProperties.PRIORITY);
model.sortSortedFilteredTaskListByPriority();
this.addUndo(this);
return new CommandResult(MESSAGE_SUCCESS);
}

@Override
public CommandResult undoIt() {
model.unSortSortedFilteredTaskList();
model.sortSortedFilteredTaskListByTime();
return new CommandResult(UNDO_SUCCESS);
}
}
6 changes: 2 additions & 4 deletions src/main/java/seedu/manager/logic/commands/UnSortCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package seedu.manager.logic.commands;

import seedu.manager.model.task.Task.TaskProperties;

// @@author A0147924X
/**
* Allows user to unsort the displayed tasks, placing completed tasks at the bottom
Expand All @@ -21,14 +19,14 @@ public UnSortCommand() {}

@Override
public CommandResult execute() {
model.unSortSortedFilteredTaskList();;
model.sortSortedFilteredTaskListByTime();
this.addUndo(this);
return new CommandResult(MESSAGE_SUCCESS);
}

@Override
public CommandResult undoIt() {
model.sortSortedFilteredTaskListByProperty(TaskProperties.PRIORITY);
model.sortSortedFilteredTaskListByPriority();
return new CommandResult(UNDO_SUCCESS);
}
}
2 changes: 0 additions & 2 deletions src/main/java/seedu/manager/logic/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,6 @@ private Command prepareAlias(String args) {
*/
private Command prepareHelp(String args) {
String[] splitArgs = args.trim().split(" ");
System.out.println(splitArgs.length);
System.out.println(splitArgs);
if (splitArgs.length > 1) {
return new IncorrectCommand(HelpCommand.MESSAGE_WRONG_NUM_ARGS);
} else if (splitArgs.length == 1 && !splitArgs[0].equals("")) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/manager/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public void setSingleCommandWord(String commandToChange, String alias,

// @@author A0147924X
/** Sorts the sorted and filtered task list by a certain property */
void sortSortedFilteredTaskListByProperty(TaskProperties property);
void sortSortedFilteredTaskListByPriority();

/** Unsorts the sorted and filtered task list */
void unSortSortedFilteredTaskList();
void sortSortedFilteredTaskListByTime();

/**
* Get the index of a specified tag in the list currently being displayed
Expand Down
27 changes: 22 additions & 5 deletions src/main/java/seedu/manager/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import seedu.manager.model.tag.UniqueTagList.DuplicateTagException;
import seedu.manager.model.task.UniqueTaskList.TaskNotFoundException;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Optional;
import java.util.logging.Logger;
Expand Down Expand Up @@ -69,7 +70,7 @@ public ModelManager(ReadOnlyTaskManager initialData, UserPrefs userPrefs) {
sortedTags = new SortedList<>(filteredTags);
this.userPrefs = userPrefs;

sortSortedFilteredTaskListByProperty(TaskProperties.DONE);
sortSortedFilteredTaskListByTime();
}

// @@author
Expand Down Expand Up @@ -176,13 +177,29 @@ public void updateSortedFilteredTaskListToShowAll() {
}

@Override
public void sortSortedFilteredTaskListByProperty(TaskProperties property) {
sortedTasks.setComparator((Task t1, Task t2) -> t1.compareProperty(t2, property));
public void sortSortedFilteredTaskListByPriority() {
Comparator<ReadOnlyTask> priorityComparator = (t1, t2) -> {
int doneCompare = t1.compareDone(t2);
if (doneCompare != 0) {
return doneCompare;
} else {
return t1.comparePriority(t2);
}
};
sortedTasks.setComparator(priorityComparator);
}

@Override
public void unSortSortedFilteredTaskList() {
sortSortedFilteredTaskListByProperty(TaskProperties.DONE);
public void sortSortedFilteredTaskListByTime() {
Comparator<ReadOnlyTask> timeComparator = (t1, t2) -> {
int doneCompare = t1.compareDone(t2);
if (doneCompare != 0) {
return doneCompare;
} else {
return t1.compareTime(t2);
}
};
sortedTasks.setComparator(timeComparator);
}

@Override
Expand Down
57 changes: 51 additions & 6 deletions src/main/java/seedu/manager/model/task/ReadOnlyTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ default boolean isDone() {
}

default boolean isTaskOverdue() {
if(this.isDone()) {
if (this.isDone()) {
return false;
}

Expand Down Expand Up @@ -142,12 +142,57 @@ default String getAsPrettyText() {
}

/**
* Compares two tasks using a certain property (for sorting)
* @param other Other task
* @param property Property to be compared on
* @return -1 if this is smaller, 0 if equal, 1 is this is larger
* Compares a certain property of one task with a certain property of another task
* @param firstTask First task to compare
* @param secondTask Second task to compare
* @param firstProperty Property of first task to compare
* @param secondProperty Property of second task to compare
* @return -1 if first task is smaller, 0 if equal, 1 if first task is larger
*/
public int compareProperty(ReadOnlyTask other, TaskProperties property);
default public int compareProperty(ReadOnlyTask firstTask, ReadOnlyTask secondTask,
TaskProperties firstProperty, TaskProperties secondProperty) {
assert firstTask != null && secondTask != null;
HashMap<TaskProperties, Optional<TaskProperty>> firstProps = firstTask.getProperties();
HashMap<TaskProperties, Optional<TaskProperty>> secondProps = secondTask.getProperties();

if (!firstProps.get(firstProperty).isPresent() && !secondProps.get(secondProperty).isPresent()) {
return 0;
} else if (!firstProps.get(firstProperty).isPresent()) {
return 1;
} else if (!secondProps.get(secondProperty).isPresent()) {
return -1;
} else {
return firstProps.get(firstProperty).get().compareTo(secondProps.get(secondProperty).get());
}
}

default public int comparePriority(ReadOnlyTask other) {
assert other != null;

return this.compareProperty(this, other, TaskProperties.PRIORITY, TaskProperties.PRIORITY);
}

default public int compareTime(ReadOnlyTask other) {
assert other != null;
HashMap<TaskProperties, Optional<TaskProperty>> thisProps = this.getProperties();
HashMap<TaskProperties, Optional<TaskProperty>> otherProps = other.getProperties();

if (thisProps.get(TaskProperties.ENDTIME).isPresent() && otherProps.get(TaskProperties.ENDTIME).isPresent()) {
return this.compareProperty(this, other, TaskProperties.ENDTIME, TaskProperties.ENDTIME);
} else if (!thisProps.get(TaskProperties.ENDTIME).isPresent() && !otherProps.get(TaskProperties.ENDTIME).isPresent()) {
return this.compareProperty(this, other, TaskProperties.STARTTIME, TaskProperties.STARTTIME);
} else if (!otherProps.get(TaskProperties.ENDTIME).isPresent()) {
return this.compareProperty(this, other, TaskProperties.ENDTIME, TaskProperties.STARTTIME);
} else {
return this.compareProperty(this, other, TaskProperties.STARTTIME, TaskProperties.ENDTIME);
}
}

default public int compareDone(ReadOnlyTask other) {
assert other != null;

return compareProperty(this, other, TaskProperties.DONE, TaskProperties.DONE);
}

// @author A0139621H
public boolean matches(HashMap<TaskProperties, Optional<TaskProperty>> other);
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/seedu/manager/model/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,6 @@ private boolean matchStartOrEndTime(HashMap<TaskProperties, Optional<TaskPropert
}
}

@Override
public int compareProperty(ReadOnlyTask other, TaskProperties property) {
assert other != null;
HashMap<TaskProperties, Optional<TaskProperty>> otherProps = other.getProperties();

if (!this.properties.get(property).isPresent() && !otherProps.get(property).isPresent()) {
return 0;
} else if (!this.properties.get(property).isPresent()) {
return 1;
} else if (!otherProps.get(property).isPresent()) {
return -1;
} else {
return this.properties.get(property).get().compareTo(otherProps.get(property).get());
}
}

// @@author
@Override
public int hashCode() {
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/seedu/manager/model/task/Time.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,30 @@ public boolean matches(TaskProperty time) {
}
}

public boolean isBefore(Date now) {
public boolean isBefore(Date time) {
if (value.before(time)) {
return true;
}
return false;
}

if (value.before(now)) {
public boolean isAfter(Date time) {
if (value.after(time)) {
return true;
}
return false;
}

@Override
public int compareTo(TaskProperty other) {
assert other instanceof Time;

if (this.isBefore(((Time) other).getTime())) {
return -1;
} else if (this.isAfter(((Time) other).getTime())) {
return 1;
} else {
return 0;
}
}
}
18 changes: 10 additions & 8 deletions src/test/java/guitests/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import static org.junit.Assert.assertTrue;

import java.util.Arrays;

// @@author A0147924X
public class AddCommandTest extends TaskManagerGuiTest {

Expand All @@ -18,13 +20,11 @@ public void add() {
//add one task
TestTask[] currentList = td.getTypicalTasks();
TestTask taskToAdd = td.hotel;
assertAddSuccess(3, taskToAdd, currentList);
currentList = TestUtil.addTasksToList(3, currentList, taskToAdd);
currentList = assertAddSuccess(taskToAdd, currentList);

//add another task
taskToAdd = td.india;
assertAddSuccess(3, taskToAdd, currentList);
currentList = TestUtil.addTasksToList(3, currentList, taskToAdd);
currentList = assertAddSuccess(taskToAdd, currentList);

//add duplicate task
commandBox.runCommand(td.hotel.getAddCommand());
Expand All @@ -33,7 +33,7 @@ public void add() {

//add to empty list
commandBox.runCommand("clear");
assertAddSuccess(0, td.alpha);
assertAddSuccess(td.alpha);

//invalid command
commandBox.runCommand("helps Johnny");
Expand All @@ -45,18 +45,20 @@ public void add() {
* @param indexToInsert Index at which new task should be inserted into the current list
* @param taskToAdd The task which should be added to the current list
* @param currentList Current task list to check panel list against
* @return The new list of tasks
*/
private void assertAddSuccess(int indexToInsert, TestTask taskToAdd, TestTask... currentList) {
private TestTask[] assertAddSuccess(TestTask taskToAdd, TestTask... currentList) {
commandBox.runCommand(taskToAdd.getAddCommand());

//confirm the new card contains the right data
TaskCardHandle addedCard = taskListPanel.navigateToTask(taskToAdd.getDesc().get().getValue());
assertMatching(taskToAdd, addedCard);

//confirm the list now contains all previous tasks plus the new task
TestTask[] expectedList = TestUtil.addTasksToList(indexToInsert, currentList, taskToAdd);
TestTask[] expectedList = TestUtil.addTasksToListSortedByTime(currentList, taskToAdd);

assertTrue(taskListPanel.isListMatching(expectedList));

return expectedList;
}

}
7 changes: 6 additions & 1 deletion src/test/java/guitests/ClearCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.junit.Test;

import seedu.manager.testutil.TestTask;
import seedu.manager.testutil.TestUtil;

import static org.junit.Assert.assertTrue;

public class ClearCommandTest extends TaskManagerGuiTest {
Expand All @@ -10,7 +13,9 @@ public class ClearCommandTest extends TaskManagerGuiTest {
public void clear() {

//verify a non-empty list can be cleared
assertTrue(taskListPanel.isListMatching(td.getTypicalTasks()));
TestTask[] currentList = td.getTypicalTasks();
TestUtil.sortListByTime(currentList);
assertTrue(taskListPanel.isListMatching(currentList));
assertClearCommandSuccess();

//verify other commands can work after a clear command
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/guitests/DeleteCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public void delete() {

//delete the first in the list
TestTask[] currentList = td.getTypicalTasks();
TestUtil.sortListByTime(currentList);

int targetIndex = 1;
assertDeleteSuccess(targetIndex, currentList);

Expand Down

0 comments on commit f1276a6

Please sign in to comment.