Skip to content

Commit

Permalink
added 3 predicates and new class, reworked existing person predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
artificialLeo committed Oct 29, 2023
1 parent 9e6e0fd commit a827107
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
19 changes: 14 additions & 5 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
import model.Candidate;

public class CandidateValidator implements Predicate<Candidate> {
private static final int MINIMUM_AGE = 35;
private static final String UKRAINIAN_NATIONALITY = "Ukrainian";
private static final int MINIMUM_UKR_PERIOD = 10;

@Override
public boolean test(Candidate candidate) {
return candidate.getAge() >= 35
&& candidate.isAllowedToVote()
&& candidate.getNationality().equals("Ukrainian")
&& Integer.parseInt(candidate.getPeriodsInUkr().split("-")[1])
- Integer.parseInt(candidate.getPeriodsInUkr().split("-")[0]) >= 10;
String[] periods = candidate.getPeriodsInUkr().split("-");
int startYear = Integer.parseInt(periods[0]);
int endYear = Integer.parseInt(periods[1]);

boolean isMinimumAge = candidate.getAge() >= MINIMUM_AGE;
boolean isAllowedToVote = candidate.isAllowedToVote();
boolean isUkrainian = candidate.getNationality().equals(UKRAINIAN_NATIONALITY);
boolean hasMinimumUkrPeriod = endYear - startYear >= MINIMUM_UKR_PERIOD;

return isMinimumAge && isAllowedToVote && isUkrainian && hasMinimumUkrPeriod;
}
}
26 changes: 26 additions & 0 deletions src/main/java/practice/PersonValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package practice;

import java.util.function.Predicate;
import model.Person;

public class PersonValidator {
public static Predicate<Person> isManBetweenAges(int fromAge, int toAge) {
return p -> p.getSex() == Person.Sex.MAN
&& p.getAge() >= fromAge
&& p.getAge() <= toAge;
}

public static Predicate<Person> selectPeopleByAge(int fromAge, int maleToAge, int femaleToAge) {
Predicate<Person> isManBetweenAges = PersonValidator.isManBetweenAges(fromAge, maleToAge);

return p -> {
int ageLimit = (p.getSex() == Person.Sex.MAN) ? maleToAge : femaleToAge;
return p.getAge() >= fromAge && p.getAge() <= ageLimit;
};
}

public static Predicate<Person> isElderlyWoman(int femaleAge) {
return p -> p.getSex() == Person.Sex.WOMAN && p.getAge() >= femaleAge;
}
}

29 changes: 16 additions & 13 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Predicate;
import java.util.stream.IntStream;
import model.Candidate;
import model.Cat;
Expand All @@ -13,8 +14,8 @@ public class StreamPractice {
public int findMinEvenNumber(List<String> numbers) {
return numbers.stream()
.flatMapToInt(s -> Arrays.stream(s.split(","))
.mapToInt(Integer::parseInt)
.filter(num -> num % 2 == 0))
.mapToInt(Integer::parseInt)
.filter(num -> num % 2 == 0))
.min()
.orElseThrow(() ->
new RuntimeException("Can't get min value from list " + numbers));
Expand All @@ -24,33 +25,35 @@ public Double getOddNumsAverage(List<Integer> numbers) {
return IntStream.range(0, numbers.size())
.map(i -> (i % 2 == 1) ? numbers.get(i) - 1 : numbers.get(i))
.filter(num -> num % 2 != 0)
.boxed()
.toList().stream()
.mapToInt(Integer::intValue)
.average()
.orElseThrow(() -> new NoSuchElementException("List is empty"));
}

public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toAge) {
Predicate<Person> isManBetweenAges = PersonValidator
.isManBetweenAges(fromAge, toAge);

return peopleList.stream()
.filter(p -> p.getSex() == Person.Sex.MAN
&& p.getAge() >= fromAge
&& p.getAge() <= toAge)
.filter(isManBetweenAges)
.toList();
}

public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
int maleToAge, List<Person> peopleList) {
Predicate<Person> workablePersonPredicate = PersonValidator
.selectPeopleByAge(fromAge, maleToAge, femaleToAge);

return peopleList.stream()
.filter(p -> {
int ageLimit = (p.getSex() == Person.Sex.MAN) ? maleToAge : femaleToAge;
return p.getAge() >= fromAge && p.getAge() <= ageLimit;
}).toList();
.filter(workablePersonPredicate)
.toList();
}

public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
Predicate<Person> elderlyWomanPredicate = PersonValidator
.isElderlyWoman(femaleAge);

return peopleList.stream()
.filter(p -> p.getSex() == Person.Sex.WOMAN && p.getAge() >= femaleAge)
.filter(elderlyWomanPredicate)
.flatMap(p -> p.getCats().stream())
.map(Cat::getName)
.toList();
Expand Down

0 comments on commit a827107

Please sign in to comment.