forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #114 from aexolate/branch-sort-command
Add Sort Command
- Loading branch information
Showing
36 changed files
with
571 additions
and
48 deletions.
There are no files selected for viewing
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
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
65 changes: 65 additions & 0 deletions
65
src/main/java/seedu/address/logic/commands/SortCommand.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,65 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_DEPARTMENT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ID; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ROLE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SALARY; | ||
|
||
import java.util.Comparator; | ||
|
||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Person; | ||
|
||
/** | ||
* Sorts the list that is displayed to the user. | ||
*/ | ||
public class SortCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "sort"; | ||
|
||
public static final String MESSAGE_SUCCESS = "List successfully sorted."; | ||
public static final String MESSAGE_MULTIPLE_PREFIXES = "Sorting by multiple prefixes is not supported."; | ||
|
||
public static final String MESSAGE_USAGE = "Sorts the list displayed based on the prefix provided." | ||
+ "\n" + "Parameters: " | ||
+ "[" + PREFIX_ID + "] " + "or " + "[" + PREFIX_DEPARTMENT + "] " + "or " + "[" + PREFIX_EMAIL + "] " | ||
+ "or " + "[" + PREFIX_SALARY + "] " + "or " + "[" + PREFIX_ROLE + "] " | ||
+ "or " + "[" + PREFIX_NAME + "] " + "or " + "[" + PREFIX_PHONE + "] " | ||
+ "\n" | ||
|
||
+ "Examples: " + "\n" + COMMAND_WORD + " " + PREFIX_ID | ||
+ "\n" + COMMAND_WORD + " " + PREFIX_NAME; | ||
|
||
private final Comparator<Person> comparator; | ||
|
||
public SortCommand(Comparator<Person> comparator) { | ||
this.comparator = comparator; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
model.updateSortedPersonList(comparator); | ||
return new CommandResult(MESSAGE_SUCCESS); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof SortCommand)) { | ||
return false; | ||
} | ||
|
||
SortCommand otherFindCommand = (SortCommand) other; | ||
return comparator.equals(otherFindCommand.comparator); | ||
} | ||
|
||
} |
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
74 changes: 74 additions & 0 deletions
74
src/main/java/seedu/address/logic/parser/SortCommandParser.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,74 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_DEPARTMENT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ID; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ROLE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SALARY; | ||
|
||
import seedu.address.logic.commands.SortCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.PersonComparators; | ||
|
||
|
||
/** | ||
* Parses input arguments and creates a new SortCommand object | ||
*/ | ||
public class SortCommandParser implements Parser<SortCommand> { | ||
|
||
private static final int EXPECTED_PREFIX_COUNT = 1; | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the SortCommand | ||
* and returns a SortCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public SortCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_ID, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_DEPARTMENT, | ||
PREFIX_ROLE, PREFIX_SALARY); | ||
|
||
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_ID, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_DEPARTMENT, | ||
PREFIX_ROLE, PREFIX_SALARY); | ||
|
||
if (argMultimap.getPrefixCount() != EXPECTED_PREFIX_COUNT) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
if (argMultimap.hasPrefix(PREFIX_ID)) { | ||
return new SortCommand(PersonComparators.COMPARATOR_ID); | ||
} | ||
|
||
if (argMultimap.hasPrefix(PREFIX_NAME)) { | ||
return new SortCommand(PersonComparators.COMPARATOR_NAME); | ||
} | ||
|
||
if (argMultimap.hasPrefix(PREFIX_DEPARTMENT)) { | ||
return new SortCommand(PersonComparators.COMPARATOR_DEPARTMENT); | ||
} | ||
|
||
if (argMultimap.hasPrefix(PREFIX_EMAIL)) { | ||
return new SortCommand(PersonComparators.COMPARATOR_EMAIL); | ||
} | ||
|
||
if (argMultimap.hasPrefix(PREFIX_ROLE)) { | ||
return new SortCommand(PersonComparators.COMPARATOR_ROLE); | ||
} | ||
|
||
if (argMultimap.hasPrefix(PREFIX_SALARY)) { | ||
return new SortCommand(PersonComparators.COMPARATOR_SALARY); | ||
} | ||
|
||
if (argMultimap.hasPrefix(PREFIX_PHONE)) { | ||
return new SortCommand(PersonComparators.COMPARATOR_PHONE); | ||
} | ||
|
||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
} |
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
Oops, something went wrong.