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 #152 from itsme-zeix/add-filter-by-status
Add Filtering by Status
- Loading branch information
Showing
3 changed files
with
164 additions
and
3 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
44 changes: 44 additions & 0 deletions
44
src/main/java/seedu/address/model/person/predicates/StatusStartsWithSubstringPredicate.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,44 @@ | ||
package seedu.address.model.person.predicates; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.StringUtil; | ||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.model.person.Person; | ||
|
||
/** | ||
* Tests that a {@code Person}'s assigned {@code Status} starts with a specified String. | ||
*/ | ||
public class StatusStartsWithSubstringPredicate implements Predicate<Person> { | ||
protected final String substring; | ||
|
||
public StatusStartsWithSubstringPredicate(String substring) { | ||
this.substring = substring.toUpperCase(); | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
return StringUtil.startsWithSubstringIgnoreCase(person.getStatus().toParsableString(), substring); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof StatusStartsWithSubstringPredicate)) { | ||
return false; | ||
} | ||
|
||
StatusStartsWithSubstringPredicate otherStatusContainsSubstringPredicate = | ||
(StatusStartsWithSubstringPredicate) other; | ||
return substring.equals(otherStatusContainsSubstringPredicate.substring); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("substring", substring).toString(); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
...st/java/seedu/address/model/person/predicates/StatusStartsWithSubstringPredicateTest.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,111 @@ | ||
package seedu.address.model.person.predicates; | ||
|
||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.testutil.PersonBuilder; | ||
|
||
public class StatusStartsWithSubstringPredicateTest { | ||
|
||
@Test | ||
public void equals() { | ||
String firstPredicateSubstring = "first"; | ||
String secondPredicateSubstring = "first second"; | ||
|
||
StatusStartsWithSubstringPredicate firstPredicate = | ||
new StatusStartsWithSubstringPredicate(firstPredicateSubstring); | ||
StatusStartsWithSubstringPredicate secondPredicate = | ||
new StatusStartsWithSubstringPredicate(secondPredicateSubstring); | ||
|
||
// same object -> returns true | ||
assertTrue(firstPredicate.equals(firstPredicate)); | ||
|
||
// same values -> returns true | ||
StatusStartsWithSubstringPredicate firstPredicateCopy = | ||
new StatusStartsWithSubstringPredicate(firstPredicateSubstring); | ||
assertTrue(firstPredicate.equals(firstPredicateCopy)); | ||
|
||
// different types -> returns false | ||
assertFalse(firstPredicate.equals(1)); | ||
|
||
// null -> returns false | ||
assertFalse(firstPredicate.equals(null)); | ||
|
||
// different person -> returns false | ||
assertFalse(firstPredicate.equals(secondPredicate)); | ||
} | ||
|
||
@Test | ||
public void test_statusStartsWithSubstring_returnsTrue() { | ||
// Full status match | ||
StatusStartsWithSubstringPredicate predicate = new StatusStartsWithSubstringPredicate("urgent"); | ||
assertTrue(predicate.test(new PersonBuilder().withStatus("urgent").build())); | ||
|
||
// Partial status match | ||
predicate = new StatusStartsWithSubstringPredicate("ur"); | ||
assertTrue(predicate.test(new PersonBuilder().withStatus("urgent").build())); | ||
|
||
// Mixed-case substring | ||
predicate = new StatusStartsWithSubstringPredicate("uRgEnt"); | ||
assertTrue(predicate.test(new PersonBuilder().withStatus("URGENT").build())); | ||
} | ||
|
||
@Test | ||
public void test_emptySubstring_throwsException() { | ||
StatusStartsWithSubstringPredicate predicate = new StatusStartsWithSubstringPredicate(""); | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> predicate.test( | ||
new PersonBuilder().withStatus("URGENT").build())); | ||
} | ||
|
||
@Test | ||
public void test_statusDoesNotStartWithSubstring_returnsFalse() { | ||
// Non-matching substring | ||
StatusStartsWithSubstringPredicate predicate = new StatusStartsWithSubstringPredicate("URGENT"); | ||
assertFalse(predicate.test(new PersonBuilder().withStatus("Non_urgent").build())); | ||
|
||
// Substring matches name but does not match status | ||
predicate = new StatusStartsWithSubstringPredicate("Alice"); | ||
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("91234567") | ||
.withEmail("[email protected]").withAddress("Main Street").withRemark("Genius") | ||
.withJob("Doctor").withTier("GOLD").withStatus("URGENT").build())); | ||
|
||
// Substring matches phone but does not match status | ||
predicate = new StatusStartsWithSubstringPredicate("91234567"); | ||
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("91234567") | ||
.withEmail("[email protected]").withAddress("Main Street").withRemark("Genius") | ||
.withJob("Doctor").withTier("GOLD").withStatus("URGENT").build())); | ||
|
||
// Substring matches email but does not match status | ||
predicate = new StatusStartsWithSubstringPredicate("[email protected]"); | ||
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("91234567") | ||
.withEmail("[email protected]").withAddress("Main Street").withRemark("Genius") | ||
.withJob("Doctor").withTier("GOLD").withStatus("URGENT").build())); | ||
|
||
// Substring matches address but does not match status | ||
predicate = new StatusStartsWithSubstringPredicate("Main Street"); | ||
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("91234567") | ||
.withEmail("[email protected]").withAddress("Main Street").withRemark("Genius") | ||
.withJob("Doctor").withTier("GOLD").withStatus("URGENT").build())); | ||
|
||
// Substring matches remark but does not match status | ||
predicate = new StatusStartsWithSubstringPredicate("Doctor"); | ||
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("91234567") | ||
.withEmail("[email protected]").withAddress("Main Street").withRemark("Genius") | ||
.withJob("Doctor").withTier("GOLD").withStatus("URGENT").build())); | ||
} | ||
|
||
@Test | ||
public void toStringMethod() { | ||
String substring = "testing substring"; | ||
StatusStartsWithSubstringPredicate predicate = new StatusStartsWithSubstringPredicate(substring); | ||
|
||
String expected = StatusStartsWithSubstringPredicate.class.getCanonicalName() | ||
+ "{substring=" + substring.toUpperCase() + "}"; | ||
assertEquals(expected, predicate.toString()); | ||
} | ||
} |