diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index b4c2a576b..06e49b8c4 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -7,6 +7,7 @@ public class CandidateValidator implements Predicate { private static final String NATIONALITY = "Ukrainian"; private static final int MIN_AGE = 35; private static final int MIN_PERIOD_IN_UKRAINE = 10; + private static final String PERIOD_SEPARATOR = "-"; @Override public boolean test(Candidate candidate) { @@ -17,7 +18,7 @@ && periodMoreThanTen(candidate.getPeriodsInUkr()) } private boolean periodMoreThanTen(String periodsInUkr) { - String[] period = periodsInUkr.split("-"); + String[] period = periodsInUkr.split(PERIOD_SEPARATOR); int start = Integer.parseInt(period[0]); int end = Integer.parseInt(period[1]); return end - start >= MIN_PERIOD_IN_UKRAINE; diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index d71f86e91..0dde38536 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -2,7 +2,7 @@ import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; +import java.util.function.Predicate; import java.util.stream.IntStream; import model.Candidate; import model.Cat; @@ -54,7 +54,7 @@ public List selectMenByAge(List peopleList, int fromAge, int toA .filter(person -> person.getSex() == Person.Sex.MAN && person.getAge() >= fromAge && person.getAge() <= toAge) - .collect(Collectors.toList()); + .toList(); } /** @@ -69,14 +69,18 @@ public List selectMenByAge(List peopleList, int fromAge, int toA */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { + + Predicate isManWithAgeRange = person -> person.getSex() == Person.Sex.MAN + && person.getAge() >= fromAge + && person.getAge() <= maleToAge; + + Predicate isWomanWithAgeRange = person -> person.getSex() == Person.Sex.WOMAN + && person.getAge() >= fromAge + && person.getAge() <= femaleToAge; + return peopleList.stream() - .filter(person -> Person.Sex.MAN.equals(person.getSex()) - && person.getAge() >= fromAge - && person.getAge() <= maleToAge - || Person.Sex.WOMAN.equals(person.getSex()) - && person.getAge() >= fromAge - && person.getAge() <= femaleToAge) - .collect(Collectors.toList()); + .filter(isManWithAgeRange.or(isWomanWithAgeRange)) + .toList(); } /** @@ -86,12 +90,12 @@ public List getWorkablePeople(int fromAge, int femaleToAge, */ public List getCatsNames(List peopleList, int femaleAge) { return peopleList.stream() - .filter(person -> Person.Sex.WOMAN.equals(person.getSex()) + .filter(person -> person.getSex() == Person.Sex.WOMAN && person.getAge() >= femaleAge && person.getCats() != null) .flatMap(person -> person.getCats().stream()) .map(Cat::getName) - .collect(Collectors.toList()); + .toList(); } /** @@ -111,6 +115,7 @@ public List validateCandidates(List candidates) { .filter(new CandidateValidator()) .map(Candidate::getName) .sorted() - .collect(Collectors.toList()); + .toList(); } + }