From a08a29549dabe0294f235e423a0b2d1960a73bf5 Mon Sep 17 00:00:00 2001 From: harithhh Date: Thu, 17 Oct 2024 16:58:34 +0800 Subject: [PATCH 1/3] Update FindAddress predicate --- .../address/logic/parser/FindAddressCommandParser.java | 8 +++----- .../model/person/AddressContainsKeywordsPredicate.java | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/FindAddressCommandParser.java b/src/main/java/seedu/address/logic/parser/FindAddressCommandParser.java index cd41423bdd7..bfb7c299c50 100644 --- a/src/main/java/seedu/address/logic/parser/FindAddressCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindAddressCommandParser.java @@ -19,14 +19,12 @@ public class FindAddressCommandParser implements Parser { * @throws ParseException if the user input does not conform the expected format */ public FindAddressCommand parse(String args) throws ParseException { - String trimmedArgs = args.trim(); - if (trimmedArgs.isEmpty()) { + String addressKeywords = args.trim(); + if (addressKeywords.isEmpty()) { throw new ParseException( String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindAddressCommand.MESSAGE_USAGE)); } - String[] addressKeywords = trimmedArgs.split("\\s+"); - - return new FindAddressCommand(new AddressContainsKeywordsPredicate(Arrays.asList(addressKeywords))); + return new FindAddressCommand(new AddressContainsKeywordsPredicate(addressKeywords)); } } diff --git a/src/main/java/seedu/address/model/person/AddressContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/person/AddressContainsKeywordsPredicate.java index 1e57ee9127e..4b87c001891 100644 --- a/src/main/java/seedu/address/model/person/AddressContainsKeywordsPredicate.java +++ b/src/main/java/seedu/address/model/person/AddressContainsKeywordsPredicate.java @@ -1,6 +1,5 @@ package seedu.address.model.person; -import java.util.List; import java.util.function.Predicate; import seedu.address.commons.util.ToStringBuilder; @@ -10,17 +9,16 @@ */ public class AddressContainsKeywordsPredicate implements Predicate { - private final List keywords; + private final String keywords; - public AddressContainsKeywordsPredicate(List keywords) { + public AddressContainsKeywordsPredicate(String keywords) { this.keywords = keywords; } @Override public boolean test(Person person) { // Check if any of the keywords match any part of the address - return keywords.stream() - .anyMatch(keywords -> person.getAddress().value.toLowerCase().contains(keywords.toLowerCase())); + return person.getAddress().value.toLowerCase().contains(keywords.toLowerCase()); } @Override From c87932a700c45bb668ff034630693c3470b9b6d9 Mon Sep 17 00:00:00 2001 From: harithhh Date: Thu, 17 Oct 2024 17:30:08 +0800 Subject: [PATCH 2/3] Update test cases and useer guide --- docs/UserGuide.md | 5 +-- .../parser/FindAddressCommandParser.java | 2 - .../AddressContainsKeywordsPredicate.java | 5 ++- .../commands/FindAddressCommandTest.java | 12 +++--- .../logic/parser/ClientHubParserTest.java | 4 +- .../parser/FindAddressCommandParserTest.java | 5 +-- .../AddressContainsKeywordsPredicateTest.java | 38 +++++++------------ 7 files changed, 28 insertions(+), 43 deletions(-) diff --git a/docs/UserGuide.md b/docs/UserGuide.md index bc3b44ae088..b861951676d 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -168,21 +168,18 @@ Result for `fp 91031282`: ![result for 'find alex david'](images/findAlexDavidResult.png) -### Locating persons by address: `fa` +### Locating persons by address : `fa` Finds persons whose address matches any part of the given keyword(s). Format `fa KEYWORD(s)` * The search is case-insensitive. e.g `tampines` will match `Tampines` -* The order of the keywords does not matter. e.g. `Street 45` will match `45 Street` * Only the address of the contact is searched. * Persons with address with any matching substring to the keyword will be returned. -* Persons matching at least one keyword will be returned (i.e. `OR` search). Examples: * `fa Blk` returns `Blk 45` and `Blk 35` -* `fa blk street` returns `Tampines Street 1` and `Blk 35 Ang Mo Kio` * `fa tampines` returns `Blk 47 Tampines Street 20` Result for `fa tampines`: diff --git a/src/main/java/seedu/address/logic/parser/FindAddressCommandParser.java b/src/main/java/seedu/address/logic/parser/FindAddressCommandParser.java index bfb7c299c50..e81bb3b0692 100644 --- a/src/main/java/seedu/address/logic/parser/FindAddressCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindAddressCommandParser.java @@ -2,8 +2,6 @@ import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; -import java.util.Arrays; - import seedu.address.logic.commands.FindAddressCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.person.AddressContainsKeywordsPredicate; diff --git a/src/main/java/seedu/address/model/person/AddressContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/person/AddressContainsKeywordsPredicate.java index 4b87c001891..74dc30a0618 100644 --- a/src/main/java/seedu/address/model/person/AddressContainsKeywordsPredicate.java +++ b/src/main/java/seedu/address/model/person/AddressContainsKeywordsPredicate.java @@ -12,11 +12,14 @@ public class AddressContainsKeywordsPredicate implements Predicate { private final String keywords; public AddressContainsKeywordsPredicate(String keywords) { - this.keywords = keywords; + this.keywords = keywords.trim(); } @Override public boolean test(Person person) { + if (keywords.isEmpty()) { + return false; + } // Check if any of the keywords match any part of the address return person.getAddress().value.toLowerCase().contains(keywords.toLowerCase()); } diff --git a/src/test/java/seedu/address/logic/commands/FindAddressCommandTest.java b/src/test/java/seedu/address/logic/commands/FindAddressCommandTest.java index e86d4a87b76..9953cea5e21 100644 --- a/src/test/java/seedu/address/logic/commands/FindAddressCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/FindAddressCommandTest.java @@ -30,9 +30,9 @@ public class FindAddressCommandTest { @Test public void equals() { AddressContainsKeywordsPredicate firstPredicate = - new AddressContainsKeywordsPredicate(Collections.singletonList("first")); + new AddressContainsKeywordsPredicate("first"); AddressContainsKeywordsPredicate secondPredicate = - new AddressContainsKeywordsPredicate(Collections.singletonList("second")); + new AddressContainsKeywordsPredicate("second"); FindAddressCommand findFirstAddressCommand = new FindAddressCommand(firstPredicate); FindAddressCommand findSecondAddressCommand = new FindAddressCommand(secondPredicate); @@ -66,17 +66,17 @@ public void execute_zeroKeywords_noPersonFound() { @Test public void execute_multipleKeywords_multiplePersonsFound() { - String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3); + String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 2); AddressContainsKeywordsPredicate predicate = prepareAddressPredicate("th street"); FindAddressCommand command = new FindAddressCommand(predicate); expectedModel.updateFilteredPersonList(predicate); assertCommandSuccess(command, model, expectedMessage, expectedModel); - assertEquals(Arrays.asList(CARL, DANIEL, GEORGE), model.getFilteredPersonList()); + assertEquals(Arrays.asList(DANIEL, GEORGE), model.getFilteredPersonList()); } @Test public void toStringMethod() { - AddressContainsKeywordsPredicate predicate = new AddressContainsKeywordsPredicate(Arrays.asList("Tampines")); + AddressContainsKeywordsPredicate predicate = new AddressContainsKeywordsPredicate("Tampines"); FindAddressCommand findAddressCommand = new FindAddressCommand(predicate); String expected = FindAddressCommand.class.getCanonicalName() + "{predicate=" + predicate + "}"; assertEquals(expected, findAddressCommand.toString()); @@ -86,7 +86,7 @@ public void toStringMethod() { * Parses {@code userInput} into a {@code AddressContainsKeywordsPredicate}. */ private AddressContainsKeywordsPredicate prepareAddressPredicate(String userInput) { - return new AddressContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+"))); + return new AddressContainsKeywordsPredicate(userInput); } } diff --git a/src/test/java/seedu/address/logic/parser/ClientHubParserTest.java b/src/test/java/seedu/address/logic/parser/ClientHubParserTest.java index b94b77b61f4..bb843dee236 100644 --- a/src/test/java/seedu/address/logic/parser/ClientHubParserTest.java +++ b/src/test/java/seedu/address/logic/parser/ClientHubParserTest.java @@ -104,9 +104,9 @@ public void parseCommand_findPhone() throws Exception { @Test public void parseCommand_findAddress() throws Exception { - List keywords = Arrays.asList("Tampines", "Street", "1"); + String keywords = "Tampines"; FindAddressCommand command = (FindAddressCommand) parser.parseCommand( - FindAddressCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" "))); + FindAddressCommand.COMMAND_WORD + " " + keywords); assertEquals(new FindAddressCommand(new AddressContainsKeywordsPredicate(keywords)), command); } diff --git a/src/test/java/seedu/address/logic/parser/FindAddressCommandParserTest.java b/src/test/java/seedu/address/logic/parser/FindAddressCommandParserTest.java index 9ae22fc3ed7..d4bf9ba7dd6 100644 --- a/src/test/java/seedu/address/logic/parser/FindAddressCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/FindAddressCommandParserTest.java @@ -25,10 +25,7 @@ public void parse_emptyArg_throwsParseException() { public void parse_validArgs_returnsFindAddressCommand() { // no leading and trailing whitespaces FindAddressCommand expectedFindAddressCommand = - new FindAddressCommand(new AddressContainsKeywordsPredicate(Arrays.asList("tampines", "blk"))); + new FindAddressCommand(new AddressContainsKeywordsPredicate("tampines blk")); assertParseSuccess(parser, "tampines blk", expectedFindAddressCommand); - - // multiple whitespaces between keywords - assertParseSuccess(parser, " \n tampines \n \t blk \t", expectedFindAddressCommand); } } diff --git a/src/test/java/seedu/address/model/person/AddressContainsKeywordsPredicateTest.java b/src/test/java/seedu/address/model/person/AddressContainsKeywordsPredicateTest.java index 8f3611f9ebc..3415d678795 100644 --- a/src/test/java/seedu/address/model/person/AddressContainsKeywordsPredicateTest.java +++ b/src/test/java/seedu/address/model/person/AddressContainsKeywordsPredicateTest.java @@ -4,10 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - import org.junit.jupiter.api.Test; import seedu.address.testutil.PersonBuilder; @@ -16,20 +12,20 @@ public class AddressContainsKeywordsPredicateTest { @Test public void equals() { - List firstPredicateKeywordList = Collections.singletonList("Tampines"); - List secondPredicateKeywordList = Arrays.asList("Tampines", "Street 1"); + String firstPredicateKeyword = "Tampines"; + String secondPredicateKeywords = "Tampines Street 1"; AddressContainsKeywordsPredicate firstPredicate = - new AddressContainsKeywordsPredicate(firstPredicateKeywordList); + new AddressContainsKeywordsPredicate(firstPredicateKeyword); AddressContainsKeywordsPredicate secondPredicate = - new AddressContainsKeywordsPredicate(secondPredicateKeywordList); + new AddressContainsKeywordsPredicate(secondPredicateKeywords); // same object -> returns true assertTrue(firstPredicate.equals(firstPredicate)); // same values -> returns true AddressContainsKeywordsPredicate firstPredicateCopy = - new AddressContainsKeywordsPredicate(firstPredicateKeywordList); + new AddressContainsKeywordsPredicate(firstPredicateKeyword); assertTrue(firstPredicate.equals(firstPredicateCopy)); // different types -> returns false @@ -45,42 +41,36 @@ public void equals() { @Test public void test_addressContainsKeywords_returnsTrue() { // One keyword - AddressContainsKeywordsPredicate predicate = - new AddressContainsKeywordsPredicate(Collections.singletonList("Tampines")); + AddressContainsKeywordsPredicate predicate = new AddressContainsKeywordsPredicate("Tampines"); assertTrue(predicate.test(new PersonBuilder().withAddress("Tampines Street 1 #01-01").build())); // Multiple keywords - predicate = new AddressContainsKeywordsPredicate(Arrays.asList("Tampines", "Street 1")); - assertTrue(predicate.test(new PersonBuilder().withAddress("Tampines Street 1 #01-01").build())); - - // Only one matching keyword - predicate = new AddressContainsKeywordsPredicate(Arrays.asList("Tampines", "Street 2")); + predicate = new AddressContainsKeywordsPredicate("Tampines Street 1"); assertTrue(predicate.test(new PersonBuilder().withAddress("Tampines Street 1 #01-01").build())); // Mixed-case keywords - predicate = new AddressContainsKeywordsPredicate(Arrays.asList("tAmPiNeS", "StReEt 1")); + predicate = new AddressContainsKeywordsPredicate("tAmPiNeS StReEt 1"); assertTrue(predicate.test(new PersonBuilder().withAddress("Tampines Street 1 #01-01").build())); } @Test public void test_addressDoesNotContainKeywords_returnsFalse() { // Zero keywords - AddressContainsKeywordsPredicate predicate = new AddressContainsKeywordsPredicate(Collections.emptyList()); + AddressContainsKeywordsPredicate predicate = new AddressContainsKeywordsPredicate(""); assertFalse(predicate.test(new PersonBuilder().withAddress("Tampines Street 1 #01-01").build())); // Non-matching keyword - predicate = new AddressContainsKeywordsPredicate(Arrays.asList("Jurong")); + predicate = new AddressContainsKeywordsPredicate("Jurong"); assertFalse(predicate.test(new PersonBuilder().withAddress("Tampines Street 1 #01-01").build())); - // Keywords match name, phone, and email, but does not match address - predicate = new AddressContainsKeywordsPredicate(Arrays.asList("12345", "alice@email.com", "Main")); - assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("12345") - .withEmail("alice@email.com").withAddress("Tampines Street 1 #01-01").build())); + // Not all matching keywords + predicate = new AddressContainsKeywordsPredicate("Tampines Street 2"); + assertFalse(predicate.test(new PersonBuilder().withAddress("Tampines Street 1 #01-01").build())); } @Test public void toStringMethod() { - List keywords = List.of("Tampines", "Street 1"); + String keywords = "Tampines Street 1"; AddressContainsKeywordsPredicate predicate = new AddressContainsKeywordsPredicate(keywords); String expected = AddressContainsKeywordsPredicate.class.getCanonicalName() + "{keywords=" + keywords + "}"; From 32e90231014e45f598b90256c620f4196bfd5391 Mon Sep 17 00:00:00 2001 From: harithhh Date: Thu, 17 Oct 2024 17:31:59 +0800 Subject: [PATCH 3/3] Update checkstyle --- .../seedu/address/logic/commands/FindAddressCommandTest.java | 1 - .../address/logic/parser/FindAddressCommandParserTest.java | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/test/java/seedu/address/logic/commands/FindAddressCommandTest.java b/src/test/java/seedu/address/logic/commands/FindAddressCommandTest.java index 9953cea5e21..613ff7dd817 100644 --- a/src/test/java/seedu/address/logic/commands/FindAddressCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/FindAddressCommandTest.java @@ -5,7 +5,6 @@ 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.CARL; import static seedu.address.testutil.TypicalPersons.DANIEL; import static seedu.address.testutil.TypicalPersons.GEORGE; import static seedu.address.testutil.TypicalPersons.getTypicalClientHub; diff --git a/src/test/java/seedu/address/logic/parser/FindAddressCommandParserTest.java b/src/test/java/seedu/address/logic/parser/FindAddressCommandParserTest.java index d4bf9ba7dd6..85d7c87b20b 100644 --- a/src/test/java/seedu/address/logic/parser/FindAddressCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/FindAddressCommandParserTest.java @@ -4,8 +4,6 @@ import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; -import java.util.Arrays; - import org.junit.jupiter.api.Test; import seedu.address.logic.commands.FindAddressCommand;