Skip to content

Commit

Permalink
Merge pull request AY2425S1-CS2103T-F15-4#116 from tingxuanp/edit-add…
Browse files Browse the repository at this point in the history
…ress-parser

Allow multiple address search
  • Loading branch information
DanzaSeah authored Oct 24, 2024
2 parents 8799d6a + 7c50466 commit 5966e95
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 30 deletions.
31 changes: 22 additions & 9 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 All @@ -30,6 +31,14 @@
*/
public class FindCommandParser implements Parser<FindCommand> {

public static final String NAME_CANNOT_BE_EMPTY = "Name cannot be empty!";
public static final String PHONE_NUMBER_CANNOT_BE_EMPTY = "Phone number cannot be empty!";
public static final String EMAIL_CANNOT_BE_EMPTY = "Email address cannot be empty!";
public static final String ADDRESS_CANNOT_BE_EMPTY = "Address cannot be empty!";
public static final String TAG_CANNOT_BE_EMPTY = "Tag cannot be empty!";



/**
* Parses the given {@code String} of arguments in the context of the FindCommand
* and returns a FindCommand object for execution.
Expand All @@ -40,7 +49,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 All @@ -58,7 +67,7 @@ public FindCommand parse(String args) throws ParseException {
if (hasNamePrefix) {
String nameInput = argMultimap.getValue(PREFIX_NAME).get().trim(); // Get the actual name input
if (nameInput.isEmpty()) {
throw new ParseException("Name cannot be empty!");
throw new ParseException(NAME_CANNOT_BE_EMPTY);
}
List<String> nameKeywords = Arrays.asList(nameInput.split("\\s+"));
return new FindNameCommand(new NameContainsKeywordsPredicate(nameKeywords));
Expand All @@ -67,7 +76,7 @@ public FindCommand parse(String args) throws ParseException {
if (hasPhonePrefix) {
String phoneNumberInput = argMultimap.getValue(PREFIX_PHONE).get().trim(); // Get the actual phone input
if (phoneNumberInput.isEmpty()) {
throw new ParseException("Phone number cannot be empty!");
throw new ParseException(PHONE_NUMBER_CANNOT_BE_EMPTY);
}
List<String> phoneKeywords = Arrays.asList(phoneNumberInput.split("\\s+"));
return new FindPhoneCommand(new PhoneContainsKeywordsPredicate(phoneKeywords));
Expand All @@ -76,25 +85,29 @@ public FindCommand parse(String args) throws ParseException {
if (hasEmailPrefix) {
String emailInput = argMultimap.getValue(PREFIX_EMAIL).get().trim(); // Get the actual email input
if (emailInput.isEmpty()) {
throw new ParseException("Email address cannot be empty!");
throw new ParseException(EMAIL_CANNOT_BE_EMPTY);
}
List<String> emailKeywords = Arrays.asList(emailInput.split("\\s+"));
return new FindEmailCommand(new EmailContainsKeywordsPredicate(emailKeywords));
}

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));
}

if (hasTagPrefix) {
String tagInput = argMultimap.getValue(PREFIX_TAG).get().trim(); // Get the actual tag input
if (tagInput.isEmpty()) {
throw new ParseException("Tag cannot be empty!");
throw new ParseException(TAG_CANNOT_BE_EMPTY);
}
List<String> tagKeywords = Arrays.asList(tagInput.split("\\s+"));
return new FindTagCommand(new TagContainsKeywordsPredicate(tagKeywords));
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 5966e95

Please sign in to comment.