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 #95 from itsme-zeix/add-filtering-by-income
Implement filtering by income
- Loading branch information
Showing
10 changed files
with
480 additions
and
61 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
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
84 changes: 84 additions & 0 deletions
84
src/main/java/seedu/address/model/person/predicates/IncomeComparisonPredicate.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,84 @@ | ||
package seedu.address.model.person.predicates; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.util.IncomeComparisonOperator; | ||
|
||
/** | ||
* Predicate that compares a {@code Person}'s income against a threshold using a specified comparison operator. | ||
*/ | ||
public class IncomeComparisonPredicate implements Predicate<Person> { | ||
private final int incomeThreshold; | ||
private final IncomeComparisonOperator incomeComparisonOperator; | ||
|
||
/** | ||
* Constructs an {@code IncomeComparisonPredicate}. | ||
* | ||
* @param incomeComparisonOperator The operator used to compare the person's income with the threshold. | ||
* @param incomeThreshold The threshold income to compare against. | ||
*/ | ||
public IncomeComparisonPredicate(IncomeComparisonOperator incomeComparisonOperator, int incomeThreshold) { | ||
requireNonNull(incomeComparisonOperator); | ||
checkPositiveIncomeThreshold(incomeThreshold); | ||
this.incomeThreshold = incomeThreshold; | ||
this.incomeComparisonOperator = incomeComparisonOperator; | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
int personIncome = person.getIncome().value; | ||
|
||
switch (incomeComparisonOperator.comparisonOperator) { | ||
case "=": | ||
return personIncome == incomeThreshold; | ||
case ">": | ||
return personIncome > incomeThreshold; | ||
case "<": | ||
return personIncome < incomeThreshold; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof IncomeComparisonPredicate)) { | ||
return false; | ||
} | ||
|
||
IncomeComparisonPredicate otherIncomeComparisonPredicate = | ||
(IncomeComparisonPredicate) other; | ||
return incomeThreshold == otherIncomeComparisonPredicate.incomeThreshold | ||
&& incomeComparisonOperator.equals(otherIncomeComparisonPredicate.incomeComparisonOperator); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("incomeThreshold", incomeThreshold) | ||
.add("incomeComparisonOperator", incomeComparisonOperator) | ||
.toString(); | ||
} | ||
|
||
/** | ||
* Ensures that the income threshold is positive. | ||
* | ||
* @param incomeThreshold The threshold to check. | ||
* @throws IllegalArgumentException if {@code incomeThreshold} is not greater than 1. | ||
*/ | ||
private void checkPositiveIncomeThreshold(int incomeThreshold) { | ||
if (incomeThreshold < 0) { | ||
throw new IllegalArgumentException("Income threshold cannot be less than 0."); | ||
} | ||
} | ||
} | ||
|
40 changes: 0 additions & 40 deletions
40
src/main/java/seedu/address/model/person/predicates/IncomeContainsSubstringPredicate.java
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
src/main/java/seedu/address/model/util/IncomeComparisonOperator.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,61 @@ | ||
package seedu.address.model.util; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.AppUtil.checkArgument; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Represents a comparison operator used in income comparisons. | ||
* The operator can only be one of '=', '>', or '<'. | ||
* Guarantees: comparisonOperator is validated upon creation. | ||
*/ | ||
public class IncomeComparisonOperator { | ||
public static final String MESSAGE_CONSTRAINTS = "Income comparison operators can only be '=', '>' or '<'"; | ||
|
||
public final String comparisonOperator; | ||
|
||
/** | ||
* Constructs a {@code IncomeComparisonOperator}. | ||
* | ||
* @param comparisonOperator A comparisonOperator | ||
*/ | ||
public IncomeComparisonOperator(String comparisonOperator) { | ||
requireNonNull(comparisonOperator); | ||
checkArgument(isValidComparisonOperator(comparisonOperator), MESSAGE_CONSTRAINTS); | ||
this.comparisonOperator = comparisonOperator; | ||
} | ||
|
||
/** | ||
* Returns true if a given string is a valid comparison operator. | ||
*/ | ||
public static boolean isValidComparisonOperator(String test) { | ||
if (test.trim().isEmpty()) { | ||
return false; | ||
} | ||
|
||
return (test.equals("=") || test.equals(">") || test.equals("<")); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (this == other) { | ||
return true; | ||
} | ||
if (other instanceof IncomeComparisonOperator) { | ||
IncomeComparisonOperator otherIncomeComparisonOperator = (IncomeComparisonOperator) other; | ||
return comparisonOperator.equals(otherIncomeComparisonOperator.comparisonOperator); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(comparisonOperator); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return comparisonOperator; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -15,11 +15,13 @@ | |
import seedu.address.model.person.predicates.AddressContainsSubstringPredicate; | ||
import seedu.address.model.person.predicates.CombinedPredicate; | ||
import seedu.address.model.person.predicates.EmailContainsSubstringPredicate; | ||
import seedu.address.model.person.predicates.IncomeComparisonPredicate; | ||
import seedu.address.model.person.predicates.JobContainsSubstringPredicate; | ||
import seedu.address.model.person.predicates.NameContainsSubstringPredicate; | ||
import seedu.address.model.person.predicates.PhoneContainsSubstringPredicate; | ||
import seedu.address.model.person.predicates.RemarkContainsSubstringPredicate; | ||
import seedu.address.model.person.predicates.TierStartsWithSubstringPredicate; | ||
import seedu.address.model.util.IncomeComparisonOperator; | ||
|
||
public class FilterCommandParserTest { | ||
|
||
|
@@ -58,13 +60,24 @@ public void parse_emailFlag_returnsEmailFilterCommand() { | |
assertParseSuccess(parser, " e/ [email protected]", expectedFilterCommand); | ||
} | ||
|
||
@Test | ||
public void parse_incomeFlag_returnsIncomeFilterCommand() { | ||
List<Predicate<Person>> expectedPredicates = new ArrayList<>(); | ||
expectedPredicates.add(new EmailContainsSubstringPredicate("[email protected]")); | ||
FilterCommand expectedFilterCommand = new FilterCommand(new CombinedPredicate(expectedPredicates)); | ||
|
||
assertParseSuccess(parser, " e/ [email protected]", expectedFilterCommand); | ||
} | ||
|
||
@Test | ||
public void parse_jobFlag_returnsRemarkFilterCommand() { | ||
List<Predicate<Person>> expectedPredicates = new ArrayList<>(); | ||
expectedPredicates.add(new JobContainsSubstringPredicate("Software Engineer")); | ||
IncomeComparisonOperator operator = new IncomeComparisonOperator(">"); | ||
expectedPredicates.add(new IncomeComparisonPredicate(operator, 5000)); | ||
|
||
FilterCommand expectedFilterCommand = new FilterCommand(new CombinedPredicate(expectedPredicates)); | ||
|
||
assertParseSuccess(parser, " j/ Software Engineer", expectedFilterCommand); | ||
assertParseSuccess(parser, " i/ >5000", expectedFilterCommand); | ||
} | ||
|
||
@Test | ||
|
@@ -112,13 +125,15 @@ public void parse_validMultipleArgs_returnsFilterCommand() { | |
expectedPredicates.add(new EmailContainsSubstringPredicate("[email protected]")); | ||
expectedPredicates.add(new AddressContainsSubstringPredicate("Block 123")); | ||
expectedPredicates.add(new JobContainsSubstringPredicate("Software Engineer")); | ||
IncomeComparisonOperator operator = new IncomeComparisonOperator(">"); | ||
expectedPredicates.add(new IncomeComparisonPredicate(operator, 5000)); | ||
expectedPredicates.add(new RemarkContainsSubstringPredicate("is a celebrity")); | ||
expectedPredicates.add(new TierStartsWithSubstringPredicate("GOLD")); | ||
|
||
FilterCommand expectedFilterCommand = new FilterCommand(new CombinedPredicate(expectedPredicates)); | ||
|
||
assertParseSuccess(parser, " n/ Alice p/ 91112222 e/ [email protected] a/ Block 123 " | ||
+ "j/ Software Engineer r/ is a celebrity t/ gold", expectedFilterCommand); | ||
+ "j/ Software Engineer i/ >5000 r/ is a celebrity t/ gold", expectedFilterCommand); | ||
} | ||
|
||
@Test | ||
|
Oops, something went wrong.