Skip to content

Commit

Permalink
Implement search by address
Browse files Browse the repository at this point in the history
  • Loading branch information
tingxuanp committed Oct 14, 2024
1 parent cd4d46e commit 18db59e
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/main/java/seedu/address/logic/commands/FindAddressCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.Model;
import seedu.address.model.person.AddressContainsKeywordsPredicate;

/**
* Finds and lists all persons in address book whose address contains any of the argument keywords.
* Keyword matching is case-insensitive and allows partial matching.
*/
public class FindAddressCommand extends FindCommand {

public static final String MESSAGE_FIND_ADDRESS_PERSON_SUCCESS = "Search for address containing \"%s\" "
+ " was successful. Showing results:";

public static final String MESSAGE_FIND_ADDRESS_PERSON_UNSUCCESSFUL = "No contacts found.";

private final AddressContainsKeywordsPredicate predicate;

/**
* Command to filter contacts in WedLinker based on phone numbers.
* The search matches any parts of the phone numbers.
*
* @param predicate Keywords used to filter contacts by their phone number.
*/
public FindAddressCommand(AddressContainsKeywordsPredicate predicate) {
super();
this.predicate = predicate;
}

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(predicate);

if (!model.getFilteredPersonList().isEmpty()) {
return new CommandResult(String.format(MESSAGE_FIND_ADDRESS_PERSON_SUCCESS, predicate.getDisplayString()));
} else {
return new CommandResult(MESSAGE_FIND_ADDRESS_PERSON_UNSUCCESSFUL);
}
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof FindAddressCommand)) {
return false;
}

FindAddressCommand otherFindCommand = (FindAddressCommand) other;
return predicate.equals(otherFindCommand.predicate);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("predicate", predicate)
.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import java.util.Arrays;
import java.util.List;

import seedu.address.logic.commands.FindAddressCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.FindNameCommand;
import seedu.address.logic.commands.FindPhoneCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.AddressContainsKeywordsPredicate;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.PhoneContainsKeywordsPredicate;

Expand Down Expand Up @@ -60,6 +62,12 @@ public FindCommand parse(String args) throws ParseException {
return new FindPhoneCommand(new PhoneContainsKeywordsPredicate(phoneKeywords));
}

if (hasAddressPrefix) {
String addressInput = argMultimap.getValue(PREFIX_ADDRESS).get().trim(); // Get the actual address input
List<String> addressKeywords = Arrays.asList(addressInput.split("\\s+"));
return new FindAddressCommand(new AddressContainsKeywordsPredicate(addressKeywords));
}

throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package seedu.address.model.person;

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

import seedu.address.commons.util.StringUtil;
import seedu.address.commons.util.ToStringBuilder;

/**
* Tests that a {@code Person}'s {@code Address} matches any of the keywords given.
*/
public class AddressContainsKeywordsPredicate implements Predicate<Person> {
private final List<String> keywords;

public AddressContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
}

@Override
public boolean test(Person person) {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsPartialWordIgnoreCase(person.getAddress().value, keyword));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

AddressContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (AddressContainsKeywordsPredicate) other;
return keywords.equals(otherNameContainsKeywordsPredicate.keywords);
}

@Override
public String toString() {
return new ToStringBuilder(this).add("keywords", keywords).toString();
}

public String getDisplayString() {
return String.join(", ", keywords);
}
}

0 comments on commit 18db59e

Please sign in to comment.