From dbe629da4d3419c87b4a98a88773fc2694491561 Mon Sep 17 00:00:00 2001 From: yooplo Date: Tue, 15 Oct 2024 14:07:37 +0800 Subject: [PATCH 1/2] Update Find command tag format original was / now is / To make it in uniform with the other commands --- .../address/logic/commands/AbstractFindCommand.java | 12 ++++++------ .../address/logic/commands/FindByContactCommand.java | 2 +- .../address/logic/commands/FindByEmailCommand.java | 2 +- .../address/logic/commands/FindByNameCommand.java | 2 +- .../address/logic/parser/FindCommandParser.java | 8 ++++---- .../address/logic/parser/FindCommandParserTest.java | 12 ++++++------ 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java b/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java index e088cf9864d..65f307a8bfc 100644 --- a/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java +++ b/src/main/java/seedu/address/logic/commands/AbstractFindCommand.java @@ -14,17 +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 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:\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"; + + 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; diff --git a/src/main/java/seedu/address/logic/commands/FindByContactCommand.java b/src/main/java/seedu/address/logic/commands/FindByContactCommand.java index 9e1565a443e..22cd2d52edb 100644 --- a/src/main/java/seedu/address/logic/commands/FindByContactCommand.java +++ b/src/main/java/seedu/address/logic/commands/FindByContactCommand.java @@ -6,7 +6,7 @@ * Finds and lists all persons in address book whose contact contains any of the argument keywords. */ public class FindByContactCommand extends AbstractFindCommand { - public static final String COMMAND_WORD = "find /c"; + public static final String COMMAND_WORD = "find c/"; public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose contact numbers " + "contain any of the specified keywords (case-insensitive) and displays them as a list with indices.\n" diff --git a/src/main/java/seedu/address/logic/commands/FindByEmailCommand.java b/src/main/java/seedu/address/logic/commands/FindByEmailCommand.java index c543e4b5295..9be64d4392b 100644 --- a/src/main/java/seedu/address/logic/commands/FindByEmailCommand.java +++ b/src/main/java/seedu/address/logic/commands/FindByEmailCommand.java @@ -8,7 +8,7 @@ */ public class FindByEmailCommand extends AbstractFindCommand { - public static final String COMMAND_WORD = "find /e"; + 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" diff --git a/src/main/java/seedu/address/logic/commands/FindByNameCommand.java b/src/main/java/seedu/address/logic/commands/FindByNameCommand.java index 8503617cba7..84a6c411bfb 100644 --- a/src/main/java/seedu/address/logic/commands/FindByNameCommand.java +++ b/src/main/java/seedu/address/logic/commands/FindByNameCommand.java @@ -7,7 +7,7 @@ * Keyword matching is case-insensitive. */ public class FindByNameCommand extends AbstractFindCommand { - public static final String COMMAND_WORD = "find /n"; + public static final String COMMAND_WORD = "find n/"; 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" diff --git a/src/main/java/seedu/address/logic/parser/FindCommandParser.java b/src/main/java/seedu/address/logic/parser/FindCommandParser.java index c020204ea36..e6c0f76a30a 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCommandParser.java @@ -21,7 +21,7 @@ public class FindCommandParser implements Parser { public static final Pattern KEYWORD_EXTRACTOR = - Pattern.compile("^(?/[cen])\\s*(?[\\S\\s]+)$"); + Pattern.compile("^(?[cen]/)\\s*(?[\\S\\s]+)$"); /** * Parses the given {@code String} of arguments in the context of the FindCommand @@ -45,13 +45,13 @@ public AbstractFindCommand parse(String args) throws ParseException { // return appropriate FindCommand class depending on tag switch (tag) { - case "/n": + case "n/": return new FindByNameCommand( new NameContainsKeywordsPredicate(Arrays.asList(searchTermArray))); - case "/c": + case "c/": return new FindByContactCommand( new ContactContainsKeywordsPredicate(Arrays.asList(searchTermArray))); - case "/e": + case "e/": return new FindByEmailCommand( new EmailContainsKeywordsPredicate(Arrays.asList(searchTermArray))); default: diff --git a/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java b/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java index e54ef478afd..83ac8f791e0 100644 --- a/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java @@ -32,10 +32,10 @@ public void parse_validArgs_returnsFindByNameCommand() { new FindByNameCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob"))); // no leading and trailing whitespaces - assertParseSuccess(parser, "/n Alice Bob", expectedFindCommand); + assertParseSuccess(parser, "n/ Alice Bob", expectedFindCommand); // multiple whitespaces between keywords - assertParseSuccess(parser, "/n \n Alice \n \t Bob \t", expectedFindCommand); + assertParseSuccess(parser, "n/ \n Alice \n \t Bob \t", expectedFindCommand); } @Test @@ -45,10 +45,10 @@ public void parse_validArgs_returnsFindByContactCommand() { Arrays.asList("91234567", "995"))); // no leading and trailing whitespaces - assertParseSuccess(parser, "/c 91234567 995", expectedFindCommand); + assertParseSuccess(parser, "c/ 91234567 995", expectedFindCommand); // multiple whitespaces between keywords - assertParseSuccess(parser, "/c \n 91234567 \n \t 995 \t", expectedFindCommand); + assertParseSuccess(parser, "c/ \n 91234567 \n \t 995 \t", expectedFindCommand); } @Test @@ -58,10 +58,10 @@ public void parse_validArgs_returnsFindByEmailCommand() { Arrays.asList("ryan@gmail.com", "tasha@gmail.com"))); // no leading and trailing whitespaces - assertParseSuccess(parser, "/e ryan@gmail.com tasha@gmail.com", expectedFindCommand); + 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); + assertParseSuccess(parser, "e/ \n ryan@gmail.com \n \t tasha@gmail.com \t", expectedFindCommand); } } From 00948941fb838d751fc83513f02c986dcabe6b8e Mon Sep 17 00:00:00 2001 From: yooplo Date: Tue, 15 Oct 2024 14:43:06 +0800 Subject: [PATCH 2/2] Improve find to include partial matches Include additional test cases --- .../ContactContainsKeywordsPredicate.java | 3 +-- .../person/EmailContainsKeywordsPredicate.java | 3 +-- .../person/NameContainsKeywordsPredicate.java | 3 +-- .../commands/FindByContactCommandTest.java | 13 +++++++++++++ .../logic/commands/FindByEmailCommandTest.java | 12 ++++++++++++ .../logic/commands/FindByNameCommandTest.java | 17 +++++++++++++++-- 6 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java index cfec0c4b404..ddec46ae176 100644 --- a/src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java +++ b/src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java @@ -2,7 +2,6 @@ import java.util.List; -import seedu.address.commons.util.StringUtil; import seedu.address.commons.util.ToStringBuilder; /** @@ -17,7 +16,7 @@ public ContactContainsKeywordsPredicate(List keywords) { @Override public boolean test(Person person) { return this.getKeywords().stream() - .anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getPhone().value, keyword)); + .anyMatch(keyword -> person.getPhone().value.toLowerCase().contains(keyword.toLowerCase())); } @Override diff --git a/src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java index 96a256e3f55..13b513226ee 100644 --- a/src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java +++ b/src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java @@ -2,7 +2,6 @@ import java.util.List; -import seedu.address.commons.util.StringUtil; import seedu.address.commons.util.ToStringBuilder; /** @@ -17,7 +16,7 @@ public EmailContainsKeywordsPredicate(List keywords) { @Override public boolean test(Person person) { return super.getKeywords().stream() - .anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getEmail().value, keyword)); + .anyMatch(keyword -> person.getEmail().value.toLowerCase().contains(keyword.toLowerCase())); } @Override diff --git a/src/main/java/seedu/address/model/person/NameContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/person/NameContainsKeywordsPredicate.java index 90037ae21bd..06bc923ed17 100644 --- a/src/main/java/seedu/address/model/person/NameContainsKeywordsPredicate.java +++ b/src/main/java/seedu/address/model/person/NameContainsKeywordsPredicate.java @@ -2,7 +2,6 @@ import java.util.List; -import seedu.address.commons.util.StringUtil; import seedu.address.commons.util.ToStringBuilder; /** @@ -17,7 +16,7 @@ public NameContainsKeywordsPredicate(List keywords) { @Override public boolean test(Person person) { return this.getKeywords().stream() - .anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getName().fullName, keyword)); + .anyMatch(keyword -> person.getName().fullName.toLowerCase().contains(keyword.toLowerCase())); } @Override diff --git a/src/test/java/seedu/address/logic/commands/FindByContactCommandTest.java b/src/test/java/seedu/address/logic/commands/FindByContactCommandTest.java index de6c00ed0fd..588beb964a8 100644 --- a/src/test/java/seedu/address/logic/commands/FindByContactCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/FindByContactCommandTest.java @@ -5,6 +5,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +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; @@ -62,6 +65,16 @@ public void execute_zeroKeywords_noPersonFound() { assertEquals(Collections.emptyList(), model.getFilteredPersonList()); } + @Test + public void execute_resultsWithPartialMatch_multiplePersonsFound() { + String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3); + ContactContainsKeywordsPredicate predicate = preparePredicate("82"); + FindByContactCommand command = new FindByContactCommand(predicate); + expectedModel.updateFilteredPersonList(predicate); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(ELLE, FIONA, GEORGE), model.getFilteredPersonList()); + } + // Commented out because expectedModel does not contain contact numbers // @Test // public void execute_multipleKeywords_multiplePersonsFound() { diff --git a/src/test/java/seedu/address/logic/commands/FindByEmailCommandTest.java b/src/test/java/seedu/address/logic/commands/FindByEmailCommandTest.java index 8bdb7a2a840..75eb90a91c8 100644 --- a/src/test/java/seedu/address/logic/commands/FindByEmailCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/FindByEmailCommandTest.java @@ -4,6 +4,8 @@ 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.DANIEL; +import static seedu.address.testutil.TypicalPersons.ELLE; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; import java.util.Arrays; @@ -58,6 +60,16 @@ public void execute_zeroKeywords_noPersonFound() { assertEquals(Collections.emptyList(), model.getFilteredPersonList()); } + @Test + public void execute_resultsWithPartialMatch_multiplePersonsFound() { + String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 2); + EmailContainsKeywordsPredicate predicate = preparePredicate("ne"); + FindByEmailCommand command = new FindByEmailCommand(predicate); + expectedModel.updateFilteredPersonList(predicate); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(DANIEL, ELLE), model.getFilteredPersonList()); + } + @Test public void toStringMethod() { EmailContainsKeywordsPredicate predicate = new EmailContainsKeywordsPredicate(Arrays.asList("keyword")); diff --git a/src/test/java/seedu/address/logic/commands/FindByNameCommandTest.java b/src/test/java/seedu/address/logic/commands/FindByNameCommandTest.java index 5cc9f6befaf..6dc96f27e9d 100644 --- a/src/test/java/seedu/address/logic/commands/FindByNameCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/FindByNameCommandTest.java @@ -5,7 +5,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW; 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.getTypicalAddressBook; @@ -24,8 +27,8 @@ * Contains integration tests (interaction with the Model) for {@code FindByNameCommand}. */ public class FindByNameCommandTest { - private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); - private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + private final Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + private final Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); @Test public void equals() { @@ -74,6 +77,16 @@ public void execute_multipleKeywords_multiplePersonsFound() { assertEquals(Arrays.asList(CARL, ELLE, FIONA), model.getFilteredPersonList()); } + @Test + public void execute_resultsWithPartialMatch_multiplePersonsFound() { + String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 4); + NameContainsKeywordsPredicate predicate = preparePredicate("n"); + FindByNameCommand command = new FindByNameCommand(predicate); + expectedModel.updateFilteredPersonList(predicate); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(ALICE, BENSON, DANIEL, FIONA), model.getFilteredPersonList()); + } + @Test public void toStringMethod() { NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword"));