forked from nus-cs2103-AY2324S1/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.
Merge pull request nus-cs2103-AY2324S1#107 from FerdiHS/add-price-and…
…-tags-predicate Add predicate for Price and Tags for Filtering Properties
- Loading branch information
Showing
4 changed files
with
275 additions
and
0 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
54 changes: 54 additions & 0 deletions
54
src/main/java/seedu/address/model/property/PriceAndTagsInRangePredicate.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,54 @@ | ||
package seedu.address.model.property; | ||
|
||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Tests that a {@code Property}'s {@code Price} and/or {@code Tags} are in range of the specified price and/or tags. | ||
*/ | ||
public class PriceAndTagsInRangePredicate implements Predicate<Property> { | ||
private final Price price; | ||
private final Set<Tag> tags; | ||
|
||
/** | ||
* Constructs a {@code PriceAndTagsInRangePredicate}. | ||
* | ||
* @param price the specified price if any | ||
* @param tags the specified tags if any | ||
*/ | ||
public PriceAndTagsInRangePredicate(Price price, Set<Tag> tags) { | ||
this.price = price; | ||
this.tags = tags; | ||
} | ||
|
||
@Override | ||
public boolean test(Property property) { | ||
return property.getTags().containsAll(tags) && property.getPrice().isInRangePrice(price); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof PriceAndTagsInRangePredicate)) { | ||
return false; | ||
} | ||
|
||
PriceAndTagsInRangePredicate otherBudgetAndTagsInRangePredicate = (PriceAndTagsInRangePredicate) other; | ||
return price.equals(otherBudgetAndTagsInRangePredicate.price) | ||
&& tags.equals(otherBudgetAndTagsInRangePredicate.tags); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("price", price) | ||
.add("tags", tags).toString(); | ||
} | ||
} |
195 changes: 195 additions & 0 deletions
195
src/test/java/seedu/address/model/property/PriceAndTagsInRangePredicateTest.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,195 @@ | ||
package seedu.address.model.property; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.model.tag.Tag; | ||
import seedu.address.testutil.PropertyBuilder; | ||
|
||
public class PriceAndTagsInRangePredicateTest { | ||
@Test | ||
public void equals() { | ||
Price firstPrice = new Price("1000000"); | ||
Price secondPrice = new Price("2000000"); | ||
|
||
Tag firstTag = new Tag("sunny"); | ||
Tag secondTag = new Tag("bright"); | ||
|
||
Set<Tag> firstTags = new HashSet<>(); | ||
Set<Tag> secondTags = new HashSet<>(); | ||
|
||
firstTags.add(firstTag); | ||
secondTags.add(secondTag); | ||
|
||
PriceAndTagsInRangePredicate firstPredicate = new PriceAndTagsInRangePredicate(firstPrice, firstTags); | ||
PriceAndTagsInRangePredicate secondPredicate = new PriceAndTagsInRangePredicate(secondPrice, firstTags); | ||
PriceAndTagsInRangePredicate thirdPredicate = new PriceAndTagsInRangePredicate(firstPrice, secondTags); | ||
PriceAndTagsInRangePredicate fourthPredicate = new PriceAndTagsInRangePredicate(secondPrice, secondTags); | ||
|
||
// same object -> returns true | ||
assertTrue(firstPredicate.equals(firstPredicate)); | ||
|
||
// same values -> returns true | ||
PriceAndTagsInRangePredicate firstPredicateCopy = new PriceAndTagsInRangePredicate(firstPrice, firstTags); | ||
assertTrue(firstPredicate.equals(firstPredicateCopy)); | ||
|
||
// different types -> returns false | ||
assertFalse(firstPredicate.equals(1)); | ||
|
||
// null -> returns false | ||
assertFalse(firstPredicate.equals(null)); | ||
|
||
// different budget -> returns false | ||
assertFalse(firstPredicate.equals(secondPredicate)); | ||
|
||
// different tags -> return false | ||
assertFalse(firstPredicate.equals(thirdPredicate)); | ||
|
||
// different tags and budget -> return false | ||
assertFalse(firstPredicate.equals(fourthPredicate)); | ||
} | ||
|
||
@Test | ||
public void test_budgetAndTagsInRangeReturnTrue() { | ||
String lowPriceString = "100000"; | ||
String highPriceString = "1000000000"; | ||
|
||
Price highPrice = new Price(highPriceString); | ||
|
||
String firstTagString = "sunny"; | ||
String secondTagString = "bright"; | ||
|
||
Tag firstTag = new Tag(firstTagString); | ||
Tag secondTag = new Tag(secondTagString); | ||
|
||
Set<Tag> emptyTags = new HashSet<>(); | ||
Set<Tag> singleTags = new HashSet<>(); | ||
Set<Tag> someTags = new HashSet<>(); | ||
|
||
singleTags.add(firstTag); | ||
someTags.add(firstTag); | ||
someTags.add(secondTag); | ||
|
||
// Same price with empty tag | ||
PriceAndTagsInRangePredicate predicate = new PriceAndTagsInRangePredicate(highPrice, emptyTags); | ||
assertTrue(predicate.test(new PropertyBuilder().withPrice(highPriceString).build())); | ||
assertTrue(predicate.test(new PropertyBuilder() | ||
.withPrice(highPriceString).withTags(firstTagString).build())); | ||
|
||
// Higher price with empty tag | ||
assertTrue(predicate.test(new PropertyBuilder().withPrice(lowPriceString).build())); | ||
assertTrue(predicate.test(new PropertyBuilder().withPrice(lowPriceString).withTags(firstTagString).build())); | ||
|
||
|
||
// No price with single tag | ||
predicate = new PriceAndTagsInRangePredicate(null, singleTags); | ||
assertTrue(predicate.test(new PropertyBuilder().withTags(firstTagString).build())); | ||
assertTrue(predicate.test(new PropertyBuilder().withTags(firstTagString, secondTagString).build())); | ||
|
||
// Same price with single tag | ||
predicate = new PriceAndTagsInRangePredicate(highPrice, singleTags); | ||
assertTrue(predicate.test(new PropertyBuilder() | ||
.withPrice(highPriceString).withTags(firstTagString).build())); | ||
assertTrue(predicate.test(new PropertyBuilder() | ||
.withPrice(highPriceString).withTags(firstTagString, secondTagString).build())); | ||
|
||
// Higher price with single tag | ||
assertTrue(predicate.test(new PropertyBuilder().withPrice(lowPriceString).withTags(firstTagString).build())); | ||
assertTrue(predicate.test(new PropertyBuilder() | ||
.withPrice(lowPriceString).withTags(firstTagString, secondTagString).build())); | ||
|
||
// No price with multiple tags | ||
predicate = new PriceAndTagsInRangePredicate(null, someTags); | ||
assertTrue(predicate.test(new PropertyBuilder().withTags(firstTagString, secondTagString).build())); | ||
|
||
// Same price with multiple tags | ||
predicate = new PriceAndTagsInRangePredicate(highPrice, someTags); | ||
assertTrue(predicate.test(new PropertyBuilder() | ||
.withPrice(highPriceString).withTags(firstTagString, secondTagString).build())); | ||
|
||
// Higher price with multiple tags | ||
assertTrue(predicate.test(new PropertyBuilder() | ||
.withPrice(lowPriceString).withTags(firstTagString, secondTagString).build())); | ||
} | ||
|
||
@Test | ||
public void test_budgetAndTagsInRangeReturnFalse() { | ||
String lowPriceString = "100000"; | ||
String highPriceString = "1000000000"; | ||
|
||
Price lowPrice = new Price(lowPriceString); | ||
|
||
String firstTagString = "sunny"; | ||
String secondTagString = "bright"; | ||
String thirdTagString = "square"; | ||
|
||
Tag firstTag = new Tag(firstTagString); | ||
Tag secondTag = new Tag(secondTagString); | ||
|
||
Set<Tag> emptyTags = new HashSet<>(); | ||
Set<Tag> singleTags = new HashSet<>(); | ||
Set<Tag> someTags = new HashSet<>(); | ||
|
||
singleTags.add(firstTag); | ||
someTags.add(firstTag); | ||
someTags.add(secondTag); | ||
|
||
// Lower price with empty tag | ||
PriceAndTagsInRangePredicate predicate = new PriceAndTagsInRangePredicate(lowPrice, emptyTags); | ||
assertFalse(predicate.test(new PropertyBuilder().withPrice(highPriceString).build())); | ||
assertFalse(predicate.test(new PropertyBuilder() | ||
.withPrice(highPriceString).withTags(firstTagString).build())); | ||
|
||
// No price with single tag | ||
predicate = new PriceAndTagsInRangePredicate(null, singleTags); | ||
assertFalse(predicate.test(new PropertyBuilder().withTags(secondTagString).build())); | ||
|
||
// Same price with single tag | ||
predicate = new PriceAndTagsInRangePredicate(lowPrice, singleTags); | ||
assertFalse(predicate.test(new PropertyBuilder().withPrice(lowPriceString).build())); | ||
assertFalse(predicate.test(new PropertyBuilder() | ||
.withPrice(lowPriceString).withTags(secondTagString).build())); | ||
|
||
// Lower price with single tag | ||
assertFalse(predicate.test(new PropertyBuilder().withPrice(highPriceString).build())); | ||
assertFalse(predicate.test(new PropertyBuilder() | ||
.withPrice(highPriceString).withTags(firstTagString).build())); | ||
assertFalse(predicate.test(new PropertyBuilder() | ||
.withPrice(highPriceString).withTags(secondTagString).build())); | ||
assertFalse(predicate.test(new PropertyBuilder() | ||
.withPrice(highPriceString).withTags(firstTagString, secondTagString).build())); | ||
|
||
// No price with multiple tags | ||
predicate = new PriceAndTagsInRangePredicate(null, someTags); | ||
assertFalse(predicate.test(new PropertyBuilder().build())); | ||
assertFalse(predicate.test(new PropertyBuilder().withTags(firstTagString).build())); | ||
assertFalse(predicate.test(new PropertyBuilder() | ||
.withTags(firstTagString, thirdTagString).build())); | ||
|
||
// Same price with multiple tags | ||
predicate = new PriceAndTagsInRangePredicate(lowPrice, someTags); | ||
assertFalse(predicate.test(new PropertyBuilder().withPrice(lowPriceString).build())); | ||
assertFalse(predicate.test(new PropertyBuilder() | ||
.withPrice(lowPriceString).withTags(firstTagString).build())); | ||
assertFalse(predicate.test(new PropertyBuilder() | ||
.withPrice(lowPriceString).withTags(thirdTagString).build())); | ||
assertFalse(predicate.test(new PropertyBuilder() | ||
.withPrice(lowPriceString).withTags(firstTagString, thirdTagString).build())); | ||
|
||
// Lower price with multiple tags | ||
predicate = new PriceAndTagsInRangePredicate(lowPrice, someTags); | ||
assertFalse(predicate.test(new PropertyBuilder().withPrice(highPriceString).build())); | ||
assertFalse(predicate.test(new PropertyBuilder() | ||
.withPrice(highPriceString).withTags(firstTagString).build())); | ||
assertFalse(predicate.test(new PropertyBuilder() | ||
.withPrice(highPriceString).withTags(firstTagString, secondTagString).build())); | ||
assertFalse(predicate.test(new PropertyBuilder() | ||
.withPrice(highPriceString).withTags(firstTagString, thirdTagString).build())); | ||
|
||
} | ||
} |
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