forked from nus-cs2103-AY2425S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from itsme-zeix/add-multiple-filtering
Implement Multi-Flag Filtering Feature
- Loading branch information
Showing
6 changed files
with
243 additions
and
67 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -203,21 +203,26 @@ edit 69 n/ TAN LESHEW p/ 77337733 e/ [email protected] a/ COM3 j/ doctor i/ 100000000 | |
This feature allows users to search for customers by specific details such as name, address, email, phone number, job title, or remarks. | ||
|
||
**How to Use It:** | ||
To perform a search, use the `filter` command followed by a flag (indicating the field you want to search) and the corresponding search term. Searches are **case-insensitive** and use **substring-matching**. See [substring-matching](#substring-matching) to learn more. | ||
To perform a search, use the `filter` command followed by one or more flags (indicating the fields to search) and the corresponding search terms. | ||
Searches are **case-insensitive** and use [**substring-matching**](#substring-matching). | ||
|
||
- **Command Format:** | ||
``` | ||
filter <FLAG>/ <SEARCH TERM> | ||
``` | ||
- **Examples:** | ||
- Filter customers by name: | ||
``` | ||
filter n/ TAN LESHEW | ||
filter <FLAG>/ <SEARCH TERM> [<ADDITIONAL_FLAGS>/ <SEARCH TERM>] | ||
``` | ||
- **Examples:** | ||
- Filter customers by name: | ||
``` | ||
filter n/ TAN LESHEW | ||
``` | ||
- Filter customers by job: | ||
``` | ||
e.g. filter j/ doctor | ||
``` | ||
``` | ||
filter j/ doctor | ||
``` | ||
- Filter customers by name, job and remark: | ||
``` | ||
filter n/ Gordon Moore j/ doctor r/ award winner | ||
``` | ||
|
||
#### Parameters | ||
| Parameter | Expected Format | Explanation | | ||
|
@@ -235,30 +240,27 @@ filter <FLAG>/ <SEARCH TERM> | |
|
||
#### Substring Matching: | ||
- Substring matching is used for searches, meaning that the search term must match a part of the field in the same order as it appears in the customer record. | ||
- For instance, if a customer’s name is "John Lee", the search term "John", "Lee", or "John Lee" will match, but "Lee John" will not. | ||
|
||
- For instance, if a customer’s name is `Gordon Moore`, the search term `Gordon`, `Moore`, or `Gordon Moore` will match, but `Moore Gordon` will not. | ||
|
||
#### What to Expect | ||
- **If Successful:** | ||
- Message: "Here are all the customers that match your search: (List of customers)." | ||
- **If Unsuccessful (No Matches Found):** | ||
- Message: "No customers match your search criteria." | ||
- **If Multiple Filters Are Used:** | ||
- Message: "Using multiple filters is not supported yet. Please use only one filter." | ||
- **If There is an Error:** | ||
- No Valid Flags Used: | ||
- Message: | ||
|
||
"filter: Searches for all customers whose specified field contains the given | ||
substring (case-insensitive) and displays the results in a numbered list. | ||
"filter: Searches for all customers whose specified field contains the given substring (case-insensitive) and displays the results in a numbered list. | ||
|
||
Parameters: `<FLAG>/ <SEARCH TERM>` | ||
|
||
Flags: `n/` (name), `p/` (phone), `e/` (email), `a/` (address), `j/` (job), `r/` (remarks) | ||
|
||
Example: `filter n/ Alice` | ||
|
||
This will find all customers whose names contain 'Alice'." | ||
Example: `filter n/ Alice p/ 91112222` | ||
|
||
This will find all customers whose names contain 'Alice' and has phone number '91112222'." | ||
|
||
- If Search Term Fails to Meet Requirement (i.e. Phone Number longer than 8 digits): | ||
- The system will display usage hints specific to the first invalid search term. | ||
|
||
|
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
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
75 changes: 75 additions & 0 deletions
75
src/main/java/seedu/address/model/person/predicates/CombinedPredicate.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,75 @@ | ||
package seedu.address.model.person.predicates; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.model.person.Person; | ||
|
||
/** | ||
* Represents a predicate that combines multiple other predicates using the AND operator. | ||
* This class allows you to test multiple conditions on a {@link Person} object in a way that | ||
* ensures all provided predicates must evaluate to true for the overall predicate to return true. | ||
* | ||
* <p>This is particularly useful for filtering when you need to apply multiple conditions | ||
* simultaneously, such as filtering by name, phone number, email, etc.</p> | ||
*/ | ||
public class CombinedPredicate implements Predicate<Person> { | ||
private final List<Predicate<Person>> predicates; | ||
|
||
/** | ||
* Constructs a {@code CombinedPredicate} with a list of predicates. | ||
* Each predicate in the list must evaluate to {@code true} for this combined predicate | ||
* to return {@code true} when tested. | ||
* | ||
* @param predicates the list of predicates to combine | ||
*/ | ||
public CombinedPredicate(List<Predicate<Person>> predicates) { | ||
this.predicates = Objects.requireNonNull(predicates, "Predicates list must not be null"); | ||
} | ||
|
||
/** | ||
* Tests the given {@code Person} object against all the combined predicates. | ||
* This method returns {@code true} if all predicates return {@code true} for the given person. | ||
* | ||
* @param person the {@code Person} to be tested | ||
* @return {@code true} if all predicates return {@code true}; {@code false} otherwise | ||
*/ | ||
@Override | ||
public boolean test(Person person) { | ||
for (Predicate<Person> predicate : predicates) { | ||
if (!predicate.test(person)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Checks whether this combined predicate is equal to another object. | ||
* Two {@code CombinedPredicate} objects are considered equal if their underlying | ||
* list of predicates are equal. This method is used for ease of testing without dealing with lambdas. | ||
* | ||
* @param other the object to compare to | ||
* @return {@code true} if this combined predicate is equal to the other object; {@code false} otherwise | ||
*/ | ||
@Override | ||
public boolean equals(Object other) { | ||
if (this == other) { | ||
return true; | ||
} | ||
|
||
if (!(other instanceof CombinedPredicate)) { | ||
return false; | ||
} | ||
|
||
CombinedPredicate that = (CombinedPredicate) other; | ||
return this.predicates.equals(that.predicates); | ||
} | ||
|
||
// This method is used for ease of testing without dealing with lambdas. | ||
@Override | ||
public int hashCode() { | ||
return predicates.hashCode(); | ||
} | ||
} |
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
Oops, something went wrong.