diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 30313a515..6f7fc48d2 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -7,15 +7,21 @@ public class CandidateValidator implements Predicate { private static final int MIN_CANDIDATE_AGE = 35; private static final int MIN_PERIOD_IN_UKRAINE = 10; + private static final String SPLIT_REGEX_HYPHEN = "-"; + private static final int STAR_YEAR_INDEX = 0; + private static final int END_YEAR_INDEX = 1; + private static final String NEEDED_NATIONALITY = "Ukrainian"; + @Override public boolean test(Candidate candidate) { if (candidate.isAllowedToVote() && candidate.getAge() >= MIN_CANDIDATE_AGE - && candidate.getNationality().equals("Ukrainian")) { + && candidate.getNationality().equals(NEEDED_NATIONALITY)) { + String[] inUkrYears = candidate.getPeriodsInUkr().split(SPLIT_REGEX_HYPHEN); int startYear = Integer.parseInt( - candidate.getPeriodsInUkr().split("-")[0]); + inUkrYears[STAR_YEAR_INDEX]); int endYear = Integer.parseInt( - candidate.getPeriodsInUkr().split("-")[1]); + inUkrYears[END_YEAR_INDEX]); return endYear - startYear >= MIN_PERIOD_IN_UKRAINE; } return false; diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index f081f4fca..c8a10578a 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -8,30 +8,30 @@ import model.Candidate; import model.Cat; import model.Person; +import model.Person.Sex; public class StreamPractice { private static final String NO_MIN_VALUE_EXCEPTION_MESSAGE = "Can't get min value from list: "; private static final String EMPTY_LIST_EXCEPTION_MESSAGE = "Empty List provided"; + private static final String SPLIT_REGEX_COMA = ","; private final Predicate isAbleToRunForPresident = new CandidateValidator(); public int findMinEvenNumber(List numbers) { return numbers.stream() - .map(e -> e.split(",")) + .map(stringNumber -> stringNumber.split(SPLIT_REGEX_COMA)) .flatMap(Arrays::stream) .map(Integer::parseInt) - .filter(e -> e % 2 == 0) + .filter(number -> number % 2 == 0) .sorted() .findFirst() .orElseThrow(() -> new RuntimeException(NO_MIN_VALUE_EXCEPTION_MESSAGE + numbers)); } - + public Double getOddNumsAverage(List numbers) { return IntStream.range(0, numbers.size()) .map(index -> { - if (index % 2 != 0) { - return numbers.get(index) - 1; - } - return numbers.get(index); + Integer numByIndex = numbers.get(index); + return index % 2 != 0 ? numByIndex - 1 : numByIndex; }) .filter(number -> number % 2 != 0) .average() @@ -40,21 +40,26 @@ public Double getOddNumsAverage(List numbers) { public List selectMenByAge(List peopleList, int fromAge, int toAge) { return peopleList.stream() - .filter(person -> person.getAge() >= fromAge && person.getAge() <= toAge) - .filter(person -> person.getSex().name().equals("MAN")) + .filter(person -> { + int personAge = person.getAge(); + ; + return personAge >= fromAge + && personAge <= toAge + && person.getSex().equals(Sex.MAN); + }) .toList(); } public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { - Predicate isWorkable = new Predicate() { - @Override - public boolean test(Person person) { - if (person.getSex().name().equals("MAN")) { - return person.getAge() >= fromAge && person.getAge() <= maleToAge; - } - return person.getAge() >= fromAge && person.getAge() <= femaleToAge; + Predicate isWorkable = person -> { + int personAge = person.getAge(); + if (person.getSex().equals(Sex.MAN)) { + return personAge >= fromAge + && personAge <= maleToAge; } + return personAge >= fromAge + && personAge <= femaleToAge; }; return peopleList.stream() .filter(isWorkable) @@ -62,7 +67,7 @@ public boolean test(Person person) { } public List getCatsNames(List peopleList, int femaleAge) { - Predicate isNeededCatOwner = person -> person.getSex().name().equals("WOMAN") + Predicate isNeededCatOwner = person -> person.getSex().equals(Sex.WOMAN) && person.getAge() >= femaleAge; return peopleList.stream() .filter(isNeededCatOwner)