Skip to content

Commit

Permalink
Merge pull request #3 from Fallman2/branch-SortTaskList
Browse files Browse the repository at this point in the history
Merge sorting of task list into Task List branch
  • Loading branch information
Fallman2 authored Nov 2, 2023
2 parents 962b40a + d3d3e07 commit 04fcc5a
Show file tree
Hide file tree
Showing 16 changed files with 354 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/main/java/seedu/address/logic/commands/SortTasksCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.task.exceptions.InvalidSortingOrderException;

/**
* Changes the sorting order of tasks in the task manager.
*/
public class SortTasksCommand extends Command {
public static final String COMMAND_WORD = "sortTasks";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Changes the way tasks in the task list are sorted."
+ "Parameters: Description OR Deadline\n"
+ "Example: " + COMMAND_WORD + " Description";

public static final String MESSAGE_SUCCESS = "Sorting order changed to %s.";

public static final String MESSAGE_INVALID_TYPE = "Sorting order provided is invalid!\n"
+ "Sorting orders available: DESCRIPTION/DEADLINE.";

private final String comparatorType;

public SortTasksCommand(String comparatorType) {
this.comparatorType = comparatorType;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

try {
model.sortTasksBy(comparatorType);
} catch (InvalidSortingOrderException e) {
throw new CommandException(MESSAGE_INVALID_TYPE);
}
return new CommandResult(String.format(MESSAGE_SUCCESS, comparatorType));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof SortTasksCommand)) {
return false;
}

SortTasksCommand otherSortTasksCommand = (SortTasksCommand) other;
return comparatorType.equals((otherSortTasksCommand).comparatorType);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("comparatorType", comparatorType)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.logic.commands.SortTasksCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new SortTasksCommand object.
*/
public class SortTasksCommandParser implements Parser<SortTasksCommand> {

/**
* Parses the given {@code String} of arguments in the context of the SortTasksCommand
* and returns a SortTasksCommand object for execution
* @throws ParseException if no parameters are provided.
*/
public SortTasksCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortTasksCommand.MESSAGE_USAGE));
}
return new SortTasksCommand(trimmedArgs);
}
}
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/parser/UniMateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.SortCommand;
import seedu.address.logic.commands.SortTasksCommand;
import seedu.address.logic.commands.SwitchListCommand;
import seedu.address.logic.parser.exceptions.ParseException;

