From 7cbbcaf88f99dafe5a7bc14c36882d7da5c45b39 Mon Sep 17 00:00:00 2001 From: Anastasiia Beshleha Date: Wed, 8 Jan 2025 23:25:15 -0400 Subject: [PATCH 1/3] done --- .../java/practice/CandidateValidator.java | 27 +++++++++- src/main/java/practice/StreamPractice.java | 54 ++++++++++++++++--- 2 files changed, 72 insertions(+), 9 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 8d2e56c0e..96209d84f 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,5 +1,28 @@ package practice; -public class CandidateValidator { - //write your code here +import java.util.function.Predicate; +import model.Candidate; + +public class CandidateValidator implements Predicate { + + @Override + public boolean test(Candidate candidate) { + if (candidate.getAge() < 35) { + return false; + } + if (!(candidate.getNationality().equals("Ukrainian"))) { + return false; + } + if (!candidate.isAllowedToVote()) { + return false; + } + String[]periods = candidate.getPeriodsInUkr().split("-"); + if (periods.length != 2) { + return false; + } + int start = Integer.parseInt(periods[0]); + int finish = Integer.parseInt(periods[1]); + return (finish - start) >= 10; + + } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 57b1ca2e2..a82b431c8 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,17 @@ 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(number -> Arrays.stream(number.split(","))) + .map(String::trim) + .map(t -> Integer.parseInt(t)) + .filter(t -> t % 2 == 0) + .sorted() + .mapToInt(Integer::intValue) + .findFirst() + .orElseThrow(() -> new RuntimeException("Can't get min value from list:" + + numbers)); } /** @@ -23,7 +37,12 @@ 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()) + .map(i -> (i % 2 != 0) ? numbers.get(i) - 1 : numbers.get(i)) + .filter(n -> n % 2 != 0) + .average() + .orElseThrow(() -> new NoSuchElementException()); + } /** @@ -35,7 +54,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(p -> p.getSex() == Person.Sex.MAN + && p.getAge() > fromAge && p.getAge() <= toAge) + .collect(Collectors.toList()); } /** @@ -50,7 +72,13 @@ 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(p -> (p.getSex() == Person.Sex.MAN + && p.getAge() >= fromAge && p.getAge() <= maleToAge) + || (p.getSex() == Person.Sex.WOMAN && p.getAge() >= fromAge + && p.getAge() <= femaleToAge)) + .collect(Collectors.toList()); + } /** @@ -59,7 +87,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(p -> p.getSex() == Person.Sex.WOMAN + && p.getAge() >= femaleAge && p.getCats() != null) + .flatMap(p -> p.getCats().stream()) + .map(Cat::getName) + .collect(Collectors.toList()); } /** @@ -75,6 +109,12 @@ public List getCatsNames(List peopleList, int femaleAge) { * parametrized with Candidate in CandidateValidator. */ public List validateCandidates(List candidates) { - return Collections.emptyList(); + + CandidateValidator validator = new CandidateValidator(); + return candidates.stream() + .filter(validator) + .map(Candidate::getName) + .sorted() + .collect(Collectors.toList()); } } From 42905c8622fd36e4ebae5b5e02e5d69d864a07f1 Mon Sep 17 00:00:00 2001 From: Anastasiia Beshleha Date: Thu, 9 Jan 2025 21:17:55 -0400 Subject: [PATCH 2/3] made changes --- src/main/java/practice/CandidateValidator.java | 9 ++++++--- src/main/java/practice/StreamPractice.java | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 96209d84f..776971f9e 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -4,13 +4,16 @@ import model.Candidate; public class CandidateValidator implements Predicate { + private final String nationality = "Ukrainian"; + private final int age = 35; + private final int minYearsInUkraine = 10; @Override public boolean test(Candidate candidate) { - if (candidate.getAge() < 35) { + if (candidate.getAge() < age) { return false; } - if (!(candidate.getNationality().equals("Ukrainian"))) { + if (!(candidate.getNationality().equals(nationality))) { return false; } if (!candidate.isAllowedToVote()) { @@ -22,7 +25,7 @@ public boolean test(Candidate candidate) { } int start = Integer.parseInt(periods[0]); int finish = Integer.parseInt(periods[1]); - return (finish - start) >= 10; + return (finish - start) >= minYearsInUkraine; } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index a82b431c8..41214dbb6 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -27,7 +27,7 @@ public int findMinEvenNumber(List numbers) { .sorted() .mapToInt(Integer::intValue) .findFirst() - .orElseThrow(() -> new RuntimeException("Can't get min value from list:" + .orElseThrow(() -> new RuntimeException("Can't get min value from list: " + numbers)); } @@ -56,7 +56,7 @@ public Double getOddNumsAverage(List numbers) { public List selectMenByAge(List peopleList, int fromAge, int toAge) { return peopleList.stream() .filter(p -> p.getSex() == Person.Sex.MAN - && p.getAge() > fromAge && p.getAge() <= toAge) + && p.getAge() >= fromAge && p.getAge() <= toAge) .collect(Collectors.toList()); } From c99f9e628ca0655f198dc38af258f13b287191bf Mon Sep 17 00:00:00 2001 From: Anastasiia Beshleha Date: Fri, 10 Jan 2025 23:19:10 -0400 Subject: [PATCH 3/3] Improved solution according to latest request --- .../java/practice/CandidateValidator.java | 26 +++++++--------- src/main/java/practice/StreamPractice.java | 31 +++++++++++-------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 776971f9e..aba304ac5 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -4,28 +4,24 @@ import model.Candidate; public class CandidateValidator implements Predicate { - private final String nationality = "Ukrainian"; - private final int age = 35; - private final int minYearsInUkraine = 10; + private static final String NATIONALITY = "Ukrainian"; + private static final int AGE = 35; + private static final int MIN_YEARS_IN_UKRAINE = 10; + private static final String SEPARATOR = "-"; @Override public boolean test(Candidate candidate) { - if (candidate.getAge() < age) { + if (candidate.getAge() < AGE + || (!candidate.isAllowedToVote()) + || (!(candidate.getNationality().equals(NATIONALITY)))) { return false; } - if (!(candidate.getNationality().equals(nationality))) { - return false; - } - if (!candidate.isAllowedToVote()) { - return false; - } - String[]periods = candidate.getPeriodsInUkr().split("-"); + + String[] periods = candidate.getPeriodsInUkr().split(SEPARATOR); if (periods.length != 2) { return false; } - int start = Integer.parseInt(periods[0]); - int finish = Integer.parseInt(periods[1]); - return (finish - start) >= minYearsInUkraine; - + return (Integer.parseInt(periods[1]) - Integer.parseInt(periods[0])) + >= MIN_YEARS_IN_UKRAINE; } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 41214dbb6..e74265d88 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -3,7 +3,7 @@ import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; -import java.util.stream.Collectors; +import java.util.function.Predicate; import java.util.stream.IntStream; import model.Candidate; import model.Cat; @@ -21,12 +21,10 @@ public int findMinEvenNumber(List numbers) { return numbers.stream() .flatMap(number -> Arrays.stream(number.split(","))) - .map(String::trim) .map(t -> Integer.parseInt(t)) .filter(t -> t % 2 == 0) - .sorted() .mapToInt(Integer::intValue) - .findFirst() + .min() .orElseThrow(() -> new RuntimeException("Can't get min value from list: " + numbers)); } @@ -41,7 +39,7 @@ public Double getOddNumsAverage(List numbers) { .map(i -> (i % 2 != 0) ? numbers.get(i) - 1 : numbers.get(i)) .filter(n -> n % 2 != 0) .average() - .orElseThrow(() -> new NoSuchElementException()); + .orElseThrow(() -> new NoSuchElementException("No such element")); } @@ -57,7 +55,7 @@ public List selectMenByAge(List peopleList, int fromAge, int toA return peopleList.stream() .filter(p -> p.getSex() == Person.Sex.MAN && p.getAge() >= fromAge && p.getAge() <= toAge) - .collect(Collectors.toList()); + .toList(); } /** @@ -72,12 +70,19 @@ public List selectMenByAge(List peopleList, int fromAge, int toA */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { + + Predicate maleAgePredicate = p -> p.getSex() == Person.Sex.MAN + && p.getAge() >= fromAge + && p.getAge() <= maleToAge; + + Predicate femaleAgePredicate = p -> p.getSex() == Person.Sex.WOMAN + && p.getAge() >= fromAge + && p.getAge() <= femaleToAge; + + Predicate eligiblePersonPredicate = maleAgePredicate.or(femaleAgePredicate); return peopleList.stream() - .filter(p -> (p.getSex() == Person.Sex.MAN - && p.getAge() >= fromAge && p.getAge() <= maleToAge) - || (p.getSex() == Person.Sex.WOMAN && p.getAge() >= fromAge - && p.getAge() <= femaleToAge)) - .collect(Collectors.toList()); + .filter(eligiblePersonPredicate) + .toList(); } @@ -93,7 +98,7 @@ public List getCatsNames(List peopleList, int femaleAge) { && p.getAge() >= femaleAge && p.getCats() != null) .flatMap(p -> p.getCats().stream()) .map(Cat::getName) - .collect(Collectors.toList()); + .toList(); } /** @@ -115,6 +120,6 @@ public List validateCandidates(List candidates) { .filter(validator) .map(Candidate::getName) .sorted() - .collect(Collectors.toList()); + .toList(); } }