diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 562669c1e..12257958a 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,23 +1,28 @@ package practice; -import model.Candidate; import java.util.Arrays; import java.util.function.Predicate; +import model.Candidate; public class CandidateValidator implements Predicate { + private static final int MIN_AGE = 35; + private static final String REQUIRED_NATIONALITY = "Ukrainian"; + private static final String PERIOD_SEPARATOR = ","; + private static final String YEAR_SEPARATOR = "-"; + private static final int MIN_YEARS_IN_UKRAINE = 10; @Override public boolean test(Candidate candidate) { - return candidate.getAge() >= 35 + return candidate.getAge() >= MIN_AGE && candidate.isAllowedToVote() - && "Ukrainian".equals(candidate.getNationality()) + && REQUIRED_NATIONALITY.equals(candidate.getNationality()) && Arrays.stream(candidate.getPeriodsInUkr() - .split(",")) + .split(PERIOD_SEPARATOR)) .anyMatch(period -> { - String[] years = period.split("-"); + String[] years = period.split(YEAR_SEPARATOR); int startYear = Integer.parseInt(years[0]); int endYear = Integer.parseInt(years[1]); - return endYear - startYear >= 10; + return endYear - startYear >= MIN_YEARS_IN_UKRAINE; }); } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index d633c48ec..6b23e53fc 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,17 +1,18 @@ package practice; +import java.util.Arrays; import java.util.List; +import java.util.NoSuchElementException; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import model.Candidate; import model.Cat; import model.Person; -import java.util.NoSuchElementException; -import java.util.stream.IntStream; -import java.util.stream.Collectors; public class StreamPractice { public int findMinEvenNumber(List numbers) { return numbers.stream() - .flatMap(s -> List.of(s.split(",")).stream()) + .flatMap(s -> Arrays.stream(s.split(","))) // Use Arrays.stream directly .map(Integer::parseInt) .filter(num -> num % 2 == 0) .min(Integer::compare) @@ -40,19 +41,14 @@ public List selectMenByAge(List peopleList, int fromAge, int toA .collect(Collectors.toList()); } - public List getWorkablePeople(int fromAge, int femaleToAge, - int maleToAge, List peopleList) { + public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { return peopleList.stream() - .filter(person -> { - if (person.getSex() == Person.Sex.MAN) { - return person.getAge() >= fromAge - && person.getAge() <= maleToAge; - } else if (person.getSex() == Person.Sex.WOMAN) { - return person.getAge() >= fromAge - && person.getAge() <= femaleToAge; - } - return false; - }) + .filter(person -> (person.getSex() == Person.Sex.MAN + && person.getAge() >= fromAge + && person.getAge() <= maleToAge) + || (person.getSex() == Person.Sex.WOMAN + && person.getAge() >= fromAge + && person.getAge() <= femaleToAge)) .collect(Collectors.toList()); } @@ -66,10 +62,8 @@ public List getCatsNames(List peopleList, int femaleAge) { } public List validateCandidates(List candidates) { - CandidateValidator validator = new CandidateValidator(); - return candidates.stream() - .filter(validator) + .filter(new CandidateValidator()) .map(Candidate::getName) .sorted() .collect(Collectors.toList());