forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Fallman2/branch-SortTaskList
Merge sorting of task list into Task List branch
- Loading branch information
Showing
16 changed files
with
354 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/main/java/seedu/address/logic/commands/SortTasksCommand.java
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,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(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/seedu/address/logic/parser/SortTasksCommandParser.java
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,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); | ||
} | ||
} |
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
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
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
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
7 changes: 7 additions & 0 deletions
7
src/main/java/seedu/address/model/task/exceptions/InvalidSortingOrderException.java
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,7 @@ | ||
package seedu.address.model.task.exceptions; | ||
|
||
/** | ||
* An exception thrown when a sorting order provided is invalid. | ||
*/ | ||
public class InvalidSortingOrderException extends RuntimeException { | ||
} |
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
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
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
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
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
70 changes: 70 additions & 0 deletions
70
src/test/java/seedu/address/logic/commands/SortTasksCommandTest.java
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,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()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/seedu/address/logic/parser/SortTasksCommandParserTest.java
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,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); | ||
} | ||
} |
Oops, something went wrong.