From 1f69b743faa7a41be4f2e9d4ad577e266c5c57df Mon Sep 17 00:00:00 2001 From: GERARDJM018 Date: Sun, 17 Mar 2024 20:52:36 +0800 Subject: [PATCH 1/2] Update ListCommand.java Update the implementation of List Command. New feature to view list only the given type of person/contact. --- .../address/logic/commands/ListCommand.java | 14 +++++- .../logic/parser/AddressBookParser.java | 2 +- .../logic/parser/ListCommandParser.java | 39 +++++++++++++++ .../address/model/person/TypePredicate.java | 50 +++++++++++++++++++ 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/main/java/seedu/address/logic/parser/ListCommandParser.java create mode 100644 src/main/java/seedu/address/model/person/TypePredicate.java diff --git a/src/main/java/seedu/address/logic/commands/ListCommand.java b/src/main/java/seedu/address/logic/commands/ListCommand.java index 84be6ad2596..8ea337094c5 100644 --- a/src/main/java/seedu/address/logic/commands/ListCommand.java +++ b/src/main/java/seedu/address/logic/commands/ListCommand.java @@ -4,6 +4,8 @@ import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; import seedu.address.model.Model; +import seedu.address.model.person.NameContainsKeywordsPredicate; +import seedu.address.model.person.TypePredicate; /** * Lists all persons in the address book to the user. @@ -14,11 +16,21 @@ public class ListCommand extends Command { public static final String MESSAGE_SUCCESS = "Listed all persons"; + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Show list of the contacts with the given type.\n" + + "Parameters: TYPE\n" + + "Example: " + COMMAND_WORD + " housekeeper"; + private final TypePredicate predicate; + public ListCommand() { + this.predicate = (TypePredicate) PREDICATE_SHOW_ALL_PERSONS; + } + public ListCommand(TypePredicate predicate) { + this.predicate = predicate; + } @Override public CommandResult execute(Model model) { requireNonNull(model); - model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); + model.updateFilteredPersonList(predicate); return new CommandResult(MESSAGE_SUCCESS); } } diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index 3149ee07e0b..88cbea43998 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -69,7 +69,7 @@ public Command parseCommand(String userInput) throws ParseException { return new FindCommandParser().parse(arguments); case ListCommand.COMMAND_WORD: - return new ListCommand(); + return new ListCommandParser().parse(arguments); case ExitCommand.COMMAND_WORD: return new ExitCommand(); diff --git a/src/main/java/seedu/address/logic/parser/ListCommandParser.java b/src/main/java/seedu/address/logic/parser/ListCommandParser.java new file mode 100644 index 00000000000..88b3a4ae001 --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/ListCommandParser.java @@ -0,0 +1,39 @@ +package seedu.address.logic.parser; + +import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; + +import seedu.address.logic.commands.FindCommand; +import seedu.address.logic.commands.ListCommand; +import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.person.TypePredicate; + +/** + * Parses input arguments and creates a new FindCommand object + */ +public class ListCommandParser implements Parser { + + /** + * Parses the given {@code String} of arguments in the context of the FindCommand + * and returns a FindCommand object for execution. + * + * @throws ParseException if the user input does not conform the expected format + */ + public ListCommand parse(String args) throws ParseException { + String trimmedArgs = args.trim(); + if (trimmedArgs.isEmpty()) { + throw new ParseException( + String.format(MESSAGE_INVALID_COMMAND_FORMAT, ListCommand.MESSAGE_USAGE)); + } + + String[] type = trimmedArgs.split("\\s+"); + + if (type.length != 1) { + throw new ParseException( + String.format(MESSAGE_INVALID_COMMAND_FORMAT, ListCommand.MESSAGE_USAGE)); + } + + + return new ListCommand(new TypePredicate(trimmedArgs)); + } + +} diff --git a/src/main/java/seedu/address/model/person/TypePredicate.java b/src/main/java/seedu/address/model/person/TypePredicate.java new file mode 100644 index 00000000000..71416ccb9df --- /dev/null +++ b/src/main/java/seedu/address/model/person/TypePredicate.java @@ -0,0 +1,50 @@ +package seedu.address.model.person; + +import java.util.List; +import java.util.function.Predicate; + +import seedu.address.commons.util.StringUtil; +import seedu.address.commons.util.ToStringBuilder; +import seedu.address.logic.commands.ClearCommand; + +/** + * Tests that a {@code Person}'s {@code Name} matches any of the keywords given. + */ +public class TypePredicate implements Predicate { + private final String type; + + public TypePredicate(String type) { + this.type = type; + } + + @Override + public boolean test(Person person) { + if (type.equals("housekeeper")) { + return person instanceof Housekeeper; + } else if (type.equals("client")) { + return person instanceof Client; + } else { + return false; + } + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof NameContainsKeywordsPredicate)) { + return false; + } + + TypePredicate otherNameContainsKeywordsPredicate = (TypePredicate) other; + return type.equals(otherNameContainsKeywordsPredicate.type); + } + + @Override + public String toString() { + return new ToStringBuilder(this).add("type", type).toString(); + } +} From dd2edab114c13e2629a926d6486c76a67d74d06b Mon Sep 17 00:00:00 2001 From: GERARDJM018 Date: Mon, 18 Mar 2024 00:19:15 +0800 Subject: [PATCH 2/2] Update TypePredicate.java Update TypePredicate.java to use isClient method. This changes is made to maintain consistency. --- src/main/java/seedu/address/model/person/TypePredicate.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/model/person/TypePredicate.java b/src/main/java/seedu/address/model/person/TypePredicate.java index 71416ccb9df..f20b27c612b 100644 --- a/src/main/java/seedu/address/model/person/TypePredicate.java +++ b/src/main/java/seedu/address/model/person/TypePredicate.java @@ -20,9 +20,9 @@ public TypePredicate(String type) { @Override public boolean test(Person person) { if (type.equals("housekeeper")) { - return person instanceof Housekeeper; + return !person.isClient(); } else if (type.equals("client")) { - return person instanceof Client; + return person.isClient(); } else { return false; }