forked from AY2324S2-CS2103T-W12-1/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
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 AY2324S2-CS2103T-W12-1#93 from solomonng2001/add-s…
…ort-by-priority Add sorting for person attributes
- Loading branch information
Showing
32 changed files
with
1,123 additions
and
17 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
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,92 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SORT_ORDER; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.PersonComparator; | ||
import seedu.address.model.person.SortCriteria; | ||
import seedu.address.model.person.SortOrder; | ||
|
||
/** | ||
* Sorts the client list in the address book. | ||
*/ | ||
public class SortCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "sort"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": sorts the client list in the address book. " | ||
+ "Parameters: " | ||
+ "CRITERIA " | ||
+ PREFIX_SORT_ORDER + "ORDER\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ "priority " | ||
+ PREFIX_SORT_ORDER + "desc"; | ||
|
||
private static final String MESSAGE_SUCCESS = "Client list sorted by %s in %s order."; | ||
private final SortCriteria sortCriteria; | ||
private final SortOrder sortOrder; | ||
|
||
/** | ||
* Creates a SortCommand to sort the client list. | ||
*/ | ||
public SortCommand(SortCriteria sortCriteria, SortOrder sortOrder) { | ||
requireNonNull(sortCriteria); | ||
requireNonNull(sortOrder); | ||
|
||
this.sortCriteria = sortCriteria; | ||
this.sortOrder = sortOrder; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
model.sortFilteredPersonList(PersonComparator.getComparator(sortCriteria, sortOrder)); | ||
return new CommandResult(getMessageSuccess(sortCriteria, sortOrder)); | ||
} | ||
|
||
/** | ||
* Returns a success message based on the sort criteria and sort order. | ||
* | ||
* @param sortCriteria the sort criteria | ||
* @param sortOrder the sort order | ||
* @return the success message | ||
*/ | ||
public static String getMessageSuccess(SortCriteria sortCriteria, SortOrder sortOrder) | ||
throws IllegalArgumentException { | ||
if (sortCriteria == null || sortOrder == null) { | ||
throw new IllegalArgumentException("SortCriteria and SortOrder cannot be null."); | ||
} | ||
if (sortCriteria == SortCriteria.INVALID || sortOrder == SortOrder.INVALID) { | ||
return String.format(MESSAGE_SUCCESS, SortCriteria.NAME, SortOrder.ASC); | ||
} | ||
return String.format(MESSAGE_SUCCESS, sortCriteria, sortOrder); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof SortCommand)) { | ||
return false; | ||
} | ||
|
||
SortCommand otherSortCommand = (SortCommand) other; | ||
return sortCriteria.equals(otherSortCommand.sortCriteria) | ||
&& sortOrder.equals(otherSortCommand.sortOrder); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("sortCriteria", sortCriteria) | ||
.add("sortOrder", sortOrder) | ||
.toString(); | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
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,44 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SORT_ORDER; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.SortCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.SortCriteria; | ||
import seedu.address.model.person.SortOrder; | ||
|
||
/** | ||
* Parses input arguments and creates a new SortCommand object | ||
*/ | ||
public class SortCommandParser implements Parser<SortCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the SortCommand | ||
* and returns an SortCommand object for execution. | ||
*/ | ||
public SortCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_SORT_ORDER); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_SORT_ORDER) | ||
|| argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_SORT_ORDER); | ||
SortCriteria sortCriteria = ParserUtil.parseSortCriteria(argMultimap.getPreamble()); | ||
SortOrder sortOrder = ParserUtil.parseSortOrder(argMultimap.getValue(PREFIX_SORT_ORDER).get()); | ||
|
||
return new SortCommand(sortCriteria, sortOrder); | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
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
Oops, something went wrong.