Expand Down Expand Up @@ -109,6 +110,9 @@ public Command parseCommand(String userInput) throws ParseException {
case SwitchListCommand.COMMAND_WORD:
return new SwitchListCommand();

case SortTasksCommand.COMMAND_WORD:
return new SortTasksCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ public interface Model {
*/
TaskManager getTaskManager();

/**
* Sorts the task list by the preset comparator type listed.
*/
void sortTasksBy(String comparatorType);

/**
* Updates the filter of the filtered person list to filter by the given {@code predicate}.
* @throws NullPointerException if {@code predicate} is null.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ public ObservableList<Task> getTaskList() {
return taskManager.getTaskList();

Check warning on line 236 in src/main/java/seedu/address/model/ModelManager.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/ModelManager.java#L236

Added line #L236 was not covered by tests
}

@Override
public void sortTasksBy(String comparatorType) {
taskManager.sortTasksBy(comparatorType);
}

//=========== Filtered Person List Accessors =============================================================

/**
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/seedu/address/model/task/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import java.util.List;

import javafx.collections.ObservableList;
import seedu.address.model.task.exceptions.InvalidSortingOrderException;

/**
* Represents a task manager that stores and manages tasks
*/
public class TaskManager implements ReadOnlyTaskManager {
private static final String COMPARATOR_TYPE_DESCRIPTION = "Description";
private static final String COMPARATOR_TYPE_DEADLINE = "Deadline";

private final TaskList tasks;
private Comparator<Task> sortingOrder;
Expand Down Expand Up @@ -74,13 +77,35 @@ public Task deleteTask(int index) {
*/
public void setSortDeadline() {
sortingOrder = new Task.TaskDeadlineComparator();
sort();
}

/**
* Sets the sorting order setting to sort by description.
*/
public void setSortDescription() {
sortingOrder = new Task.TaskDescriptorComparator();
sort();
}

/**
* Returns the sorting order.
*/
public Comparator<Task> getSortingOrder() {
return sortingOrder;
}

/**
* Sets the sorting order of the task list to the specified comparator type.
*/
public void sortTasksBy(String comparatorType) {
if (comparatorType.equalsIgnoreCase(COMPARATOR_TYPE_DESCRIPTION)) {
setSortDescription();
} else if (comparatorType.equalsIgnoreCase(COMPARATOR_TYPE_DEADLINE)) {
setSortDeadline();;
} else {
throw new InvalidSortingOrderException();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package seedu.address.model.task.exceptions;

/**
* An exception thrown when a sorting order provided is invalid.
*/
public class InvalidSortingOrderException extends RuntimeException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ public TaskManager getTaskManager() {
throw new AssertionError("This method should not be called.");
}

@Override
public void sortTasksBy(String comparatorType) {
throw new AssertionError("This method should not be called.");
}

@Override
public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ public TaskManager getTaskManager() {
throw new AssertionError("This method should not be called.");
}

@Override
public void sortTasksBy(String comparatorType) {
throw new AssertionError("This method should not be called.");
}

@Override
public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ public TaskManager getTaskManager() {
throw new AssertionError("This method should not be called.");
}

@Override
public void sortTasksBy(String comparatorType) {
throw new AssertionError("This method should not be called.");
}

@Override
public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ public TaskManager getTaskManager() {
throw new AssertionError("This method should not be called.");
}

@Override
public void sortTasksBy(String comparatorType) {
throw new AssertionError("This method should not be called.");
}

@Override
public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ public TaskManager getTaskManager() {
throw new AssertionError("This method should not be called.");
}

@Override
public void sortTasksBy(String comparatorType) {
throw new AssertionError("This method should not be called.");
}

@Override
public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.TypicalEvents.getTypicalCalendar;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;
import static seedu.address.testutil.TypicalTasks.getTypicalTaskManager;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.task.Task;


class SortTasksCommandTest {
private static final String COMPARATOR_TYPE_DESCRIPTION = "Description";
private static final String COMPARATOR_TYPE_DEADLINE = "Deadline";
private static final String COMPARATOR_TYPE_INVALID = "Invalid";
private Model model = new ModelManager(getTypicalAddressBook(), getTypicalCalendar(), getTypicalTaskManager(),
new UserPrefs());

@Test
public void equals() {
SortTasksCommand sortTasksByDescriptionCommand = new SortTasksCommand(COMPARATOR_TYPE_DESCRIPTION);
SortTasksCommand sortTasksByDeadlineCommand = new SortTasksCommand(COMPARATOR_TYPE_DEADLINE);
SortTasksCommand invalidSortTasksCommand = new SortTasksCommand(COMPARATOR_TYPE_INVALID);
Object notSortTasksCommand = new Object();

assertTrue(sortTasksByDescriptionCommand.equals(sortTasksByDescriptionCommand));

assertFalse(sortTasksByDeadlineCommand.equals(sortTasksByDescriptionCommand));

assertFalse(invalidSortTasksCommand.equals(sortTasksByDeadlineCommand));

assertFalse(sortTasksByDescriptionCommand.equals(notSortTasksCommand));

assertFalse(sortTasksByDescriptionCommand.equals(null));
}

@Test
public void execute_nullInput_throwsNullPointerException() {
assertThrows(NullPointerException.class, () ->
new SortTasksCommand(COMPARATOR_TYPE_DESCRIPTION).execute(null));
}

@Test
public void execute_invalidType_throwsCommandException() {
assertThrows(CommandException.class, () ->
new SortTasksCommand(COMPARATOR_TYPE_INVALID).execute(model));
}

@Test
public void execute_validType_success() throws CommandException {
new SortTasksCommand(COMPARATOR_TYPE_DESCRIPTION).execute(model);
assertTrue(model.getTaskManager().getSortingOrder().equals(new Task.TaskDescriptorComparator()));
}

@Test
public void toStringMethod() {
SortTasksCommand sortTasksCommand = new SortTasksCommand(COMPARATOR_TYPE_DESCRIPTION);
String expected = SortTasksCommand.class.getCanonicalName() + "{comparatorType=" + COMPARATOR_TYPE_DESCRIPTION
+ "}";
assertEquals(expected, sortTasksCommand.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.SortTasksCommand;

class SortTasksCommandParserTest {
private static final String COMPARATOR_TYPE_DESCRIPTION = "Description";
private SortTasksCommandParser parser = new SortTasksCommandParser();

@Test
public void parse_emptyArg_throwsParseException() {
assertParseFailure(parser, " ",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortTasksCommand.MESSAGE_USAGE));
}

@Test
public void parse_validArgs_returnsSortTasksCommand() {
SortTasksCommand expectedSortTasksCommand = new SortTasksCommand(COMPARATOR_TYPE_DESCRIPTION);
assertParseSuccess(parser, "Description", expectedSortTasksCommand);

assertParseSuccess(parser, " \n \t Description", expectedSortTasksCommand);
}
}
Loading

0 comments on commit 04fcc5a

Please sign in to comment.