I've below requirement:-
Employee.java
public boolean isAdult(Integer age) {
if(age >= 18) {
return true;
}
return false;
}
Predicate.java
private Integer age;
Predicate<Integer> isAdult;
public PredicateAnotherClass(Integer age, Predicate<Integer> isAdult) {
this.age = age;
System.out.println("Test result is "+ isAdult(age));
}
public void testPredicate() {
System.out.println("Calling the method defined in the manager class");
}
Now My goal is to test whether the age which i pass to Predicate
is adult or not using the method defined in Employee
class , for which i am passing the method reference which i pass in the constructor of Predicate
class.
But i don't know how to call the method defined in Employee
class, below is my test class :-
public class PredicateTest {
public static void main(String[] args) {
PredicateManager predicateManager = new PredicateManager();
PredicateAnotherClass predicateAnotherClass = new PredicateAnotherClass(20, predicateManager::isAdult);
predicateAnotherClass.testPredicate();;
}
}
I am getting the compilation error in the System.out.println("Test result is "+ isAdult(age));
in the predicate
class.
Let me know how to resolve this issue. and if i need to provide any other information.
Predicate interface has method test()
. You should use this method in a following way:
isAdult.test(age)
This method evaluates this predicate on the given argument. It returns true if the input argument matches the predicate, otherwise false