forked from AY2425S1-CS2103T-F15-4/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/main/java/seedu/address/logic/commands/FindAddressCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/seedu/address/model/person/AddressContainsKeywordsPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |