Skip to content

Commit

Permalink
Allow multiple address search
Browse files Browse the repository at this point in the history
  • Loading branch information
tingxuanp committed Oct 21, 2024
1 parent a205916 commit c8fda1f
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 26 deletions.
15 changes: 10 additions & 5 deletions src/main/java/seedu/address/logic/parser/FindCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -40,7 +41,7 @@ public FindCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG);

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_TAG);

String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
Expand Down Expand Up @@ -83,11 +84,15 @@ public FindCommand parse(String args) throws ParseException {
}

if (hasAddressPrefix) {
String addressInput = argMultimap.getValue(PREFIX_ADDRESS).get().trim(); // Get the actual address input
if (addressInput.isEmpty()) {
throw new ParseException("Address cannot be empty!");
// Collect all address inputs
List<String> addressKeywords = new ArrayList<>();
for (String address : argMultimap.getAllValues(PREFIX_ADDRESS)) {
String addressInput = address.trim();
if (addressInput.isEmpty()) {
throw new ParseException("Address cannot be empty!");
}
addressKeywords.add(addressInput);
}
List<String> addressKeywords = Arrays.asList(addressInput.split("\\s+"));
return new FindAddressCommand(new AddressContainsKeywordsPredicate(addressKeywords));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public AddressContainsKeywordsPredicate(List<String> keywords) {
@Override
public boolean test(Person person) {
return keywords.stream()
.allMatch(keyword -> StringUtil.containsPartialWordIgnoreCase(person.getAddress().value, keyword));
.anyMatch(keyword -> StringUtil.containsPartialWordIgnoreCase(person.getAddress().value, keyword));
}

@Override
Expand All @@ -26,10 +26,10 @@ public boolean equals(Object other) {
}

// instanceof handles nulls
if (!(other instanceof AddressContainsKeywordsPredicate otherNameContainsKeywordsPredicate)) {
if (!(other instanceof AddressContainsKeywordsPredicate otherAddressContainsKeywordsPredicate)) {
return false;
}

return keywords.equals(otherNameContainsKeywordsPredicate.keywords);
return keywords.equals(otherAddressContainsKeywordsPredicate.keywords);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public boolean equals(Object other) {
}

// instanceof handles nulls
if (!(other instanceof EmailContainsKeywordsPredicate otherNameContainsKeywordsPredicate)) {
if (!(other instanceof EmailContainsKeywordsPredicate otherEmailContainsKeywordsPredicate)) {
return false;
}

return keywords.equals(otherNameContainsKeywordsPredicate.keywords);
return keywords.equals(otherEmailContainsKeywordsPredicate.keywords);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public boolean equals(Object other) {
}

// instanceof handles nulls
if (!(other instanceof PhoneContainsKeywordsPredicate otherNameContainsKeywordsPredicate)) {
if (!(other instanceof PhoneContainsKeywordsPredicate otherPhoneContainsKeywordsPredicate)) {
return false;
}

return keywords.equals(otherNameContainsKeywordsPredicate.keywords);
return keywords.equals(otherPhoneContainsKeywordsPredicate.keywords);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public boolean equals(Object other) {
}

// instanceof handles nulls
if (!(other instanceof TagContainsKeywordsPredicate otherNameContainsKeywordsPredicate)) {
if (!(other instanceof TagContainsKeywordsPredicate otherTagContainsKeywordsPredicate)) {
return false;
}

return keywords.equals(otherNameContainsKeywordsPredicate.keywords);
return keywords.equals(otherTagContainsKeywordsPredicate.keywords);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ public void toStringMethod() {
* Parses {@code userInput} into a {@code AddressContainsKeywordsPredicate}.
*/
private AddressContainsKeywordsPredicate preparePredicate(String userInput) {
return new AddressContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
return new AddressContainsKeywordsPredicate(Arrays.asList(userInput));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,24 @@ public void parse_missingNameWithTrailingWhiteSpace_throwsParseException() {
public void parse_validFindAddressArgs_returnsFindAddressCommand() {
// no leading and trailing whitespaces
FindAddressCommand expectedFindCommand =
new FindAddressCommand(new AddressContainsKeywordsPredicate(Arrays.asList("5,", "Clementi",
"Ave", "4,", "#03-945")));
assertParseSuccess(parser, "find a/5, Clementi Ave 4, #03-945", expectedFindCommand);
new FindAddressCommand(new AddressContainsKeywordsPredicate(Arrays.asList("5 Clementi Ave 4 "
+ "#03-945")));
assertParseSuccess(parser, "find a/5 Clementi Ave 4 #03-945", expectedFindCommand);

// multiple whitespaces between keywords
assertParseSuccess(parser, "find a/ \n5, Clementi Ave 4, \t #03-945 \t", expectedFindCommand);
// multiple whitespaces before and after keywords
assertParseSuccess(parser, "find a/ \n5 Clementi Ave 4 #03-945 \t", expectedFindCommand);
}

@Test
public void parse_validMultipleFindAddressArgs_returnsFindAddressCommand() {
// no leading and trailing whitespaces
FindAddressCommand expectedFindCommand =
new FindAddressCommand(new AddressContainsKeywordsPredicate(Arrays.asList("5 Clementi Ave 4 "
+ "#03-945", "Lorong 1")));
assertParseSuccess(parser, "find a/5 Clementi Ave 4 #03-945 a/Lorong 1", expectedFindCommand);

// multiple whitespaces before and after keywords
assertParseSuccess(parser, "find a/ \n5 Clementi Ave 4 #03-945 \t a/ \t Lorong 1", expectedFindCommand);
}

@Test
Expand All @@ -94,7 +106,6 @@ public void parse_missingAddressAfterPrefix_throwsParseException() {

// Check for correct error message
assertEquals("Address cannot be empty!", thrown.getMessage());

}

@Test
Expand All @@ -108,6 +119,17 @@ public void missingAddressWithTrailingWhiteSpace_throwsParseException() {
assertEquals("Address cannot be empty!", thrown.getMessage());
}

@Test
public void missingAddressWithTrailingWhiteSpace2_throwsParseException() {
String input = "find a/ Woodlands Avenue 3 a/\t \n"; // Input with empty name prefix
ParseException thrown = assertThrows(ParseException.class, () -> {
parser.parse(input);
});

// Check for correct error message
assertEquals("Address cannot be empty!", thrown.getMessage());
}

@Test
public void parse_validFindPhoneArgs_returnsFindPhoneCommand() {
// no leading and trailing whitespaces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public void test_addressContainsKeywords_returnsTrue() {
new AddressContainsKeywordsPredicate(Arrays.asList("74 University Town, #04-02"));
assertTrue(predicate.test(new PersonBuilder().withAddress("74 University Town, #04-02").build()));

// One full-matching keyword
predicate = new AddressContainsKeywordsPredicate(Arrays.asList("73 Jurong Island, #02-04"));
assertTrue(predicate.test(new PersonBuilder().withAddress("73 Jurong Island, #02-04").build()));

// One partial-matching keyword
predicate = new AddressContainsKeywordsPredicate(Arrays.asList("University Town"));
assertTrue(predicate.test(new PersonBuilder().withAddress("74 University Town, #04-02").build()));

// One partial-matching keyword, case-insensitive
predicate = new AddressContainsKeywordsPredicate(Arrays.asList("uNiversiTy Tow"));
assertTrue(predicate.test(new PersonBuilder().withAddress("74 University Town, #04-02").build()));
}

@Test
Expand All @@ -66,7 +66,7 @@ public void test_addressDoesNotContainKeywords_returnsFalse() {

// Keywords match name, email and phone, but does not match address
predicate = new AddressContainsKeywordsPredicate(Arrays.asList("Alice", "12345",
"[email protected]", "West", "Street"));
"[email protected]", "West", "Avenue"));
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("12345")
.withEmail("[email protected]").withAddress("Main Street").build()));
}
Expand Down

0 comments on commit c8fda1f

Please sign in to comment.