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.
Unable to sort clients by their attributes. Addition of sort will allow convenient access when looking up clients. Let's * make person attributes comparable, by implementing the interface. * Add sorting command and parser. * Add person comparator to abstract to logic for handling comparators. * Add tests in model, commands and parser. * Update ui to refresh client list after every command execute.
- Loading branch information
1 parent
fc6890d
commit 2552b3d
Showing
29 changed files
with
805 additions
and
12 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SORT_ORDER; | ||
|
||
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"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Client list sorted."; | ||
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) { | ||
requireNonNull(model); | ||
model.sortFilteredPersonList(PersonComparator.getComparator(sortCriteria, sortOrder)); | ||
return new CommandResult(MESSAGE_SUCCESS); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
src/main/java/seedu/address/model/person/PersonComparator.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,51 @@ | ||
package seedu.address.model.person; | ||
|
||
import java.util.Comparator; | ||
|
||
/** | ||
* Comparator for comparing persons. | ||
*/ | ||
public class PersonComparator { | ||
/** | ||
* Returns a comparator based on the sort criteria and sort order. | ||
* | ||
* @param sortCriteria the sort criteria | ||
* @param sortOrder the sort order | ||
* @return the comparator | ||
*/ | ||
public static Comparator<Person> getComparator(SortCriteria sortCriteria, SortOrder sortOrder) { | ||
Comparator<Person> comparator; | ||
switch (sortCriteria) { | ||
case NAME: | ||
comparator = Comparator.comparing(Person::getName); | ||
break; | ||
case PHONE: | ||
comparator = Comparator.comparing(Person::getPhone); | ||
break; | ||
case EMAIL: | ||
comparator = Comparator.comparing(Person::getEmail); | ||
break; | ||
case ADDRESS: | ||
comparator = Comparator.comparing(Person::getAddress); | ||
break; | ||
case PRIORITY: | ||
comparator = Comparator.comparing(Person::getPriority); | ||
break; | ||
case BIRTHDAY: | ||
comparator = Comparator.comparing(Person::getBirthday); | ||
break; | ||
case LASTMET: | ||
comparator = Comparator.comparing(Person::getLastMet); | ||
break; | ||
case SCHEDULE: | ||
comparator = Comparator.comparing(Person::getSchedule); | ||
break; | ||
default: | ||
return Comparator.comparing(Person::getName); | ||
} | ||
if (sortOrder == SortOrder.DESC) { | ||
return comparator.reversed(); | ||
} | ||
return 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
Oops, something went wrong.