From eb54268c7d3df691458d5069ad6de777a94396c6 Mon Sep 17 00:00:00 2001 From: yooplo Date: Thu, 10 Oct 2024 18:55:49 +0800 Subject: [PATCH 1/3] Implement FindingByEmailCommand Class --- .../logic/commands/FindByEmailCommand.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/seedu/address/logic/commands/FindByEmailCommand.java diff --git a/src/main/java/seedu/address/logic/commands/FindByEmailCommand.java b/src/main/java/seedu/address/logic/commands/FindByEmailCommand.java new file mode 100644 index 00000000000..c543e4b5295 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/FindByEmailCommand.java @@ -0,0 +1,36 @@ +package seedu.address.logic.commands; + +import seedu.address.model.person.EmailContainsKeywordsPredicate; + +/** + * Finds and lists all persons in address book whose email is equivalent to the specified keyword. + * Keyword matching is case-insensitive. + */ +public class FindByEmailCommand extends AbstractFindCommand { + + public static final String COMMAND_WORD = "find /e"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose email " + + "is the same as the specified keywords (case-insensitive) and displays them as a list with indices.\n" + + "Parameters: KEYWORD [MORE_KEYWORDS]...\n" + + "Example: " + COMMAND_WORD + " alexyeoh@gmail.com"; + + public FindByEmailCommand(EmailContainsKeywordsPredicate predicate) { + super(predicate); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof FindByEmailCommand)) { + return false; + } + + FindByEmailCommand otherFindCommand = (FindByEmailCommand) other; + return this.getPredicate().equals(otherFindCommand.getPredicate()); + } +} From 3a8fb0327420ce905b80ea11a14db43fb92ed7c2 Mon Sep 17 00:00:00 2001 From: yooplo Date: Thu, 10 Oct 2024 19:37:27 +0800 Subject: [PATCH 2/3] Add test cases for FindByEmailCommand --- .../logic/parser/FindCommandParser.java | 5 ++ .../commands/FindByContactCommandTest.java | 4 +- .../commands/FindByEmailCommandTest.java | 76 +++++++++++++++++++ .../logic/parser/FindCommandParserTest.java | 15 ++++ 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/test/java/seedu/address/logic/commands/FindByEmailCommandTest.java diff --git a/src/main/java/seedu/address/logic/parser/FindCommandParser.java b/src/main/java/seedu/address/logic/parser/FindCommandParser.java index d88839e480a..c020204ea36 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCommandParser.java @@ -8,9 +8,11 @@ import seedu.address.logic.commands.AbstractFindCommand; import seedu.address.logic.commands.FindByContactCommand; +import seedu.address.logic.commands.FindByEmailCommand; import seedu.address.logic.commands.FindByNameCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.person.ContactContainsKeywordsPredicate; +import seedu.address.model.person.EmailContainsKeywordsPredicate; import seedu.address.model.person.NameContainsKeywordsPredicate; /** @@ -49,6 +51,9 @@ public AbstractFindCommand parse(String args) throws ParseException { case "/c": return new FindByContactCommand( new ContactContainsKeywordsPredicate(Arrays.asList(searchTermArray))); + case "/e": + return new FindByEmailCommand( + new EmailContainsKeywordsPredicate(Arrays.asList(searchTermArray))); default: return null; // temporary value, this should not occur due to regex } diff --git a/src/test/java/seedu/address/logic/commands/FindByContactCommandTest.java b/src/test/java/seedu/address/logic/commands/FindByContactCommandTest.java index e0271ef7ac5..de6c00ed0fd 100644 --- a/src/test/java/seedu/address/logic/commands/FindByContactCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/FindByContactCommandTest.java @@ -22,8 +22,8 @@ * Contains integration tests (interaction with the Model) for {@code FindByContactCommand}. */ public class FindByContactCommandTest { - private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); - private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + private final Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + private final Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); @Test public void equals() { diff --git a/src/test/java/seedu/address/logic/commands/FindByEmailCommandTest.java b/src/test/java/seedu/address/logic/commands/FindByEmailCommandTest.java new file mode 100644 index 00000000000..8bdb7a2a840 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/FindByEmailCommandTest.java @@ -0,0 +1,76 @@ +package seedu.address.logic.commands; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.jupiter.api.Test; + +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.model.person.EmailContainsKeywordsPredicate; + +public class FindByEmailCommandTest { + + private final Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + private final Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + + @Test + public void equals() { + EmailContainsKeywordsPredicate firstPredicate = + new EmailContainsKeywordsPredicate(Collections.singletonList("first")); + EmailContainsKeywordsPredicate secondPredicate = + new EmailContainsKeywordsPredicate(Collections.singletonList("second")); + + FindByEmailCommand findFirstCommand = new FindByEmailCommand(firstPredicate); + FindByEmailCommand findSecondCommand = new FindByEmailCommand(secondPredicate); + + // same object -> returns true + assertEquals(findFirstCommand, findFirstCommand); + + // same values -> returns true + FindByEmailCommand findFirstCommandCopy = new FindByEmailCommand(firstPredicate); + assertEquals(findFirstCommand, findFirstCommandCopy); + + // different types -> returns false + assertNotEquals(1, findFirstCommand); + + // null -> returns false + assertNotEquals(null, findFirstCommand); + + // different person -> returns false + assertNotEquals(findFirstCommand, findSecondCommand); + } + + @Test + public void execute_zeroKeywords_noPersonFound() { + String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0); + EmailContainsKeywordsPredicate predicate = preparePredicate(" "); + FindByEmailCommand command = new FindByEmailCommand(predicate); + expectedModel.updateFilteredPersonList(predicate); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Collections.emptyList(), model.getFilteredPersonList()); + } + + @Test + public void toStringMethod() { + EmailContainsKeywordsPredicate predicate = new EmailContainsKeywordsPredicate(Arrays.asList("keyword")); + FindByEmailCommand findCommand = new FindByEmailCommand(predicate); + String expected = FindByEmailCommand.class.getCanonicalName() + "{predicate=" + predicate + "}"; + assertEquals(expected, findCommand.toString()); + } + + /** + * Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}. + */ + private EmailContainsKeywordsPredicate preparePredicate(String userInput) { + return new EmailContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+"))); + } + +} diff --git a/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java b/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java index 7975408a57e..e54ef478afd 100644 --- a/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java @@ -10,8 +10,10 @@ import seedu.address.logic.commands.AbstractFindCommand; import seedu.address.logic.commands.FindByContactCommand; +import seedu.address.logic.commands.FindByEmailCommand; import seedu.address.logic.commands.FindByNameCommand; import seedu.address.model.person.ContactContainsKeywordsPredicate; +import seedu.address.model.person.EmailContainsKeywordsPredicate; import seedu.address.model.person.NameContainsKeywordsPredicate; public class FindCommandParserTest { @@ -49,4 +51,17 @@ public void parse_validArgs_returnsFindByContactCommand() { assertParseSuccess(parser, "/c \n 91234567 \n \t 995 \t", expectedFindCommand); } + @Test + public void parse_validArgs_returnsFindByEmailCommand() { + FindByEmailCommand expectedFindCommand = + new FindByEmailCommand(new EmailContainsKeywordsPredicate( + Arrays.asList("ryan@gmail.com", "tasha@gmail.com"))); + + // no leading and trailing whitespaces + assertParseSuccess(parser, "/e ryan@gmail.com tasha@gmail.com", expectedFindCommand); + + // multiple whitespaces between keywords + assertParseSuccess(parser, "/e \n ryan@gmail.com \n \t tasha@gmail.com \t", expectedFindCommand); + } + } From db4fd07f6db2331090db6cb3fb71fc663b6df553 Mon Sep 17 00:00:00 2001 From: yooplo Date: Thu, 10 Oct 2024 19:49:51 +0800 Subject: [PATCH 3/3] Update error message for wrong input format --- .../seedu/address/logic/commands/AbstractFindCommand.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java b/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java index d94bf2b8731..e088cf9864d 100644 --- a/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java +++ b/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java @@ -14,11 +14,17 @@ public abstract class AbstractFindCommand extends Command { public static final String COMMAND_WORD = "find"; + public static final String NAME_COMMAND_WORD = "/n"; + public static final String EMAIL_COMMAND_WORD = "/e"; + public static final String CONTACT_COMMAND_WORD = "/c"; 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"; + + "Example:\n" + + COMMAND_WORD + NAME_COMMAND_WORD + " alice bob charlie\n" + + COMMAND_WORD + EMAIL_COMMAND_WORD + " bob@gmail.com\n" + + COMMAND_WORD + CONTACT_COMMAND_WORD + " 12345678\n"; private final ContainsKeywordsPredicate predicate;