diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 8d2e56c0e..bcdf846a6 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,5 +1,26 @@ package practice; -public class CandidateValidator { - //write your code here +import java.util.function.Predicate; +import model.Candidate; +/*The requirements are: person should be older than 35 years, should be allowed to vote, + * have nationality - 'Ukrainian' + * and live in Ukraine for 10 years. For the last requirement use field periodsInUkr, + * which has following view: "2002-2015" + * We want to reuse our validation in future, so let's write our own impl of Predicate + * parametrized with Candidate in CandidateValidator. + */ + +public class CandidateValidator implements Predicate { + public static final int MIN_AGE = 35; + public static final String NATIONALITY = "Ukrainian"; + public static final int MIN_PERIOD = 10; + + @Override + public boolean test(Candidate candidate) { + String[] period = candidate.getPeriodsInUkr().split("-"); + int periodInt = Integer.parseInt(period[1]) - Integer.parseInt(period[0]); + return candidate.getAge() >= MIN_AGE + && candidate.getNationality().equals(NATIONALITY) + && periodInt >= MIN_PERIOD && candidate.isAllowedToVote(); + } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 57b1ca2e2..a89c60323 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,15 @@ public class StreamPractice { * "Can't get min value from list: < Here is our input 'numbers' >" */ public int findMinEvenNumber(List numbers) { - return 0; + if (numbers.isEmpty()) { + throw new RuntimeException("Can't get min value from list" + numbers); + } + return numbers.stream() + .flatMap(s -> Arrays.stream(s.split(","))) + .mapToInt(Integer::parseInt) + .filter(i -> i % 2 == 0) + .min() + .orElseThrow(); } /** @@ -23,7 +35,24 @@ 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; + IntStream.range(0, numbers.size()) + .filter(x -> x % 2 == 1) + .forEach(i -> numbers.set(i, numbers.get(i) - 1)); + long count = numbers.stream() + .filter(i -> i % 2 != 0) + .count(); + + if (count == 0) { + throw new NoSuchElementException(); + } + + long sum = numbers.stream() + .mapToInt(i -> i) + .filter(i -> i % 2 != 0) + .sum(); + + return (double) sum / count; + } /** @@ -35,7 +64,11 @@ 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(s -> s.getSex().equals(Person.Sex.MAN)) + .filter(s -> s.getAge() >= fromAge) + .filter(s -> s.getAge() <= toAge) + .collect(Collectors.toList()); } /** @@ -50,7 +83,14 @@ 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(s -> (s.getSex().equals(Person.Sex.MAN) + && s.getAge() <= maleToAge + && s.getAge() >= fromAge) + || (s.getSex().equals(Person.Sex.WOMAN) + && s.getAge() <= femaleToAge + && s.getAge() >= fromAge)) + .collect(Collectors.toList()); } /** @@ -59,7 +99,13 @@ 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(s -> s.getSex().equals(Person.Sex.WOMAN) + && s.getAge() >= femaleAge + && !s.getCats().isEmpty()) + .flatMap(person -> person.getCats().stream()) + .map(Cat::getName) + .collect(Collectors.toList()); } /** @@ -75,6 +121,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()); } }