Skip to content

Commit

Permalink
Merge pull request #94 from Harithhh06/branch-find-address
Browse files Browse the repository at this point in the history
Update FindAddress Command
  • Loading branch information
rubinnn authored Oct 17, 2024
2 parents 9311fba + 32e9023 commit 40f57ec
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 56 deletions.
5 changes: 1 addition & 4 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,18 @@ Result for `fp 9234`:
![result for 'find 9234'](images/find9234Result.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`:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,14 +17,12 @@ public class FindAddressCommandParser implements Parser<FindAddressCommand> {
* @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));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package seedu.address.model.person;

import java.util.List;
import java.util.function.Predicate;

import seedu.address.commons.util.ToStringBuilder;
Expand All @@ -10,17 +9,19 @@
*/
public class AddressContainsKeywordsPredicate implements Predicate<Person> {

private final List<String> keywords;
private final String keywords;

public AddressContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
public AddressContainsKeywordsPredicate(String 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 keywords.stream()
.anyMatch(keywords -> person.getAddress().value.toLowerCase().contains(keywords.toLowerCase()));
return person.getAddress().value.toLowerCase().contains(keywords.toLowerCase());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,9 +29,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);
Expand Down Expand Up @@ -66,17 +65,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());
Expand All @@ -86,7 +85,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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ public void parseCommand_findPhone() throws Exception {

@Test
public void parseCommand_findAddress() throws Exception {
List<String> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,10 +23,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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,20 +12,20 @@ public class AddressContainsKeywordsPredicateTest {

@Test
public void equals() {
List<String> firstPredicateKeywordList = Collections.singletonList("Tampines");
List<String> 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
Expand All @@ -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", "[email protected]", "Main"));
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("12345")
.withEmail("[email protected]").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<String> keywords = List.of("Tampines", "Street 1");
String keywords = "Tampines Street 1";
AddressContainsKeywordsPredicate predicate = new AddressContainsKeywordsPredicate(keywords);

String expected = AddressContainsKeywordsPredicate.class.getCanonicalName() + "{keywords=" + keywords + "}";
Expand Down

0 comments on commit 40f57ec

Please sign in to comment.