From 5a0107013865f3d6ebdf1ee91de361df0e34e0de Mon Sep 17 00:00:00 2001 From: KT27Learn Date: Wed, 13 Oct 2021 02:50:36 +0800 Subject: [PATCH 1/5] Update sort command skeleton --- .../address/logic/commands/SortCommand.java | 24 +++- .../logic/parser/AddressBookParser.java | 4 + .../logic/parser/DeleteCommandParser.java | 8 -- .../address/logic/parser/ParserUtil.java | 18 ++- .../logic/parser/SortCommandParser.java | 110 +++++++++++++++++- .../java/seedu/address/model/AddressBook.java | 1 + src/main/java/seedu/address/model/Model.java | 7 ++ .../seedu/address/model/ModelManager.java | 16 ++- .../address/model/person/RiskAppetite.java | 4 + .../person/comparators/SortByAddress.java | 33 ++++++ .../person/comparators/SortByClientID.java | 23 ++++ .../person/comparators/SortByCurrentPlan.java | 33 ++++++ .../comparators/SortByDisposableIncome.java | 35 ++++++ .../model/person/comparators/SortByEmail.java | 23 ++++ .../person/comparators/SortByLastMet.java | 33 ++++++ .../model/person/comparators/SortByName.java | 23 ++++ .../model/person/comparators/SortByPhone.java | 34 ++++++ .../comparators/SortByRiskAppetite.java | 35 ++++++ .../person/comparators/SortDirection.java | 29 +++++ 19 files changed, 476 insertions(+), 17 deletions(-) create mode 100644 src/main/java/seedu/address/model/person/comparators/SortByAddress.java create mode 100644 src/main/java/seedu/address/model/person/comparators/SortByClientID.java create mode 100644 src/main/java/seedu/address/model/person/comparators/SortByCurrentPlan.java create mode 100644 src/main/java/seedu/address/model/person/comparators/SortByDisposableIncome.java create mode 100644 src/main/java/seedu/address/model/person/comparators/SortByEmail.java create mode 100644 src/main/java/seedu/address/model/person/comparators/SortByLastMet.java create mode 100644 src/main/java/seedu/address/model/person/comparators/SortByName.java create mode 100644 src/main/java/seedu/address/model/person/comparators/SortByPhone.java create mode 100644 src/main/java/seedu/address/model/person/comparators/SortByRiskAppetite.java create mode 100644 src/main/java/seedu/address/model/person/comparators/SortDirection.java diff --git a/src/main/java/seedu/address/logic/commands/SortCommand.java b/src/main/java/seedu/address/logic/commands/SortCommand.java index 959c4d04f3d..ea3dc6d67f8 100644 --- a/src/main/java/seedu/address/logic/commands/SortCommand.java +++ b/src/main/java/seedu/address/logic/commands/SortCommand.java @@ -1,7 +1,15 @@ package seedu.address.logic.commands; import seedu.address.commons.core.Messages; +import seedu.address.logic.parser.Prefix; import seedu.address.model.Model; +import seedu.address.model.person.Person; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.function.Predicate; + +import static java.util.Objects.requireNonNull; /** * Sorts all persons in address book whose according to the specified attribute in either ascending or descending @@ -16,18 +24,24 @@ public class SortCommand extends Command { + "Parameters: /{ASC/DESC}\n" + "Example: " + COMMAND_WORD + " ra/ ASC"; + public String MESSAGE_SUCCESS; + + private final Comparator sorter; + private final String field; - public SortCommand() { + public SortCommand(Comparator sorter, String field) { + this.sorter = sorter; + this.field = field; + this.MESSAGE_SUCCESS = "list sorted by " + field; } @Override public CommandResult execute(Model model) { - - - return new CommandResult( - String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); + requireNonNull(model); + model.sortFilteredPersonList(sorter); + return new CommandResult(MESSAGE_SUCCESS); } @Override diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index 48fc51c24c8..ee082ca5afe 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -17,6 +17,7 @@ import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.ListCommand; import seedu.address.logic.commands.SearchCommand; +import seedu.address.logic.commands.SortCommand; import seedu.address.logic.commands.ViewCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.Model; @@ -72,6 +73,9 @@ public Command parseCommand(String userInput) throws ParseException { case SearchCommand.COMMAND_WORD: return new SearchCommandParser().parse(arguments); + case SortCommand.COMMAND_WORD: + return new SortCommandParser().parse(arguments); + case FilterCommand.COMMAND_WORD: return new FilterCommandParser().parse(arguments); diff --git a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java index 4edb2e02da8..bbbd7eadf97 100644 --- a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java @@ -69,14 +69,6 @@ public DeleteCommand parse(String args) throws ParseException { } - /** - * 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()); - } - /** * Returns true if any of the prefixes contains empty {@code Optional} values in the given * {@code ArgumentMultimap}. diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 89cc5ab9899..48bf1a89049 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -18,6 +18,7 @@ import seedu.address.model.person.Name; import seedu.address.model.person.Phone; import seedu.address.model.person.RiskAppetite; +import seedu.address.model.person.comparators.SortDirection; import seedu.address.model.tag.Tag; /** @@ -171,7 +172,7 @@ public static RiskAppetite parseRiskAppetite(String riskAppetite) throws ParseEx } /** - * Parses a {@code String disposableIncome} into an {@code disposableIncome}. + * Parses a {@code String disposableIncome} into an {@code DisposableIncome}. * Leading and trailing whitespaces will be trimmed. * * @throws ParseException if the given {@code DisposableIncome} is invalid. @@ -185,6 +186,21 @@ public static DisposableIncome parseDisposableIncome(String disposableIncome) th return new DisposableIncome(trimmedDisposableIncome); } + /** + * Parses a {@code String sortDirection} into an {@code SortDirection}. + * Leading and trailing whitespaces will be trimmed. + * + * @throws ParseException if the given {@code DisposableIncome} is invalid. + */ + public static SortDirection parseSortDirection(String sortDirection) throws ParseException { + requireNonNull(sortDirection); + String trimmedSortDirection = sortDirection.trim(); + if (!SortDirection.isValidDirection(trimmedSortDirection)) { + throw new ParseException(SortDirection.MESSAGE_CONSTRAINTS); + } + return new SortDirection(trimmedSortDirection); + } + /** * Parses a {@code String tag} into a {@code Tag}. * Leading and trailing whitespaces will be trimmed. diff --git a/src/main/java/seedu/address/logic/parser/SortCommandParser.java b/src/main/java/seedu/address/logic/parser/SortCommandParser.java index ef77d03053c..55e0eb2636b 100644 --- a/src/main/java/seedu/address/logic/parser/SortCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/SortCommandParser.java @@ -1,13 +1,45 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_TOO_MANY_FIELDS; +import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; +import static seedu.address.logic.parser.CliSyntax.PREFIX_CLIENTID; +import static seedu.address.logic.parser.CliSyntax.PREFIX_CURRENTPLAN; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DISPOSABLEINCOME; +import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; +import static seedu.address.logic.parser.CliSyntax.PREFIX_LASTMET; +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_RISKAPPETITE; +import static seedu.address.logic.parser.ParserUtil.parseSortDirection; import seedu.address.logic.commands.FindCommand; import seedu.address.logic.commands.SortCommand; import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.person.Person; +import seedu.address.model.person.comparators.SortByAddress; +import seedu.address.model.person.comparators.SortByClientID; +import seedu.address.model.person.comparators.SortByCurrentPlan; +import seedu.address.model.person.comparators.SortByDisposableIncome; +import seedu.address.model.person.comparators.SortByEmail; +import seedu.address.model.person.comparators.SortByLastMet; +import seedu.address.model.person.comparators.SortByName; +import seedu.address.model.person.comparators.SortByPhone; +import seedu.address.model.person.comparators.SortByRiskAppetite; +import seedu.address.model.person.comparators.SortDirection; + +import java.util.Comparator; +import java.util.stream.Stream; public class SortCommandParser implements Parser { + private static final Prefix[] ALL_PREFIXES = { + PREFIX_CLIENTID, PREFIX_NAME, PREFIX_EMAIL, PREFIX_PHONE, PREFIX_ADDRESS, PREFIX_RISKAPPETITE, + PREFIX_DISPOSABLEINCOME, PREFIX_CURRENTPLAN, PREFIX_LASTMET + }; + + private String sortedField; + /** * Parses the given {@code String} of arguments in the context of the FindCommand * and returns a SortCommand object for execution. @@ -20,8 +52,82 @@ public SortCommand parse(String args) throws ParseException { String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); } - String[] nameKeywords = trimmedArgs.split("\\s+"); - return new SortCommand(); + ArgumentMultimap argMultimap = + ArgumentTokenizer.tokenize(args, PREFIX_CLIENTID, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, + PREFIX_ADDRESS, PREFIX_RISKAPPETITE, PREFIX_DISPOSABLEINCOME, PREFIX_CURRENTPLAN, PREFIX_LASTMET); + if (!anyPrefixesPresent(argMultimap, PREFIX_CLIENTID, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, + PREFIX_RISKAPPETITE, PREFIX_DISPOSABLEINCOME, PREFIX_CURRENTPLAN, PREFIX_LASTMET) + || !argMultimap.getPreamble().isEmpty()) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE)); + } + + Comparator sorter = parseSorter(argMultimap); + + return new SortCommand(sorter, sortedField); + } + + /** + * Returns true if any of the prefixes contains empty {@code Optional} values in the given + * {@code ArgumentMultimap}. + */ + private static boolean anyPrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { + return Stream.of(prefixes).anyMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); + } + + /** + * Constructs a {@code Comparator} from based off inputted {@code Prefix} with given {@code SortDirection} + */ + private Comparator createSorter(Prefix prefix, SortDirection sortDirection) throws ParseException { + if (PREFIX_CLIENTID.equals(prefix)) { + sortedField = "Client Id"; + return new SortByClientID(sortDirection); + } else if (PREFIX_NAME.equals(prefix)) { + sortedField = "Name"; + return new SortByName(sortDirection); + } else if (PREFIX_EMAIL.equals(prefix)) { + sortedField = "Email"; + return new SortByEmail(sortDirection); + } else if (PREFIX_PHONE.equals(prefix)) { + sortedField = "Phone"; + return new SortByPhone(sortDirection); + } else if (PREFIX_ADDRESS.equals(prefix)) { + sortedField = "Address"; + return new SortByAddress(sortDirection); + } else if (PREFIX_RISKAPPETITE.equals(prefix)) { + sortedField = "Risk Appetite"; + return new SortByRiskAppetite(sortDirection); + } else if (PREFIX_DISPOSABLEINCOME.equals(prefix)) { + sortedField = "Disposable Income"; + return new SortByDisposableIncome(sortDirection); + } else if (PREFIX_CURRENTPLAN.equals(prefix)) { + sortedField = "Current Plan"; + return new SortByCurrentPlan(sortDirection); + } else if (PREFIX_LASTMET.equals(prefix)) { + sortedField = "Last Met"; + return new SortByLastMet(sortDirection); + } else { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE)); + } + + } + + /** + * Constructs a {@code Comparator} from parsed prefixes in {@code ArgumentMultiMap }based off inputted prefix + * with appriopriate {@code SortDirection} + */ + private Comparator parseSorter(ArgumentMultimap argumentMultimap) throws ParseException { + Comparator sorter = null; + for (int i = 0; i < ALL_PREFIXES.length; i++) { + Prefix currentPrefix = ALL_PREFIXES[i]; + if (!argumentMultimap.getValue(currentPrefix).isEmpty() && sorter == null) { + SortDirection sortDirection = parseSortDirection(argumentMultimap.getValue(currentPrefix).get()); + sorter = createSorter(currentPrefix, sortDirection); + } else if (!argumentMultimap.getValue(currentPrefix).isEmpty() && !(sorter == null)) { + //too many fields + throw new ParseException(String.format(MESSAGE_TOO_MANY_FIELDS, SortCommand.MESSAGE_USAGE)); + } + } + return sorter; } } diff --git a/src/main/java/seedu/address/model/AddressBook.java b/src/main/java/seedu/address/model/AddressBook.java index 85edcd8fc43..adbf6ee8656 100644 --- a/src/main/java/seedu/address/model/AddressBook.java +++ b/src/main/java/seedu/address/model/AddressBook.java @@ -3,6 +3,7 @@ import static java.util.Objects.requireNonNull; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.function.Predicate; diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 65cbb02a0aa..ed58ee78717 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -2,6 +2,7 @@ import java.nio.file.Path; import java.util.ArrayList; +import java.util.Comparator; import java.util.function.Predicate; import javafx.collections.ObservableList; @@ -98,6 +99,12 @@ public interface Model { */ void filterFilteredPersonList(Predicate predicate); + /** + * Sorts the filtered person list to sort by the given {@code sorter}. + * @throws NullPointerException if {@code predicate} is null. + */ + void sortFilteredPersonList(Comparator sorter); + /** Returns an unmodifiable view of the person to view */ ObservableList getPersonToView(); diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 27460302c2f..efaf5ef9f21 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -5,9 +5,11 @@ import java.nio.file.Path; import java.util.ArrayList; +import java.util.Comparator; import java.util.function.Predicate; import java.util.logging.Logger; +import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.collections.transformation.FilteredList; import seedu.address.commons.core.GuiSettings; @@ -22,7 +24,7 @@ public class ModelManager implements Model { private final AddressBook addressBook; private final UserPrefs userPrefs; - private final FilteredList filteredPersons; + private FilteredList filteredPersons; private final FilteredList personToView; /** @@ -147,6 +149,18 @@ public void filterFilteredPersonList(Predicate predicate) { filteredPersons.setPredicate(predicate.and(currentPredicate)); } + @Override + public void sortFilteredPersonList(Comparator sorter) { + requireNonNull(sorter); + ArrayList tempList = new ArrayList<>(); + filteredPersons.forEach(person -> tempList.add(person)); + tempList.sort(sorter); + ObservableList sortedData = FXCollections.observableArrayList(); + tempList.stream().forEach(person -> sortedData.add(person)); + FilteredList tempPersonList = new FilteredList(sortedData); + filteredPersons = tempPersonList; + } + //=========== Person To View List Accessors ============================================================= /** diff --git a/src/main/java/seedu/address/model/person/RiskAppetite.java b/src/main/java/seedu/address/model/person/RiskAppetite.java index 1010d8dde52..5e8d8b670fc 100644 --- a/src/main/java/seedu/address/model/person/RiskAppetite.java +++ b/src/main/java/seedu/address/model/person/RiskAppetite.java @@ -1,5 +1,7 @@ package seedu.address.model.person; +import org.jetbrains.annotations.NotNull; + import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.AppUtil.checkArgument; @@ -46,4 +48,6 @@ public boolean equals(Object other) { public int hashCode() { return value.hashCode(); } + + } diff --git a/src/main/java/seedu/address/model/person/comparators/SortByAddress.java b/src/main/java/seedu/address/model/person/comparators/SortByAddress.java new file mode 100644 index 00000000000..2b5e57004e2 --- /dev/null +++ b/src/main/java/seedu/address/model/person/comparators/SortByAddress.java @@ -0,0 +1,33 @@ +package seedu.address.model.person.comparators; + +import seedu.address.model.person.Person; + +import java.util.Comparator; + +import static java.util.Objects.requireNonNull; + +public class SortByAddress implements Comparator { + private SortDirection direction; + + public SortByAddress(SortDirection direction) { + requireNonNull(direction); + this.direction = direction; + } + + @Override + public int compare(Person a, Person b) { + //cause i need the sort direction here, i implement compareTo in riskAppetite + //cause if not i need double check here for empty values + //wrt person a + if (a.getAddress().value.isEmpty()) { + return 1; + } else { + if (b.getAddress().value.isEmpty()) { + return -1; + } + + int result = a.getAddress().value.compareTo(b.getAddress().value); + return direction.isAscending() ? result : -result; + } + } +} diff --git a/src/main/java/seedu/address/model/person/comparators/SortByClientID.java b/src/main/java/seedu/address/model/person/comparators/SortByClientID.java new file mode 100644 index 00000000000..6a005943ae6 --- /dev/null +++ b/src/main/java/seedu/address/model/person/comparators/SortByClientID.java @@ -0,0 +1,23 @@ +package seedu.address.model.person.comparators; + +import seedu.address.model.person.Person; + +import java.util.Comparator; + +import static java.util.Objects.requireNonNull; + +public class SortByClientID implements Comparator { + private SortDirection direction; + + public SortByClientID(SortDirection direction) { + requireNonNull(direction); + this.direction = direction; + } + + @Override + public int compare(Person a, Person b) { + int result = a.getClientId().value.compareTo(b.getClientId().value); + return direction.isAscending() ? result : -result; + } + +} diff --git a/src/main/java/seedu/address/model/person/comparators/SortByCurrentPlan.java b/src/main/java/seedu/address/model/person/comparators/SortByCurrentPlan.java new file mode 100644 index 00000000000..e0c980da899 --- /dev/null +++ b/src/main/java/seedu/address/model/person/comparators/SortByCurrentPlan.java @@ -0,0 +1,33 @@ +package seedu.address.model.person.comparators; + +import seedu.address.model.person.Person; + +import java.util.Comparator; + +import static java.util.Objects.requireNonNull; + +public class SortByCurrentPlan implements Comparator { + private SortDirection direction; + + public SortByCurrentPlan(SortDirection direction) { + requireNonNull(direction); + this.direction = direction; + } + + @Override + public int compare(Person a, Person b) { + //cause i need the sort direction here, i implement compareTo in riskAppetite + //cause if not i need double check here for empty values + //wrt person a + if (a.getCurrentPlan().value.isEmpty()) { + return 1; + } else { + if (b.getCurrentPlan().value.isEmpty()) { + return -1; + } + int result = a.getCurrentPlan().value.compareTo(b.getCurrentPlan().value); + return direction.isAscending() ? result : -result; + } + } + +} diff --git a/src/main/java/seedu/address/model/person/comparators/SortByDisposableIncome.java b/src/main/java/seedu/address/model/person/comparators/SortByDisposableIncome.java new file mode 100644 index 00000000000..ed192593aed --- /dev/null +++ b/src/main/java/seedu/address/model/person/comparators/SortByDisposableIncome.java @@ -0,0 +1,35 @@ +package seedu.address.model.person.comparators; + +import seedu.address.model.person.Person; + +import java.util.Comparator; + +import static java.util.Objects.requireNonNull; + +public class SortByDisposableIncome implements Comparator { + + private SortDirection direction; + + public SortByDisposableIncome(SortDirection direction) { + requireNonNull(direction); + this.direction = direction; + } + + @Override + public int compare(Person a, Person b) { + //cause i need the sort direction here, i implement compareTo in riskAppetite + //cause if not i need double check here for empty values + //wrt person a + if (a.getDisposableIncome().value.isEmpty()) { + return 1; + } else { + if (b.getDisposableIncome().value.isEmpty()) { + return -1; + } + + int result = a.getDisposableIncome().value.compareTo(b.getDisposableIncome().value); + return direction.isAscending() ? result : -result; + } + } + +} diff --git a/src/main/java/seedu/address/model/person/comparators/SortByEmail.java b/src/main/java/seedu/address/model/person/comparators/SortByEmail.java new file mode 100644 index 00000000000..64d7c1a8fcf --- /dev/null +++ b/src/main/java/seedu/address/model/person/comparators/SortByEmail.java @@ -0,0 +1,23 @@ +package seedu.address.model.person.comparators; + +import seedu.address.model.person.Person; + +import java.util.Comparator; + +import static java.util.Objects.requireNonNull; + +public class SortByEmail implements Comparator { + + private SortDirection direction; + + public SortByEmail(SortDirection direction) { + requireNonNull(direction); + this.direction = direction; + } + + @Override + public int compare(Person a, Person b) { + int result = a.getEmail().value.compareTo(b.getEmail().value); + return direction.isAscending() ? result : -result; + } +} diff --git a/src/main/java/seedu/address/model/person/comparators/SortByLastMet.java b/src/main/java/seedu/address/model/person/comparators/SortByLastMet.java new file mode 100644 index 00000000000..8492059487f --- /dev/null +++ b/src/main/java/seedu/address/model/person/comparators/SortByLastMet.java @@ -0,0 +1,33 @@ +package seedu.address.model.person.comparators; + +import seedu.address.model.person.Person; + +import java.util.Comparator; + +import static java.util.Objects.requireNonNull; + +public class SortByLastMet implements Comparator { + private SortDirection direction; + + public SortByLastMet(SortDirection direction) { + requireNonNull(direction); + this.direction = direction; + } + + @Override + public int compare(Person a, Person b) { + //cause i need the sort direction here, i implement compareTo in riskAppetite + //cause if not i need double check here for empty values + //wrt person a + if (a.getLastMet().value == null) { + return 1; + } else { + if (b.getLastMet().value == null) { + return -1; + } + + int result = a.getLastMet().value.compareTo(b.getLastMet().value); + return direction.isAscending() ? result : -result; + } + } +} diff --git a/src/main/java/seedu/address/model/person/comparators/SortByName.java b/src/main/java/seedu/address/model/person/comparators/SortByName.java new file mode 100644 index 00000000000..da10604f8f9 --- /dev/null +++ b/src/main/java/seedu/address/model/person/comparators/SortByName.java @@ -0,0 +1,23 @@ +package seedu.address.model.person.comparators; + +import seedu.address.model.person.Person; + +import java.util.Comparator; + +import static java.util.Objects.requireNonNull; + +public class SortByName implements Comparator { + + private SortDirection direction; + + public SortByName(SortDirection direction) { + requireNonNull(direction); + this.direction = direction; + } + + @Override + public int compare(Person a, Person b) { + int result = a.getName().fullName.compareTo(b.getName().fullName); + return direction.isAscending() ? result : -result; + } +} diff --git a/src/main/java/seedu/address/model/person/comparators/SortByPhone.java b/src/main/java/seedu/address/model/person/comparators/SortByPhone.java new file mode 100644 index 00000000000..31c5d209586 --- /dev/null +++ b/src/main/java/seedu/address/model/person/comparators/SortByPhone.java @@ -0,0 +1,34 @@ +package seedu.address.model.person.comparators; + +import seedu.address.model.person.Person; + +import java.util.Comparator; + +import static java.util.Objects.requireNonNull; + +public class SortByPhone implements Comparator { + private SortDirection direction; + + public SortByPhone(SortDirection direction) { + requireNonNull(direction); + this.direction = direction; + } + + @Override + public int compare(Person a, Person b) { + //cause i need the sort direction here, i implement compareTo in riskAppetite + //cause if not i need double check here for empty values + //wrt person a + if (a.getPhone().value.isEmpty()) { + return 1; + } else { + if (b.getPhone().value.isEmpty()) { + return -1; + } + + int result = a.getPhone().value.compareTo(b.getPhone().value); + return direction.isAscending() ? result : -result; + } + } + +} diff --git a/src/main/java/seedu/address/model/person/comparators/SortByRiskAppetite.java b/src/main/java/seedu/address/model/person/comparators/SortByRiskAppetite.java new file mode 100644 index 00000000000..5d6553a8f35 --- /dev/null +++ b/src/main/java/seedu/address/model/person/comparators/SortByRiskAppetite.java @@ -0,0 +1,35 @@ +package seedu.address.model.person.comparators; + +import seedu.address.model.person.Person; + +import java.util.Comparator; + +import static java.util.Objects.requireNonNull; + +public class SortByRiskAppetite implements Comparator { + + private SortDirection direction; + + public SortByRiskAppetite(SortDirection direction) { + requireNonNull(direction); + this.direction = direction; + } + + @Override + public int compare(Person a, Person b) { + //cause i need the sort direction here, i implement compareTo in riskAppetite + //cause if not i need double check here for empty values + //wrt person a + if (a.getRiskAppetite().value.isEmpty()) { + return 1; + } else { + if (b.getRiskAppetite().value.isEmpty()) { + return -1; + } + + int result = a.getRiskAppetite().value.compareTo(b.getRiskAppetite().value); + return direction.isAscending() ? result : -result; + } + } + +} diff --git a/src/main/java/seedu/address/model/person/comparators/SortDirection.java b/src/main/java/seedu/address/model/person/comparators/SortDirection.java new file mode 100644 index 00000000000..367827e31a3 --- /dev/null +++ b/src/main/java/seedu/address/model/person/comparators/SortDirection.java @@ -0,0 +1,29 @@ +package seedu.address.model.person.comparators; + +import static java.util.Objects.requireNonNull; +import static seedu.address.commons.util.AppUtil.checkArgument; + +public class SortDirection { + + private String direction; + + public static final String MESSAGE_CONSTRAINTS = "Sort direction should either be asc or dsc"; + + public SortDirection(String direction) { + requireNonNull(direction); + checkArgument(isValidDirection(direction), MESSAGE_CONSTRAINTS); + this.direction = direction; + } + + /** + * Returns true if a given string is a valid sorting direction. + */ + public static boolean isValidDirection(String test) { + return test.toLowerCase().equals("asc") || test.toLowerCase().equals("dsc"); + } + + public boolean isAscending() { + return direction.toLowerCase().equals("asc"); + } + +} From 68d5854e0101ce5614e8f062e147b48f14e8f297 Mon Sep 17 00:00:00 2001 From: benedictchuajj Date: Wed, 13 Oct 2021 12:04:05 +0800 Subject: [PATCH 2/5] Update filteredList to wrap around a SortedList for sorting --- .../java/seedu/address/model/ModelManager.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index efaf5ef9f21..51e7b62589b 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -12,6 +12,7 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.collections.transformation.FilteredList; +import javafx.collections.transformation.SortedList; import seedu.address.commons.core.GuiSettings; import seedu.address.commons.core.LogsCenter; import seedu.address.model.person.Person; @@ -24,7 +25,8 @@ public class ModelManager implements Model { private final AddressBook addressBook; private final UserPrefs userPrefs; - private FilteredList filteredPersons; + private final SortedList sortedPersons; + private final FilteredList filteredPersons; private final FilteredList personToView; /** @@ -38,7 +40,8 @@ public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyUserPrefs userPrefs this.addressBook = new AddressBook(addressBook); this.userPrefs = new UserPrefs(userPrefs); - filteredPersons = new FilteredList<>(this.addressBook.getPersonList()); + sortedPersons = new SortedList<>(this.addressBook.getPersonList()); + filteredPersons = new FilteredList<>(sortedPersons); personToView = new FilteredList<>(this.addressBook.getPersonList()); } @@ -151,14 +154,7 @@ public void filterFilteredPersonList(Predicate predicate) { @Override public void sortFilteredPersonList(Comparator sorter) { - requireNonNull(sorter); - ArrayList tempList = new ArrayList<>(); - filteredPersons.forEach(person -> tempList.add(person)); - tempList.sort(sorter); - ObservableList sortedData = FXCollections.observableArrayList(); - tempList.stream().forEach(person -> sortedData.add(person)); - FilteredList tempPersonList = new FilteredList(sortedData); - filteredPersons = tempPersonList; + sortedPersons.setComparator(sorter); } //=========== Person To View List Accessors ============================================================= From d71c7006c1c9cb5df470e1d3feb3f1385bc36054 Mon Sep 17 00:00:00 2001 From: benedictchuajj Date: Wed, 13 Oct 2021 12:30:22 +0800 Subject: [PATCH 3/5] Update code to pass checkstyle tests --- .../seedu/address/commons/core/Messages.java | 1 + .../address/logic/commands/SortCommand.java | 27 +++++++++---------- .../logic/parser/SortCommandParser.java | 10 +++---- .../java/seedu/address/model/AddressBook.java | 1 - .../seedu/address/model/ModelManager.java | 1 - .../address/model/person/RiskAppetite.java | 2 -- .../person/comparators/SortByAddress.java | 7 +++-- .../person/comparators/SortByClientID.java | 7 +++-- .../person/comparators/SortByCurrentPlan.java | 7 +++-- .../comparators/SortByDisposableIncome.java | 9 ++++--- .../model/person/comparators/SortByEmail.java | 7 +++-- .../person/comparators/SortByLastMet.java | 7 +++-- .../model/person/comparators/SortByName.java | 7 +++-- .../model/person/comparators/SortByPhone.java | 7 +++-- .../comparators/SortByRiskAppetite.java | 7 +++-- .../person/comparators/SortDirection.java | 6 +++-- .../logic/commands/AddCommandTest.java | 6 +++++ 17 files changed, 74 insertions(+), 45 deletions(-) diff --git a/src/main/java/seedu/address/commons/core/Messages.java b/src/main/java/seedu/address/commons/core/Messages.java index 4a99b3ebf75..06ad7aaf1a8 100644 --- a/src/main/java/seedu/address/commons/core/Messages.java +++ b/src/main/java/seedu/address/commons/core/Messages.java @@ -13,5 +13,6 @@ public class Messages { public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!"; public static final String MESSAGE_VIEW_SUCCESS = "Viewing person: %1$s"; public static final String MESSAGE_VIEW_INVALID_CLIENT_ID = "There's no contact with client ID %s"; + public static final String MESSAGE_SORT_SUCCESS = "List sorted by %s"; } diff --git a/src/main/java/seedu/address/logic/commands/SortCommand.java b/src/main/java/seedu/address/logic/commands/SortCommand.java index ea3dc6d67f8..111d1748c05 100644 --- a/src/main/java/seedu/address/logic/commands/SortCommand.java +++ b/src/main/java/seedu/address/logic/commands/SortCommand.java @@ -1,15 +1,12 @@ package seedu.address.logic.commands; -import seedu.address.commons.core.Messages; -import seedu.address.logic.parser.Prefix; -import seedu.address.model.Model; -import seedu.address.model.person.Person; +import static java.util.Objects.requireNonNull; -import java.util.ArrayList; import java.util.Comparator; -import java.util.function.Predicate; -import static java.util.Objects.requireNonNull; +import seedu.address.commons.core.Messages; +import seedu.address.model.Model; +import seedu.address.model.person.Person; /** * Sorts all persons in address book whose according to the specified attribute in either ascending or descending @@ -22,32 +19,32 @@ public class SortCommand extends Command { public static final String MESSAGE_USAGE = COMMAND_WORD + ": Sorts leads according to " + "the specified attribute and in either an ascending or descending order\n" + "Parameters: /{ASC/DESC}\n" - + "Example: " + COMMAND_WORD + " ra/ ASC"; - - public String MESSAGE_SUCCESS; + + "Example: " + COMMAND_WORD + " ra/ asc"; private final Comparator sorter; private final String field; + /** + * @param sorter to sort the persons list with. + * @param field which is the field that the list is sorted by. + */ public SortCommand(Comparator sorter, String field) { this.sorter = sorter; this.field = field; - this.MESSAGE_SUCCESS = "list sorted by " + field; - } @Override public CommandResult execute(Model model) { requireNonNull(model); model.sortFilteredPersonList(sorter); - return new CommandResult(MESSAGE_SUCCESS); + return new CommandResult(String.format(Messages.MESSAGE_SORT_SUCCESS, field)); } @Override public boolean equals(Object other) { return other == this // short circuit if same object - || other instanceof SortCommand; // instanceof handles nulls - //&& predicate.equals(((SortCommand) other).predicate)); // state check + || (other instanceof SortCommand // instanceof handles nulls + && sorter.equals(((SortCommand) other).sorter)); // state check } } diff --git a/src/main/java/seedu/address/logic/parser/SortCommandParser.java b/src/main/java/seedu/address/logic/parser/SortCommandParser.java index 55e0eb2636b..2543ae32c62 100644 --- a/src/main/java/seedu/address/logic/parser/SortCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/SortCommandParser.java @@ -13,6 +13,9 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_RISKAPPETITE; import static seedu.address.logic.parser.ParserUtil.parseSortDirection; +import java.util.Comparator; +import java.util.stream.Stream; + import seedu.address.logic.commands.FindCommand; import seedu.address.logic.commands.SortCommand; import seedu.address.logic.parser.exceptions.ParseException; @@ -28,14 +31,11 @@ import seedu.address.model.person.comparators.SortByRiskAppetite; import seedu.address.model.person.comparators.SortDirection; -import java.util.Comparator; -import java.util.stream.Stream; - public class SortCommandParser implements Parser { private static final Prefix[] ALL_PREFIXES = { - PREFIX_CLIENTID, PREFIX_NAME, PREFIX_EMAIL, PREFIX_PHONE, PREFIX_ADDRESS, PREFIX_RISKAPPETITE, - PREFIX_DISPOSABLEINCOME, PREFIX_CURRENTPLAN, PREFIX_LASTMET + PREFIX_CLIENTID, PREFIX_NAME, PREFIX_EMAIL, PREFIX_PHONE, PREFIX_ADDRESS, PREFIX_RISKAPPETITE, + PREFIX_DISPOSABLEINCOME, PREFIX_CURRENTPLAN, PREFIX_LASTMET }; private String sortedField; diff --git a/src/main/java/seedu/address/model/AddressBook.java b/src/main/java/seedu/address/model/AddressBook.java index adbf6ee8656..85edcd8fc43 100644 --- a/src/main/java/seedu/address/model/AddressBook.java +++ b/src/main/java/seedu/address/model/AddressBook.java @@ -3,7 +3,6 @@ import static java.util.Objects.requireNonNull; import java.util.ArrayList; -import java.util.Comparator; import java.util.List; import java.util.function.Predicate; diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 51e7b62589b..ccc951f34e2 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -9,7 +9,6 @@ import java.util.function.Predicate; import java.util.logging.Logger; -import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.collections.transformation.FilteredList; import javafx.collections.transformation.SortedList; diff --git a/src/main/java/seedu/address/model/person/RiskAppetite.java b/src/main/java/seedu/address/model/person/RiskAppetite.java index 5e8d8b670fc..c03ea08c3ef 100644 --- a/src/main/java/seedu/address/model/person/RiskAppetite.java +++ b/src/main/java/seedu/address/model/person/RiskAppetite.java @@ -1,7 +1,5 @@ package seedu.address.model.person; -import org.jetbrains.annotations.NotNull; - import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.AppUtil.checkArgument; diff --git a/src/main/java/seedu/address/model/person/comparators/SortByAddress.java b/src/main/java/seedu/address/model/person/comparators/SortByAddress.java index 2b5e57004e2..22096c8085b 100644 --- a/src/main/java/seedu/address/model/person/comparators/SortByAddress.java +++ b/src/main/java/seedu/address/model/person/comparators/SortByAddress.java @@ -1,14 +1,17 @@ package seedu.address.model.person.comparators; -import seedu.address.model.person.Person; +import static java.util.Objects.requireNonNull; import java.util.Comparator; -import static java.util.Objects.requireNonNull; +import seedu.address.model.person.Person; public class SortByAddress implements Comparator { private SortDirection direction; + /** + * @param direction to sort by. Either asc or dsc. + */ public SortByAddress(SortDirection direction) { requireNonNull(direction); this.direction = direction; diff --git a/src/main/java/seedu/address/model/person/comparators/SortByClientID.java b/src/main/java/seedu/address/model/person/comparators/SortByClientID.java index 6a005943ae6..395ea07b342 100644 --- a/src/main/java/seedu/address/model/person/comparators/SortByClientID.java +++ b/src/main/java/seedu/address/model/person/comparators/SortByClientID.java @@ -1,14 +1,17 @@ package seedu.address.model.person.comparators; -import seedu.address.model.person.Person; +import static java.util.Objects.requireNonNull; import java.util.Comparator; -import static java.util.Objects.requireNonNull; +import seedu.address.model.person.Person; public class SortByClientID implements Comparator { private SortDirection direction; + /** + * @param direction to sort by. Either asc or dsc. + */ public SortByClientID(SortDirection direction) { requireNonNull(direction); this.direction = direction; diff --git a/src/main/java/seedu/address/model/person/comparators/SortByCurrentPlan.java b/src/main/java/seedu/address/model/person/comparators/SortByCurrentPlan.java index e0c980da899..768cb1ce72e 100644 --- a/src/main/java/seedu/address/model/person/comparators/SortByCurrentPlan.java +++ b/src/main/java/seedu/address/model/person/comparators/SortByCurrentPlan.java @@ -1,14 +1,17 @@ package seedu.address.model.person.comparators; -import seedu.address.model.person.Person; +import static java.util.Objects.requireNonNull; import java.util.Comparator; -import static java.util.Objects.requireNonNull; +import seedu.address.model.person.Person; public class SortByCurrentPlan implements Comparator { private SortDirection direction; + /** + * @param direction to sort by. Either asc or dsc. + */ public SortByCurrentPlan(SortDirection direction) { requireNonNull(direction); this.direction = direction; diff --git a/src/main/java/seedu/address/model/person/comparators/SortByDisposableIncome.java b/src/main/java/seedu/address/model/person/comparators/SortByDisposableIncome.java index ed192593aed..7c7be3539fd 100644 --- a/src/main/java/seedu/address/model/person/comparators/SortByDisposableIncome.java +++ b/src/main/java/seedu/address/model/person/comparators/SortByDisposableIncome.java @@ -1,15 +1,18 @@ package seedu.address.model.person.comparators; -import seedu.address.model.person.Person; +import static java.util.Objects.requireNonNull; import java.util.Comparator; -import static java.util.Objects.requireNonNull; +import seedu.address.model.person.Person; -public class SortByDisposableIncome implements Comparator { +public class SortByDisposableIncome implements Comparator { private SortDirection direction; + /** + * @param direction to sort by. Either asc or dsc. + */ public SortByDisposableIncome(SortDirection direction) { requireNonNull(direction); this.direction = direction; diff --git a/src/main/java/seedu/address/model/person/comparators/SortByEmail.java b/src/main/java/seedu/address/model/person/comparators/SortByEmail.java index 64d7c1a8fcf..eac21595a75 100644 --- a/src/main/java/seedu/address/model/person/comparators/SortByEmail.java +++ b/src/main/java/seedu/address/model/person/comparators/SortByEmail.java @@ -1,15 +1,18 @@ package seedu.address.model.person.comparators; -import seedu.address.model.person.Person; +import static java.util.Objects.requireNonNull; import java.util.Comparator; -import static java.util.Objects.requireNonNull; +import seedu.address.model.person.Person; public class SortByEmail implements Comparator { private SortDirection direction; + /** + * @param direction to sort by. Either asc or dsc. + */ public SortByEmail(SortDirection direction) { requireNonNull(direction); this.direction = direction; diff --git a/src/main/java/seedu/address/model/person/comparators/SortByLastMet.java b/src/main/java/seedu/address/model/person/comparators/SortByLastMet.java index 8492059487f..ef577f99e01 100644 --- a/src/main/java/seedu/address/model/person/comparators/SortByLastMet.java +++ b/src/main/java/seedu/address/model/person/comparators/SortByLastMet.java @@ -1,14 +1,17 @@ package seedu.address.model.person.comparators; -import seedu.address.model.person.Person; +import static java.util.Objects.requireNonNull; import java.util.Comparator; -import static java.util.Objects.requireNonNull; +import seedu.address.model.person.Person; public class SortByLastMet implements Comparator { private SortDirection direction; + /** + * @param direction to sort by. Either asc or dsc. + */ public SortByLastMet(SortDirection direction) { requireNonNull(direction); this.direction = direction; diff --git a/src/main/java/seedu/address/model/person/comparators/SortByName.java b/src/main/java/seedu/address/model/person/comparators/SortByName.java index da10604f8f9..844539259db 100644 --- a/src/main/java/seedu/address/model/person/comparators/SortByName.java +++ b/src/main/java/seedu/address/model/person/comparators/SortByName.java @@ -1,15 +1,18 @@ package seedu.address.model.person.comparators; -import seedu.address.model.person.Person; +import static java.util.Objects.requireNonNull; import java.util.Comparator; -import static java.util.Objects.requireNonNull; +import seedu.address.model.person.Person; public class SortByName implements Comparator { private SortDirection direction; + /** + * @param direction to sort by. Either asc or dsc. + */ public SortByName(SortDirection direction) { requireNonNull(direction); this.direction = direction; diff --git a/src/main/java/seedu/address/model/person/comparators/SortByPhone.java b/src/main/java/seedu/address/model/person/comparators/SortByPhone.java index 31c5d209586..95099d262f5 100644 --- a/src/main/java/seedu/address/model/person/comparators/SortByPhone.java +++ b/src/main/java/seedu/address/model/person/comparators/SortByPhone.java @@ -1,14 +1,17 @@ package seedu.address.model.person.comparators; -import seedu.address.model.person.Person; +import static java.util.Objects.requireNonNull; import java.util.Comparator; -import static java.util.Objects.requireNonNull; +import seedu.address.model.person.Person; public class SortByPhone implements Comparator { private SortDirection direction; + /** + * @param direction to sort by. Either asc or dsc. + */ public SortByPhone(SortDirection direction) { requireNonNull(direction); this.direction = direction; diff --git a/src/main/java/seedu/address/model/person/comparators/SortByRiskAppetite.java b/src/main/java/seedu/address/model/person/comparators/SortByRiskAppetite.java index 5d6553a8f35..3a6f13e548a 100644 --- a/src/main/java/seedu/address/model/person/comparators/SortByRiskAppetite.java +++ b/src/main/java/seedu/address/model/person/comparators/SortByRiskAppetite.java @@ -1,15 +1,18 @@ package seedu.address.model.person.comparators; -import seedu.address.model.person.Person; +import static java.util.Objects.requireNonNull; import java.util.Comparator; -import static java.util.Objects.requireNonNull; +import seedu.address.model.person.Person; public class SortByRiskAppetite implements Comparator { private SortDirection direction; + /** + * @param direction to sort by. Either asc or dsc. + */ public SortByRiskAppetite(SortDirection direction) { requireNonNull(direction); this.direction = direction; diff --git a/src/main/java/seedu/address/model/person/comparators/SortDirection.java b/src/main/java/seedu/address/model/person/comparators/SortDirection.java index 367827e31a3..a1ff86727d1 100644 --- a/src/main/java/seedu/address/model/person/comparators/SortDirection.java +++ b/src/main/java/seedu/address/model/person/comparators/SortDirection.java @@ -4,11 +4,13 @@ import static seedu.address.commons.util.AppUtil.checkArgument; public class SortDirection { + public static final String MESSAGE_CONSTRAINTS = "Sort direction should either be asc or dsc"; private String direction; - public static final String MESSAGE_CONSTRAINTS = "Sort direction should either be asc or dsc"; - + /** + * @param direction to sort by. Either asc or dsc. + */ public SortDirection(String direction) { requireNonNull(direction); checkArgument(isValidDirection(direction), MESSAGE_CONSTRAINTS); diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 2c53a6ce765..b2c5161ebca 100644 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -9,6 +9,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; import java.util.function.Predicate; import org.junit.jupiter.api.Test; @@ -160,6 +161,11 @@ public void filterFilteredPersonList(Predicate predicate) { throw new AssertionError("This method should not be called."); } + @Override + public void sortFilteredPersonList(Comparator predicate) { + throw new AssertionError("This method should not be called."); + } + @Override public ObservableList getPersonToView() { throw new AssertionError("This method should not be called."); From 29f9327515991a8c42adcef6b66a24d97a51233a Mon Sep 17 00:00:00 2001 From: KT27Learn Date: Wed, 13 Oct 2021 23:11:55 +0800 Subject: [PATCH 4/5] Update sort command test --- .../address/logic/commands/SortCommand.java | 3 +- .../address/logic/parser/ParserUtil.java | 3 + .../person/comparators/SortDirection.java | 8 + .../typicalPersonsAddressBook.json | 44 ++--- .../logic/commands/SortCommandTest.java | 160 ++++++++++++++++++ .../address/logic/parser/ParserUtilTest.java | 124 ++++++++++++++ .../model/person/SortDirectionTest.java | 40 +++++ .../address/testutil/TypicalPersons.java | 26 +-- 8 files changed, 372 insertions(+), 36 deletions(-) create mode 100644 src/test/java/seedu/address/logic/commands/SortCommandTest.java create mode 100644 src/test/java/seedu/address/model/person/SortDirectionTest.java diff --git a/src/main/java/seedu/address/logic/commands/SortCommand.java b/src/main/java/seedu/address/logic/commands/SortCommand.java index 111d1748c05..26a88e6c345 100644 --- a/src/main/java/seedu/address/logic/commands/SortCommand.java +++ b/src/main/java/seedu/address/logic/commands/SortCommand.java @@ -45,6 +45,7 @@ public CommandResult execute(Model model) { public boolean equals(Object other) { return other == this // short circuit if same object || (other instanceof SortCommand // instanceof handles nulls - && sorter.equals(((SortCommand) other).sorter)); // state check + && sorter.equals(((SortCommand) other).sorter) + && field.equals(((SortCommand) other).field)); // state check } } diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 48bf1a89049..f78f77ba22a 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -154,6 +154,9 @@ public static CurrentPlan parseCurrentPlan(String currentPlan) throws ParseExcep public static LastMet parseLastMet(String lastMet) throws ParseException { requireNonNull(lastMet); String trimmedLastMet = lastMet.trim(); + if (!LastMet.isValidLastMet(trimmedLastMet)) { + throw new ParseException(LastMet.MESSAGE_CONSTRAINTS); + } return new LastMet(trimmedLastMet); } diff --git a/src/main/java/seedu/address/model/person/comparators/SortDirection.java b/src/main/java/seedu/address/model/person/comparators/SortDirection.java index a1ff86727d1..c2c4f7ba669 100644 --- a/src/main/java/seedu/address/model/person/comparators/SortDirection.java +++ b/src/main/java/seedu/address/model/person/comparators/SortDirection.java @@ -28,4 +28,12 @@ public boolean isAscending() { return direction.toLowerCase().equals("asc"); } + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof SortDirection // instanceof handles nulls + && direction.equals(((SortDirection) other).direction)); // state check + } + + } diff --git a/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json index f4bf4a0cd7e..dfd4860768e 100644 --- a/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json +++ b/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json @@ -6,8 +6,8 @@ "phone" : "94351253", "email" : "alice@example.com", "address" : "123, Jurong West Ave 6, #08-111", - "riskAppetite": "3", - "disposableIncome": "300", + "riskAppetite": "2", + "disposableIncome": "200", "last-met" : "07-10-2021", "current-plan" : "Prudential PRUShield", "tagged" : [ "friends" ] @@ -17,10 +17,10 @@ "phone" : "98765432", "email" : "johnd@example.com", "address" : "311, Clementi Ave 2, #02-25", - "riskAppetite": "3", + "riskAppetite": "4", "disposableIncome": "300", - "last-met" : "07-10-2021", - "current-plan" : "Prudential PRUShield", + "last-met" : "08-10-2021", + "current-plan" : "Prudential PROLife", "tagged" : [ "owesMoney", "friends" ] }, { "clientId": "2", @@ -29,9 +29,9 @@ "email" : "heinz@example.com", "address" : "wall street", "riskAppetite": "3", - "disposableIncome": "300", - "last-met" : "07-10-2021", - "current-plan" : "Prudential PRUShield", + "disposableIncome": "100", + "last-met" : "06-10-2021", + "current-plan" : "Aviva Shield", "tagged" : [ ] }, { "clientId": "3", @@ -39,10 +39,10 @@ "phone" : "87652533", "email" : "cornelia@example.com", "address" : "10th street", - "riskAppetite": "3", - "disposableIncome": "300", - "last-met" : "07-10-2021", - "current-plan" : "Prudential PRUShield", + "riskAppetite": "1", + "disposableIncome": "50000", + "last-met" : "02-10-2021", + "current-plan" : "Aviva Ace", "tagged" : [ "friends" ] }, { "clientId": "4", @@ -50,10 +50,10 @@ "phone" : "9482224", "email" : "werner@example.com", "address" : "michegan ave", - "riskAppetite": "3", - "disposableIncome": "300", - "last-met" : "07-10-2021", - "current-plan" : "Prudential PRUShield", + "riskAppetite": "1", + "disposableIncome": "4000", + "last-met" : "03-10-2021", + "current-plan" : "AIA Gold", "tagged" : [ ] }, { "clientId": "5", @@ -62,8 +62,8 @@ "email" : "lydia@example.com", "address" : "little tokyo", "riskAppetite": "3", - "disposableIncome": "300", - "last-met" : "07-10-2021", + "disposableIncome": "200", + "last-met" : "04-10-2021", "current-plan" : "Prudential PRUShield", "tagged" : [ ] }, { @@ -72,10 +72,10 @@ "phone" : "9482442", "email" : "anna@example.com", "address" : "4th street", - "riskAppetite": "3", - "disposableIncome": "300", - "last-met" : "07-10-2021", - "current-plan" : "Prudential PRUShield", + "riskAppetite": "5", + "disposableIncome": "1", + "last-met" : "03-10-2021", + "current-plan" : "AIA Zero", "tagged" : [ ] } ] } diff --git a/src/test/java/seedu/address/logic/commands/SortCommandTest.java b/src/test/java/seedu/address/logic/commands/SortCommandTest.java new file mode 100644 index 00000000000..c159f9afc60 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/SortCommandTest.java @@ -0,0 +1,160 @@ +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.assertTrue; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.testutil.TypicalPersons.ALICE; +import static seedu.address.testutil.TypicalPersons.BENSON; +import static seedu.address.testutil.TypicalPersons.CARL; +import static seedu.address.testutil.TypicalPersons.DANIEL; +import static seedu.address.testutil.TypicalPersons.ELLE; +import static seedu.address.testutil.TypicalPersons.FIONA; +import static seedu.address.testutil.TypicalPersons.GEORGE; +import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; + +import java.util.Arrays; +import java.util.Comparator; + +import org.junit.jupiter.api.Test; + +import seedu.address.commons.core.Messages; +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.model.person.Person; +import seedu.address.model.person.comparators.SortByAddress; +import seedu.address.model.person.comparators.SortByClientID; +import seedu.address.model.person.comparators.SortByCurrentPlan; +import seedu.address.model.person.comparators.SortByDisposableIncome; +import seedu.address.model.person.comparators.SortByEmail; +import seedu.address.model.person.comparators.SortByLastMet; +import seedu.address.model.person.comparators.SortByName; +import seedu.address.model.person.comparators.SortByRiskAppetite; +import seedu.address.model.person.comparators.SortDirection; + +public class SortCommandTest { + private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + + @Test + public void equals() { + SortDirection ascending = new SortDirection("asc"); + SortDirection descending = new SortDirection("dsc"); + SortByAddress sortByAddressAscending = new SortByAddress(ascending); + SortByAddress sortByAddressDesceding = new SortByAddress(descending); + SortByClientID sortByClientIdAscending = new SortByClientID(ascending); + SortByClientID sortByClientIdDescending = new SortByClientID(descending); + + SortCommand sortCommandAddressAsc = new SortCommand(sortByAddressAscending, "Address"); + SortCommand sortCommandAddressDsc = new SortCommand(sortByAddressDesceding, "Address"); + SortCommand sortCommandClientIdAsc = new SortCommand(sortByClientIdAscending, "Client ID"); + SortCommand sortCommandClientIdDsc = new SortCommand(sortByClientIdDescending, "Client Id"); + + // same object -> returns true + assertTrue(sortCommandAddressAsc.equals(sortCommandAddressAsc)); + + // same values -> returns true + SortCommand sortAddressSortCommandAsc = new SortCommand(sortByAddressAscending, "Address"); + assertTrue(sortCommandAddressAsc.equals(sortAddressSortCommandAsc)); + + // different sort direction -> returns false + assertFalse(sortCommandAddressAsc.equals(sortCommandAddressDsc)); + + // different fields -> returns false + assertFalse(sortCommandClientIdAsc.equals(sortCommandClientIdDsc)); + + // null -> returns false + assertFalse(sortCommandAddressAsc.equals(null)); + + } + + @Test + public void execute_oneKeyword_listSortedAccordingToClientId() { + String expectedMessage = String.format(Messages.MESSAGE_SORT_SUCCESS, "Client ID"); + SortDirection direction = new SortDirection("dsc"); + Comparator sorter = new SortByClientID(direction); + SortCommand command = new SortCommand(sorter, "Client ID"); + expectedModel.sortFilteredPersonList(sorter); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(GEORGE, FIONA, ELLE, DANIEL, CARL, BENSON, ALICE), model.getFilteredPersonList()); + } + + @Test + public void execute_oneKeyword_listSortedAccordingToName() { + String expectedMessage = String.format(Messages.MESSAGE_SORT_SUCCESS, "Risk Appetite"); + SortDirection direction = new SortDirection("asc"); + Comparator sorter = new SortByName(direction); + SortCommand command = new SortCommand(sorter, "Risk Appetite"); + expectedModel.sortFilteredPersonList(sorter); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(ALICE, BENSON, CARL, DANIEL, ELLE, FIONA, GEORGE), model.getFilteredPersonList()); + } + + @Test + public void execute_oneKeyword_listSortedAccordingToEmail() { + String expectedMessage = String.format(Messages.MESSAGE_SORT_SUCCESS, "Risk Appetite"); + SortDirection direction = new SortDirection("asc"); + Comparator sorter = new SortByEmail(direction); + SortCommand command = new SortCommand(sorter, "Risk Appetite"); + expectedModel.sortFilteredPersonList(sorter); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(ALICE, GEORGE, DANIEL, CARL, BENSON, FIONA, ELLE), model.getFilteredPersonList()); + } + + @Test + public void execute_oneKeyword_listSortedAccordingToAddress() { + String expectedMessage = String.format(Messages.MESSAGE_SORT_SUCCESS, "Risk Appetite"); + SortDirection direction = new SortDirection("dsc"); + Comparator sorter = new SortByAddress(direction); + SortCommand command = new SortCommand(sorter, "Risk Appetite"); + expectedModel.sortFilteredPersonList(sorter); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(CARL, ELLE, FIONA, GEORGE, BENSON, ALICE, DANIEL), model.getFilteredPersonList()); + } + + @Test + public void execute_oneKeyword_listSortedAccordingToRiskAppetite() { + String expectedMessage = String.format(Messages.MESSAGE_SORT_SUCCESS, "Risk Appetite"); + SortDirection direction = new SortDirection("asc"); + Comparator sorter = new SortByRiskAppetite(direction); + SortCommand command = new SortCommand(sorter, "Risk Appetite"); + expectedModel.sortFilteredPersonList(sorter); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(DANIEL, ELLE, ALICE, CARL, FIONA, BENSON, GEORGE), model.getFilteredPersonList()); + } + + @Test + public void execute_oneKeyword_listSortedAccordingToDisposableIncome() { + String expectedMessage = String.format(Messages.MESSAGE_SORT_SUCCESS, "Risk Appetite"); + SortDirection direction = new SortDirection("asc"); + Comparator sorter = new SortByDisposableIncome(direction); + SortCommand command = new SortCommand(sorter, "Risk Appetite"); + expectedModel.sortFilteredPersonList(sorter); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(GEORGE, CARL, ALICE, FIONA, BENSON, ELLE, DANIEL), model.getFilteredPersonList()); + } + + @Test + public void execute_oneKeyword_listSortedAccordingToCurrentPlan() { + String expectedMessage = String.format(Messages.MESSAGE_SORT_SUCCESS, "Risk Appetite"); + SortDirection direction = new SortDirection("asc"); + Comparator sorter = new SortByCurrentPlan(direction); + SortCommand command = new SortCommand(sorter, "Risk Appetite"); + expectedModel.sortFilteredPersonList(sorter); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(ELLE, GEORGE, DANIEL, CARL, BENSON, ALICE, FIONA), model.getFilteredPersonList()); + } + + @Test + public void execute_oneKeyword_listSortedAccordingToLastMet() { + String expectedMessage = String.format(Messages.MESSAGE_SORT_SUCCESS, "Risk Appetite"); + SortDirection direction = new SortDirection("asc"); + Comparator sorter = new SortByLastMet(direction); + SortCommand command = new SortCommand(sorter, "Risk Appetite"); + expectedModel.sortFilteredPersonList(sorter); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(DANIEL, ELLE, GEORGE, FIONA, CARL, ALICE, BENSON), model.getFilteredPersonList()); + } + +} diff --git a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java index 69655f1423d..0d885b4f621 100644 --- a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java +++ b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java @@ -17,9 +17,14 @@ import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.person.Address; +import seedu.address.model.person.CurrentPlan; +import seedu.address.model.person.DisposableIncome; import seedu.address.model.person.Email; +import seedu.address.model.person.LastMet; import seedu.address.model.person.Name; import seedu.address.model.person.Phone; +import seedu.address.model.person.RiskAppetite; +import seedu.address.model.person.comparators.SortDirection; import seedu.address.model.tag.Tag; public class ParserUtilTest { @@ -27,6 +32,10 @@ public class ParserUtilTest { private static final String INVALID_EMAIL = "example.com"; private static final String INVALID_PHONE_FROM_PARSER = "+651234"; private static final String INVALID_TAG = "#friend"; + private static final String INVALID_DIRECTION = "desc"; + private static final String INVALID_RISKAPPETITE = "10"; + private static final String INVALID_DISPOSABLEINCOME = "-2313213"; + private static final String INVALID_LASTMET = "20-30-2021"; private static final String VALID_NAME = "Rachel Walker"; private static final String VALID_PHONE_FROM_PARSER = "123456"; @@ -36,6 +45,11 @@ public class ParserUtilTest { private static final String VALID_EMAIL = "rachel@example.com"; private static final String VALID_TAG_1 = "friend"; private static final String VALID_TAG_2 = "neighbour"; + private static final String VALID_DIRECTION = "asc"; + private static final String VALID_RISKAPPETITE = "2"; + private static final String VALID_DISPOSABLEINCOME = "5000"; + private static final String VALID_CURRENTPLAN = "Prudential Prolife"; + private static final String VALID_LASTMET = "05-10-2021"; private static final String WHITESPACE = " \t\r\n"; @@ -166,6 +180,116 @@ public void parseEmail_validValueWithWhitespace_returnsTrimmedEmail() throws Exc assertEquals(expectedEmail, ParserUtil.parseEmail(emailWithWhitespace)); } + @Test + public void parseDirection_validValueWithoutWhitespace_returnsDirection() throws Exception { + SortDirection expectedSortDirection = new SortDirection(VALID_DIRECTION); + assertEquals(expectedSortDirection, ParserUtil.parseSortDirection(VALID_DIRECTION)); + } + + @Test + public void parseDirection_validValueWithWhitespace_returnsTrimmedDirection() throws Exception { + String directionWithWhitespace = WHITESPACE + VALID_DIRECTION + WHITESPACE; + SortDirection expectedSortDirection = new SortDirection(VALID_DIRECTION); + assertEquals(expectedSortDirection, ParserUtil.parseSortDirection(directionWithWhitespace)); + } + + @Test + public void parseDirection_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> ParserUtil.parseSortDirection((String) null)); + } + + @Test + public void parseDirection_invalidValue_throwsParseException() { + assertThrows(ParseException.class, () -> ParserUtil.parseEmail(INVALID_DIRECTION)); + } + + @Test + public void parseRiskAppetite_validValueWithoutWhitespace_returnsRiskAppetite() throws Exception { + RiskAppetite expectedRiskAppetite = new RiskAppetite(VALID_RISKAPPETITE); + assertEquals(expectedRiskAppetite, ParserUtil.parseRiskAppetite(VALID_RISKAPPETITE)); + } + + @Test + public void parseRiskAppetite_validValueWithWhitespace_returnsTrimmedRiskAppetite() throws Exception { + String riskAppetiteWithWhitespace = WHITESPACE + VALID_RISKAPPETITE + WHITESPACE; + RiskAppetite expectedRiskAppetite = new RiskAppetite(VALID_RISKAPPETITE); + assertEquals(expectedRiskAppetite, ParserUtil.parseRiskAppetite(riskAppetiteWithWhitespace)); + } + + @Test + public void parseRiskAppetite_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> ParserUtil.parseRiskAppetite((String) null)); + } + + @Test + public void parseRiskAppetite_invalidValue_throwsParseException() { + assertThrows(ParseException.class, () -> ParserUtil.parseRiskAppetite(INVALID_RISKAPPETITE)); + } + + @Test + public void parseDisposableIncome_invalidValue_throwsParseException() { + assertThrows(ParseException.class, () -> ParserUtil.parseDisposableIncome(INVALID_DISPOSABLEINCOME)); + } + + @Test + public void parseDisposableIncome_validValueWithoutWhitespace_returnsDisposableIncome() throws Exception { + DisposableIncome expectedDisposableIncome = new DisposableIncome(VALID_DISPOSABLEINCOME); + assertEquals(expectedDisposableIncome, ParserUtil.parseDisposableIncome(VALID_DISPOSABLEINCOME)); + } + + @Test + public void parseDisposableIncome_validValueWithWhitespace_returnsTrimmedDisposableIncome() throws Exception { + String disposableIncomeWithWhitespace = WHITESPACE + VALID_DISPOSABLEINCOME + WHITESPACE; + DisposableIncome expectedDisposableIncome = new DisposableIncome(VALID_DISPOSABLEINCOME); + assertEquals(expectedDisposableIncome, ParserUtil.parseDisposableIncome(disposableIncomeWithWhitespace)); + } + + @Test + public void parseCurrentPlan_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> ParserUtil.parseCurrentPlan(null)); + } + + @Test + public void parseCurrentPlan_validValueWithoutWhitespace_returnsCurrentPlan() throws Exception { + CurrentPlan expectedCurrentPlan = new CurrentPlan(VALID_CURRENTPLAN); + assertEquals(expectedCurrentPlan, ParserUtil.parseCurrentPlan(VALID_CURRENTPLAN)); + } + + @Test + public void parseCurrentPlan_validValueWithWhitespace_returnsTrimmedCurrentPlan() throws Exception { + String currentPlanWithWhitespace = WHITESPACE + VALID_CURRENTPLAN + WHITESPACE; + CurrentPlan expectedCurrentPlan = new CurrentPlan(VALID_CURRENTPLAN); + assertEquals(expectedCurrentPlan, ParserUtil.parseCurrentPlan(currentPlanWithWhitespace)); + } + + @Test + public void parseLastMet_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> ParserUtil.parseLastMet((String) null)); + } + + @Test + public void parseLastMet_invalidValue_throwsParseException() { + assertThrows(ParseException.class, () -> ParserUtil.parseLastMet(INVALID_LASTMET)); + } + + @Test + public void parseLastMet_validValueWithoutWhitespace_returnsLastMet() throws Exception { + LastMet expectedLastMet = new LastMet(VALID_LASTMET); + assertEquals(expectedLastMet, ParserUtil.parseLastMet(VALID_LASTMET)); + } + + @Test + public void parseLastMet_validValueWithWhitespace_returnsTrimmedLastMet() throws Exception { + String lastMetWithWhitespace = WHITESPACE + VALID_LASTMET + WHITESPACE; + LastMet expectedLastMet = new LastMet(VALID_LASTMET); + assertEquals(expectedLastMet, ParserUtil.parseLastMet(lastMetWithWhitespace)); + } + + @Test + public void parseDisposableIncome_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> ParserUtil.parseDisposableIncome((String) null)); + } + @Test public void parseTag_null_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> ParserUtil.parseTag(null)); diff --git a/src/test/java/seedu/address/model/person/SortDirectionTest.java b/src/test/java/seedu/address/model/person/SortDirectionTest.java new file mode 100644 index 00000000000..1f81449d055 --- /dev/null +++ b/src/test/java/seedu/address/model/person/SortDirectionTest.java @@ -0,0 +1,40 @@ +package seedu.address.model.person; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; + +import org.junit.jupiter.api.Test; + +import seedu.address.model.person.comparators.SortDirection; + +public class SortDirectionTest { + + @Test + public void constructor_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> new SortDirection(null)); + } + + @Test + public void constructor_invalidSortDirection_throwsIllegalArgumentException() { + String invalidSortDirection = "asec"; + assertThrows(IllegalArgumentException.class, () -> new SortDirection(invalidSortDirection)); + } + + @Test + public void isValidRiskAppetite() { + // null phone number + assertThrows(NullPointerException.class, () -> SortDirection.isValidDirection(null)); + + // invalid directions + assertFalse(SortDirection.isValidDirection(" ")); // spaces only + assertFalse(SortDirection.isValidDirection("ascending")); // integer not within 1 and 5 + assertFalse(SortDirection.isValidDirection("213421312312")); // numeric + assertFalse(SortDirection.isValidDirection("as2c")); // digits within alphabets + assertFalse(SortDirection.isValidDirection("as c")); // spaces within alphabets + + // valid directions + assertTrue(SortDirection.isValidDirection("asc")); // Ascending + assertTrue(SortDirection.isValidDirection("dsc")); // Descending + } +} diff --git a/src/test/java/seedu/address/testutil/TypicalPersons.java b/src/test/java/seedu/address/testutil/TypicalPersons.java index dc181f25ebe..0ca0e52c39f 100644 --- a/src/test/java/seedu/address/testutil/TypicalPersons.java +++ b/src/test/java/seedu/address/testutil/TypicalPersons.java @@ -33,36 +33,36 @@ public class TypicalPersons { public static final Person ALICE = new PersonBuilder().withClientId("0").withName("Alice Pauline") .withAddress("123, Jurong West Ave 6, #08-111").withEmail("alice@example.com") - .withPhone("94351253").withRiskAppetite("3").withDisposableIncome("300") + .withPhone("94351253").withRiskAppetite("2").withDisposableIncome("200") .withCurrentPlan("Prudential PRUShield").withLastMet("07-10-2021").withTags("friends").build(); public static final Person BENSON = new PersonBuilder().withClientId("1").withName("Benson Meier") .withAddress("311, Clementi Ave 2, #02-25").withEmail("johnd@example.com").withPhone("98765432") - .withRiskAppetite("3").withDisposableIncome("300").withCurrentPlan("Prudential PRUShield") - .withLastMet("07-10-2021").withTags("owesMoney", "friends").build(); + .withRiskAppetite("4").withDisposableIncome("300").withCurrentPlan("Prudential PROLife") + .withLastMet("08-10-2021").withTags("owesMoney", "friends").build(); public static final Person CARL = new PersonBuilder().withClientId("2").withName("Carl Kurz").withPhone("95352563") .withEmail("heinz@example.com").withAddress("wall street").withRiskAppetite("3") - .withDisposableIncome("300").withCurrentPlan("Prudential PRUShield").withLastMet("07-10-2021").build(); + .withDisposableIncome("100").withCurrentPlan("Aviva Shield").withLastMet("06-10-2021").build(); public static final Person DANIEL = new PersonBuilder().withClientId("3").withName("Daniel Meier") - .withPhone("87652533").withEmail("cornelia@example.com").withAddress("10th street").withRiskAppetite("3") - .withDisposableIncome("300").withCurrentPlan("Prudential PRUShield").withLastMet("07-10-2021") + .withPhone("87652533").withEmail("cornelia@example.com").withAddress("10th street").withRiskAppetite("1") + .withDisposableIncome("50000").withCurrentPlan("Aviva Ace").withLastMet("02-10-2021") .withTags("friends").build(); public static final Person ELLE = new PersonBuilder().withClientId("4").withName("Elle Meyer").withPhone("9482224") - .withEmail("werner@example.com").withAddress("michegan ave").withRiskAppetite("3") - .withDisposableIncome("300").withCurrentPlan("Prudential PRUShield") - .withLastMet("07-10-2021").build(); + .withEmail("werner@example.com").withAddress("michegan ave").withRiskAppetite("1") + .withDisposableIncome("4000").withCurrentPlan("AIA Gold") + .withLastMet("03-10-2021").build(); public static final Person FIONA = new PersonBuilder().withClientId("5").withName("Fiona Kunz").withPhone("9482427") .withEmail("lydia@example.com").withAddress("little tokyo").withRiskAppetite("3") - .withDisposableIncome("300").withCurrentPlan("Prudential PRUShield").withLastMet("07-10-2021").build(); + .withDisposableIncome("200").withCurrentPlan("Prudential PRUShield").withLastMet("04-10-2021").build(); public static final Person GEORGE = new PersonBuilder().withClientId("6").withName("George Best") - .withPhone("9482442").withEmail("anna@example.com").withAddress("4th street").withRiskAppetite("3") - .withDisposableIncome("300").withCurrentPlan("Prudential PRUShield").withLastMet("07-10-2021").build(); + .withPhone("9482442").withEmail("anna@example.com").withAddress("4th street").withRiskAppetite("5") + .withDisposableIncome("1").withCurrentPlan("AIA Zero").withLastMet("03-10-2021").build(); // Manually added public static final Person HOON = new PersonBuilder().withClientId("7").withName("Hoon Meier").withPhone("8482424") .withEmail("stefan@example.com").withAddress("little india").withRiskAppetite("3") .withDisposableIncome("300").withCurrentPlan("Prudential PRUShield").withLastMet("10-06-2021").build(); public static final Person IDA = new PersonBuilder().withClientId("8").withName("Ida Mueller").withPhone("8482131") - .withEmail("hans@example.com").withAddress("chicago ave").withRiskAppetite("3") + .withEmail("hans@example.com").withAddress("chicago ave").withRiskAppetite("4") .withDisposableIncome("300").withCurrentPlan("Prudential PRUShield").withLastMet("10-06-2021").build(); // Manually added - Person's details found in {@code CommandTestUtil} From fee46d6214948ba274856153cc624f69a7804cbc Mon Sep 17 00:00:00 2001 From: KT27Learn Date: Wed, 13 Oct 2021 23:32:32 +0800 Subject: [PATCH 5/5] Fix EditCommandTest Bug --- src/main/java/seedu/address/logic/commands/EditCommand.java | 2 ++ src/main/java/seedu/address/model/Model.java | 2 +- src/main/java/seedu/address/model/ModelManager.java | 2 +- src/test/java/seedu/address/logic/commands/AddCommandTest.java | 1 + 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index 8764a8de61b..e0ff85d6e65 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -165,6 +165,8 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) { setPhone(toCopy.phone); setEmail(toCopy.email); setAddress(toCopy.address); + setDisposableIncome(toCopy.disposableIncome); + setRiskAppetite(toCopy.riskAppetite); setLastMet(toCopy.lastMet); setCurrentPlan(toCopy.currentPlan); setTags(toCopy.tags); diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 67bcdee3751..848de2c462f 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -1,8 +1,8 @@ package seedu.address.model; import java.nio.file.Path; -import java.util.ArrayList; import java.util.Comparator; +import java.util.List; import java.util.function.Predicate; import javafx.collections.ObservableList; diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index e04c2283802..b7c993926c3 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -4,8 +4,8 @@ import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; import java.nio.file.Path; -import java.util.ArrayList; import java.util.Comparator; +import java.util.List; import java.util.function.Predicate; import java.util.logging.Logger; diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 16026eb475f..31f4100ab47 100644 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; +import java.util.List; import java.util.function.Predicate; import org.junit.jupiter.api.Test;