diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 8d2e56c0e..5120c3e65 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,5 +1,29 @@ package practice; -public class CandidateValidator { - //write your code here +import java.util.function.Predicate; +import model.Candidate; + +public class CandidateValidator implements Predicate { + private static final int MIN_CANDIDATE_AGE = 35; + private static final String CANDIDATE_NATIONALITY = "Ukrainian"; + private static final int LIVING_TIME_IN_COUNTRY = 10; + + // string has following view: "2002-2015" + private static Integer timeLivingInCountry(String fromTo) { + String[] parts = fromTo.split("-"); + if (parts.length == 2) { + return Integer.parseInt(parts[1]) - Integer.parseInt(parts[0]); + } + + throw new IllegalArgumentException("Invalid from-to: " + fromTo); + } + + @Override + public boolean test(Candidate candidate) { + return candidate.getAge() >= MIN_CANDIDATE_AGE + && candidate.getNationality().equals(CANDIDATE_NATIONALITY) + && candidate.isAllowedToVote() + && timeLivingInCountry(candidate.getPeriodsInUkr()) + >= LIVING_TIME_IN_COUNTRY; + } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 57b1ca2e2..77bc33eaa 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,8 +1,12 @@ package practice; -import java.util.Collections; +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; public class StreamPractice { @@ -14,7 +18,12 @@ public class StreamPractice { * "Can't get min value from list: < Here is our input 'numbers' >" */ public int findMinEvenNumber(List numbers) { - return 0; + return numbers.stream() + .flatMap(str -> Arrays.stream(str.split(","))) + .mapToInt(Integer::parseInt) + .filter(i -> i % 2 == 0) + .min() + .orElseThrow(() -> new RuntimeException("Can't get min value from list: " + numbers)); } /** @@ -23,7 +32,14 @@ public int findMinEvenNumber(List numbers) { * But before that subtract 1 from each element on an odd position (having the odd index). */ public Double getOddNumsAverage(List numbers) { - return 0D; + + return IntStream.range(0, numbers.size()) + .mapToDouble(index -> index % 2 == 0 + ? numbers.get(index).doubleValue() + : numbers.get(index).doubleValue() - 1) + .filter(i -> (int)i % 2 == 1 || (int)i % 2 == -1) + .average() + .orElseThrow(NoSuchElementException::new); } /** @@ -35,7 +51,10 @@ 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 Collections.emptyList(); + return peopleList.stream() + .filter(person -> Person.Sex.MAN.equals(person.getSex()) + && person.getAge() >= fromAge && person.getAge() <= toAge) + .collect(Collectors.toList()); } /** @@ -50,7 +69,11 @@ public List selectMenByAge(List peopleList, int fromAge, int toA */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { - return Collections.emptyList(); + return peopleList.stream() + .filter(person -> Person.Sex.MAN.equals(person.getSex()) + ? person.getAge() >= fromAge && person.getAge() <= maleToAge + : person.getAge() >= fromAge && person.getAge() <= femaleToAge) + .collect(Collectors.toList()); } /** @@ -59,7 +82,12 @@ public List getWorkablePeople(int fromAge, int femaleToAge, * return the names of all cats whose owners are women from `femaleAge` years old inclusively. */ public List getCatsNames(List peopleList, int femaleAge) { - return Collections.emptyList(); + return peopleList.stream() + .filter(person -> Person.Sex.WOMAN.equals(person.getSex()) + && person.getAge() >= femaleAge) + .flatMap(woman -> woman.getCats().stream()) + .map(Cat::getName) + .collect(Collectors.toList()); } /** @@ -75,6 +103,10 @@ public List getCatsNames(List peopleList, int femaleAge) { * parametrized with Candidate in CandidateValidator. */ public List validateCandidates(List candidates) { - return Collections.emptyList(); + return candidates.stream() + .filter(new CandidateValidator()) + .map(Candidate::getName) + .sorted() + .collect(Collectors.toList()); } }