diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index edd516de..0f91bee1 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -6,28 +6,22 @@ public class CandidateValidator implements Predicate { @Override public boolean test(Candidate candidate) { - System.out.println("Validating candidate: " + candidate.getName()); - boolean valid = candidate.getAge() >= 35 + final String nationality = "Ukrainian"; + final int ageLimit = 35; + return candidate.getAge() >= ageLimit && candidate.isAllowedToVote() - && "Ukrainian".equals(candidate.getNationality()) + && nationality.equals(candidate.getNationality()) && isEligibleResidency(candidate.getPeriodsInUkr()); - if (valid) { - System.out.println("Candidate passed: " + candidate.getName()); - } else { - System.out.println("Candidate failed: " + candidate.getName()); - } - return valid; } private static boolean isEligibleResidency(String periodInUkr) { - + final int requiredPeriodInUkr = 10; if (periodInUkr == null || !periodInUkr.matches("\\d{4}-\\d{4}")) { return false; } String[] years = periodInUkr.split("-"); int startYear = Integer.parseInt((years[0])); int endYear = Integer.parseInt(years[1]); - return endYear - startYear >= 10; + return endYear - startYear >= requiredPeriodInUkr; } - } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 9ecc2b0a..fcaa1a42 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,11 +1,8 @@ package practice; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; -import java.util.OptionalDouble; -import java.util.stream.Collectors; import model.Candidate; import model.Cat; import model.Person; @@ -19,39 +16,28 @@ public class StreamPractice { * "Can't get min value from list: < Here is our input 'numbers' >" */ public int findMinEvenNumber(List numbers) { - List evenNumbers = numbers.stream() + return numbers.stream() .flatMap(s -> Arrays.stream(s.split(","))) .map(String::trim) .map(Integer::parseInt) .filter(n -> n % 2 == 0) - .collect(Collectors.toList()); - if (evenNumbers.isEmpty()) { - throw new RuntimeException("Can't get min value from list: " + numbers); - } - return evenNumbers.stream().min(Integer::compareTo).get(); + .min(Integer::compareTo) + .orElseThrow(() -> new RuntimeException("Can't get min value from list " + + numbers)); } - /** * Given a List of Integer numbers, * return the average of all odd numbers from the list or throw NoSuchElementException. * But before that subtract 1 from each element on an odd position (having the odd index). */ + public Double getOddNumsAverage(List numbers) { - List modifiedNumbers = new ArrayList<>(); - for (int i = 0; i < numbers.size(); i++) { - int num = numbers.get(i); - if (i % 2 != 0) { - num -= 1; - } - modifiedNumbers.add(num); - } - OptionalDouble average = modifiedNumbers.stream() - .filter(n -> n % 2 != 0) + return numbers.stream() + .map(n -> n % 2 == 0 ? n - 1 : n) .mapToInt(Integer::intValue) - .average(); - return average.orElseThrow(() -> new NoSuchElementException("No odd numbers found")); + .average() + .orElseThrow(() -> new NoSuchElementException("No odd numbers found")); } - /** * Given a List of `Person` instances (having `name`, `age` and `sex` fields), * for example, `Arrays.asList( new Person(«Victor», 16, Sex.MAN), @@ -60,13 +46,13 @@ public Double getOddNumsAverage(List numbers) { *

* Example: select men who can be recruited to army (from 18 to 27 years old inclusively). */ + public List selectMenByAge(List peopleList, int fromAge, int toAge) { return peopleList.stream() .filter(person -> person.getSex() == Person.Sex.MAN) .filter((person -> person.getAge() >= fromAge && person.getAge() <= toAge)) - .collect(Collectors.toList()); + .toList(); } - /** * Given a List of `Person` instances (having `name`, `age` and `sex` fields), * for example, `Arrays.asList( new Person(«Victor», 16, Sex.MAN), @@ -77,6 +63,7 @@ public List selectMenByAge(List peopleList, int fromAge, int toA * Example: select people of working age * (from 18 y.o. and to 60 y.o. for men and to 55 y.o. for women inclusively). */ + public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { return peopleList.stream() @@ -88,23 +75,22 @@ public List getWorkablePeople(int fromAge, int femaleToAge, return person.getAge() <= maleToAge; } }) - .collect(Collectors.toList()); + .toList(); } - /** * Given a List of `Person` instances (having `name`, `age`, `sex` and `cats` fields, * and each `Cat` having a `name` and `age`), * return the names of all cats whose owners are women from `femaleAge` years old inclusively. */ + public List getCatsNames(List peopleList, int femaleAge) { return peopleList.stream() .filter(person -> person.getSex() == Person.Sex.WOMAN) .filter(person -> person.getAge() >= femaleAge) .flatMap(person -> person.getCats().stream()) .map(Cat::getName) - .collect(Collectors.toList()); + .toList(); } - /** * Your help with a election is needed. Given list of candidates, where each element * has Candidate.class type. @@ -117,12 +103,13 @@ public List getCatsNames(List peopleList, int femaleAge) { * We want to reuse our validation in future, so let's write our own impl of Predicate * parametrized with Candidate in CandidateValidator. */ + public List validateCandidates(List candidates) { CandidateValidator candidateValidator = new CandidateValidator(); return candidates.stream() .filter(candidateValidator) .map(Candidate::getName) .sorted() - .collect(Collectors.toList()); + .toList(); } }