From adca7b53baeea439332f33a93bd1ebcc5f71f68a Mon Sep 17 00:00:00 2001 From: KrashKart Date: Wed, 9 Oct 2024 23:54:52 +0800 Subject: [PATCH 1/6] Add hierachy to ContainsKeywordsPredicate Let's * create an abstract ContainsKeywordsPredicate class * let Name-, Contact- and EmailContainsKeywordsPredicate inherit from the abstract class This is done to create a hierachy of Predicates used for the various find commands: find by name, contact and email --- .../ContactContainsKeywordsPredicate.java | 43 +++++++++++++++++++ .../person/ContainsKeywordsPredicate.java | 42 ++++++++++++++++++ .../EmailContainsKeywordsPredicate.java | 43 +++++++++++++++++++ .../person/NameContainsKeywordsPredicate.java | 11 +++-- 4 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java create mode 100644 src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java create mode 100644 src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java diff --git a/src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java new file mode 100644 index 00000000000..ba0e368a48d --- /dev/null +++ b/src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java @@ -0,0 +1,43 @@ +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; + +/** + * Tests that a {@code Person}'s {@code Name} matches any of the keywords given. + */ +public class ContactContainsKeywordsPredicate extends ContainsKeywordsPredicate { + + public ContactContainsKeywordsPredicate(List keywords) { + super(keywords); + } + + @Override + public boolean test(Person person) { + return this.getKeywords().stream() + .anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getPhone().value, keyword)); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof ContactContainsKeywordsPredicate)) { + return false; + } + + ContactContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (ContactContainsKeywordsPredicate) other; + return this.getKeywords().equals(otherNameContainsKeywordsPredicate.getKeywords()); + } + + @Override + public String toString() { + return new ToStringBuilder(this).add("keywords", this.getKeywords()).toString(); + } +} diff --git a/src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java new file mode 100644 index 00000000000..793b9c52503 --- /dev/null +++ b/src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java @@ -0,0 +1,42 @@ +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; + +/** + * Tests that a {@code Person}'s {@code Name} matches any of the keywords given. + */ +abstract public class ContainsKeywordsPredicate implements Predicate { + private final List keywords; + + public ContainsKeywordsPredicate(List keywords) { + this.keywords = keywords; + } + + protected List getKeywords() { + return this.keywords; + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof ContainsKeywordsPredicate)) { + return false; + } + + ContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (ContainsKeywordsPredicate) other; + return keywords.equals(otherNameContainsKeywordsPredicate.keywords); + } + + @Override + public String toString() { + return new ToStringBuilder(this).add("keywords", keywords).toString(); + } +} diff --git a/src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java new file mode 100644 index 00000000000..85fc33c12c8 --- /dev/null +++ b/src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java @@ -0,0 +1,43 @@ +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; + +/** + * Tests that a {@code Person}'s {@code Name} matches any of the keywords given. + */ +public class EmailContainsKeywordsPredicate extends ContainsKeywordsPredicate { + + public EmailContainsKeywordsPredicate(List keywords) { + super(keywords); + } + + @Override + public boolean test(Person person) { + return super.getKeywords().stream() + .anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getEmail().value, keyword)); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof EmailContainsKeywordsPredicate)) { + return false; + } + + EmailContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (EmailContainsKeywordsPredicate) other; + return this.getKeywords().equals(otherNameContainsKeywordsPredicate.getKeywords()); + } + + @Override + public String toString() { + return new ToStringBuilder(this).add("keywords", this.getKeywords()).toString(); + } +} diff --git a/src/main/java/seedu/address/model/person/NameContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/person/NameContainsKeywordsPredicate.java index 62d19be2977..5c5f97f01e1 100644 --- a/src/main/java/seedu/address/model/person/NameContainsKeywordsPredicate.java +++ b/src/main/java/seedu/address/model/person/NameContainsKeywordsPredicate.java @@ -9,16 +9,15 @@ /** * Tests that a {@code Person}'s {@code Name} matches any of the keywords given. */ -public class NameContainsKeywordsPredicate implements Predicate { - private final List keywords; +public class NameContainsKeywordsPredicate extends ContainsKeywordsPredicate { public NameContainsKeywordsPredicate(List keywords) { - this.keywords = keywords; + super(keywords); } @Override public boolean test(Person person) { - return keywords.stream() + return this.getKeywords().stream() .anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getName().fullName, keyword)); } @@ -34,11 +33,11 @@ public boolean equals(Object other) { } NameContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (NameContainsKeywordsPredicate) other; - return keywords.equals(otherNameContainsKeywordsPredicate.keywords); + return this.getKeywords().equals(otherNameContainsKeywordsPredicate.getKeywords()); } @Override public String toString() { - return new ToStringBuilder(this).add("keywords", keywords).toString(); + return new ToStringBuilder(this).add("keywords", this.getKeywords()).toString(); } } From c483bc18d519cb82fa647883a482b20388a329d0 Mon Sep 17 00:00:00 2001 From: KrashKart Date: Wed, 9 Oct 2024 23:58:19 +0800 Subject: [PATCH 2/6] Make FindCommand abstract This is so that we can use this as a template for the sub-commands for Find --- .../address/logic/commands/FindCommand.java | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/FindCommand.java b/src/main/java/seedu/address/logic/commands/FindCommand.java index 72b9eddd3a7..73989d93799 100644 --- a/src/main/java/seedu/address/logic/commands/FindCommand.java +++ b/src/main/java/seedu/address/logic/commands/FindCommand.java @@ -11,7 +11,7 @@ * Finds and lists all persons in address book whose name contains any of the argument keywords. * Keyword matching is case insensitive. */ -public class FindCommand extends Command { +abstract public class FindCommand extends Command { public static final String COMMAND_WORD = "find"; @@ -20,39 +20,39 @@ public class FindCommand extends Command { + "Parameters: KEYWORD [MORE_KEYWORDS]...\n" + "Example: " + COMMAND_WORD + " alice bob charlie"; - private final NameContainsKeywordsPredicate predicate; - - public FindCommand(NameContainsKeywordsPredicate predicate) { - this.predicate = predicate; - } - - @Override +// private final NameContainsKeywordsPredicate predicate; +// +// public FindCommand(NameContainsKeywordsPredicate predicate) { +// this.predicate = predicate; +// } +// +// @Override public CommandResult execute(Model model) { - requireNonNull(model); +// requireNonNull(model); model.updateFilteredPersonList(predicate); - return new CommandResult( - String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); - } - - @Override - public boolean equals(Object other) { - if (other == this) { - return true; - } - - // instanceof handles nulls - if (!(other instanceof FindCommand)) { - return false; - } - - FindCommand otherFindCommand = (FindCommand) other; - return predicate.equals(otherFindCommand.predicate); - } - - @Override - public String toString() { - return new ToStringBuilder(this) - .add("predicate", predicate) - .toString(); - } +// return new CommandResult( +// String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); +// } + +// @Override +// public boolean equals(Object other) { +// if (other == this) { +// return true; +// } +// +// // instanceof handles nulls +// if (!(other instanceof FindCommand)) { +// return false; +// } +// +// FindCommand otherFindCommand = (FindCommand) other; +// return predicate.equals(otherFindCommand.predicate); +// } +// +// @Override +// public String toString() { +// return new ToStringBuilder(this) +// .add("predicate", predicate) +// .toString(); +// } } From a44950e9662e4b5f233c25e44deedf93dfef179b Mon Sep 17 00:00:00 2001 From: KrashKart Date: Thu, 10 Oct 2024 00:02:33 +0800 Subject: [PATCH 3/6] Change message usage and other methods Temporarily correct until we implement the sub-categories of find --- .../address/logic/commands/FindCommand.java | 74 +++++++++---------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/FindCommand.java b/src/main/java/seedu/address/logic/commands/FindCommand.java index 73989d93799..5eb1886d12c 100644 --- a/src/main/java/seedu/address/logic/commands/FindCommand.java +++ b/src/main/java/seedu/address/logic/commands/FindCommand.java @@ -5,7 +5,7 @@ import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; import seedu.address.model.Model; -import seedu.address.model.person.NameContainsKeywordsPredicate; +import seedu.address.model.person.ContainsKeywordsPredicate; /** * Finds and lists all persons in address book whose name contains any of the argument keywords. @@ -15,44 +15,44 @@ abstract public class FindCommand extends Command { public static final String COMMAND_WORD = "find"; - public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names contain any of " - + "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n" + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names, contacts or emails " + + "contain any of the specified keywords (case-insensitive) and displays them as a list with indices.\n" + "Parameters: KEYWORD [MORE_KEYWORDS]...\n" + "Example: " + COMMAND_WORD + " alice bob charlie"; -// private final NameContainsKeywordsPredicate predicate; -// -// public FindCommand(NameContainsKeywordsPredicate predicate) { -// this.predicate = predicate; -// } -// -// @Override + private final ContainsKeywordsPredicate predicate; + + public FindCommand(ContainsKeywordsPredicate predicate) { + this.predicate = predicate; + } + + @Override public CommandResult execute(Model model) { -// requireNonNull(model); - model.updateFilteredPersonList(predicate); -// return new CommandResult( -// String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); -// } - -// @Override -// public boolean equals(Object other) { -// if (other == this) { -// return true; -// } -// -// // instanceof handles nulls -// if (!(other instanceof FindCommand)) { -// return false; -// } -// -// FindCommand otherFindCommand = (FindCommand) other; -// return predicate.equals(otherFindCommand.predicate); -// } -// -// @Override -// public String toString() { -// return new ToStringBuilder(this) -// .add("predicate", predicate) -// .toString(); -// } + requireNonNull(model); + model.updateFilteredPersonList(this.predicate); + return new CommandResult( + String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof FindCommand)) { + return false; + } + + FindCommand otherFindCommand = (FindCommand) other; + return this.predicate.equals(otherFindCommand.predicate); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .add("predicate", this.predicate) + .toString(); + } } From 5ffbc0e7b1ce0ad578ad2f9bb98132c50d5a0ed1 Mon Sep 17 00:00:00 2001 From: KrashKart Date: Thu, 10 Oct 2024 00:05:21 +0800 Subject: [PATCH 4/6] Add AbstractFindCommand Temporary so the code does not break --- .../logic/commands/AbstractFindCommand.java | 58 +++++++++++++++++++ .../address/logic/commands/FindCommand.java | 4 +- 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/main/java/seedu/address/logic/commands/AbstractFindCommand.java diff --git a/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java b/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java new file mode 100644 index 00000000000..082b2b6a25e --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java @@ -0,0 +1,58 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; + +import seedu.address.commons.util.ToStringBuilder; +import seedu.address.logic.Messages; +import seedu.address.model.Model; +import seedu.address.model.person.ContainsKeywordsPredicate; + +/** + * Finds and lists all persons in address book whose name contains any of the argument keywords. + * Keyword matching is case-insensitive. + */ +abstract public class AbstractFindCommand extends Command { + + public static final String COMMAND_WORD = "find"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names, contacts or emails " + + "contain any of the specified keywords (case-insensitive) and displays them as a list with indices.\n" + + "Parameters: KEYWORD [MORE_KEYWORDS]...\n" + + "Example: " + COMMAND_WORD + " alice bob charlie"; + + private final ContainsKeywordsPredicate predicate; + + public AbstractFindCommand(ContainsKeywordsPredicate predicate) { + this.predicate = predicate; + } + + @Override + public CommandResult execute(Model model) { + requireNonNull(model); + model.updateFilteredPersonList(this.predicate); + return new CommandResult( + String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof AbstractFindCommand)) { + return false; + } + + AbstractFindCommand otherFindCommand = (AbstractFindCommand) other; + return this.predicate.equals(otherFindCommand.predicate); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .add("predicate", this.predicate) + .toString(); + } +} diff --git a/src/main/java/seedu/address/logic/commands/FindCommand.java b/src/main/java/seedu/address/logic/commands/FindCommand.java index 5eb1886d12c..8595cd020f8 100644 --- a/src/main/java/seedu/address/logic/commands/FindCommand.java +++ b/src/main/java/seedu/address/logic/commands/FindCommand.java @@ -9,9 +9,9 @@ /** * Finds and lists all persons in address book whose name contains any of the argument keywords. - * Keyword matching is case insensitive. + * Keyword matching is case-insensitive. */ -abstract public class FindCommand extends Command { +public class FindCommand extends Command { public static final String COMMAND_WORD = "find"; From 95a09a23133c6aa15b313422bf1a47262e7f3663 Mon Sep 17 00:00:00 2001 From: KrashKart Date: Thu, 10 Oct 2024 00:13:08 +0800 Subject: [PATCH 5/6] Modify code for checkStyle --- .../java/seedu/address/logic/commands/AbstractFindCommand.java | 2 +- .../address/model/person/ContactContainsKeywordsPredicate.java | 1 - .../seedu/address/model/person/ContainsKeywordsPredicate.java | 3 +-- .../address/model/person/EmailContainsKeywordsPredicate.java | 1 - .../address/model/person/NameContainsKeywordsPredicate.java | 3 +-- 5 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java b/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java index 082b2b6a25e..9c9b7f1fb76 100644 --- a/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java +++ b/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java @@ -11,7 +11,7 @@ * Finds and lists all persons in address book whose name contains any of the argument keywords. * Keyword matching is case-insensitive. */ -abstract public class AbstractFindCommand extends Command { +public abstract class AbstractFindCommand extends Command { public static final String COMMAND_WORD = "find"; diff --git a/src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java index ba0e368a48d..cfec0c4b404 100644 --- a/src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java +++ b/src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java @@ -1,7 +1,6 @@ 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; diff --git a/src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java index 793b9c52503..6b2100e35a3 100644 --- a/src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java +++ b/src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java @@ -3,13 +3,12 @@ import java.util.List; import java.util.function.Predicate; -import seedu.address.commons.util.StringUtil; import seedu.address.commons.util.ToStringBuilder; /** * Tests that a {@code Person}'s {@code Name} matches any of the keywords given. */ -abstract public class ContainsKeywordsPredicate implements Predicate { +public abstract class ContainsKeywordsPredicate implements Predicate { private final List keywords; public ContainsKeywordsPredicate(List keywords) { diff --git a/src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java index 85fc33c12c8..96a256e3f55 100644 --- a/src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java +++ b/src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java @@ -1,7 +1,6 @@ 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; diff --git a/src/main/java/seedu/address/model/person/NameContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/person/NameContainsKeywordsPredicate.java index 5c5f97f01e1..90037ae21bd 100644 --- a/src/main/java/seedu/address/model/person/NameContainsKeywordsPredicate.java +++ b/src/main/java/seedu/address/model/person/NameContainsKeywordsPredicate.java @@ -1,7 +1,6 @@ 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; @@ -12,7 +11,7 @@ public class NameContainsKeywordsPredicate extends ContainsKeywordsPredicate { public NameContainsKeywordsPredicate(List keywords) { - super(keywords); + super(keywords); } @Override From d5b0b4cef5a07812c337c1d56f63673485ae2a8b Mon Sep 17 00:00:00 2001 From: KrashKart Date: Thu, 10 Oct 2024 00:26:44 +0800 Subject: [PATCH 6/6] Swap both classes I super sot --- .../java/seedu/address/logic/commands/FindCommand.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/FindCommand.java b/src/main/java/seedu/address/logic/commands/FindCommand.java index 8595cd020f8..fb87d2ae31b 100644 --- a/src/main/java/seedu/address/logic/commands/FindCommand.java +++ b/src/main/java/seedu/address/logic/commands/FindCommand.java @@ -5,7 +5,7 @@ import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; import seedu.address.model.Model; -import seedu.address.model.person.ContainsKeywordsPredicate; +import seedu.address.model.person.NameContainsKeywordsPredicate; /** * Finds and lists all persons in address book whose name contains any of the argument keywords. @@ -15,14 +15,14 @@ public class FindCommand extends Command { public static final String COMMAND_WORD = "find"; - public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names, contacts or emails " + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names " + "contain any of the specified keywords (case-insensitive) and displays them as a list with indices.\n" + "Parameters: KEYWORD [MORE_KEYWORDS]...\n" + "Example: " + COMMAND_WORD + " alice bob charlie"; - private final ContainsKeywordsPredicate predicate; + private final NameContainsKeywordsPredicate predicate; - public FindCommand(ContainsKeywordsPredicate predicate) { + public FindCommand(NameContainsKeywordsPredicate predicate) { this.predicate = predicate; }