From a8271073eb912012c1c286cec129cdb78fb47519 Mon Sep 17 00:00:00 2001 From: artificialLeo Date: Sun, 29 Oct 2023 14:03:03 +0200 Subject: [PATCH] added 3 predicates and new class, reworked existing person predicate --- .../java/practice/CandidateValidator.java | 19 ++++++++---- src/main/java/practice/PersonValidator.java | 26 +++++++++++++++++ src/main/java/practice/StreamPractice.java | 29 ++++++++++--------- 3 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 src/main/java/practice/PersonValidator.java diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 388925f3e..576d0e42f 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -4,12 +4,21 @@ import model.Candidate; public class CandidateValidator implements Predicate { + 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; } } diff --git a/src/main/java/practice/PersonValidator.java b/src/main/java/practice/PersonValidator.java new file mode 100644 index 000000000..013ae2080 --- /dev/null +++ b/src/main/java/practice/PersonValidator.java @@ -0,0 +1,26 @@ +package practice; + +import java.util.function.Predicate; +import model.Person; + +public class PersonValidator { + public static Predicate isManBetweenAges(int fromAge, int toAge) { + return p -> p.getSex() == Person.Sex.MAN + && p.getAge() >= fromAge + && p.getAge() <= toAge; + } + + public static Predicate selectPeopleByAge(int fromAge, int maleToAge, int femaleToAge) { + Predicate 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 isElderlyWoman(int femaleAge) { + return p -> p.getSex() == Person.Sex.WOMAN && p.getAge() >= femaleAge; + } +} + diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 631d4caa7..e9f103437 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -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; @@ -13,8 +14,8 @@ public class StreamPractice { public int findMinEvenNumber(List 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)); @@ -24,33 +25,35 @@ public Double getOddNumsAverage(List 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 selectMenByAge(List peopleList, int fromAge, int toAge) { + Predicate 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 getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { + Predicate 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 getCatsNames(List peopleList, int femaleAge) { + Predicate 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();