From 6453d277e57daf76aaea03d78612ce9502c0b336 Mon Sep 17 00:00:00 2001 From: Mich Hmk Date: Thu, 9 Jan 2025 17:37:25 +0200 Subject: [PATCH 1/3] Practice Completed --- .../java/practice/CandidateValidator.java | 21 +++++- src/main/java/practice/StreamPractice.java | 64 +++++++++++++++++-- 2 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 8d2e56c0e..3e4f3547f 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,5 +1,22 @@ 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 MINIMUM_AGE = 35; + private static final int MIN_YEARS_IN_UKRAINE = 10; + private static final String REQUIRED_NATIONALITY = "Ukrainian"; + + @Override + public boolean test(Candidate candidate) { + int startYear = Integer.parseInt(candidate.getPeriodsInUkr().split("-")[0]); + int endYear = Integer.parseInt(candidate.getPeriodsInUkr().split("-")[1]); + int yearsInUkraine = endYear - startYear; + + return candidate.isAllowedToVote() + && candidate.getAge() >= MINIMUM_AGE + && candidate.getNationality().equals(REQUIRED_NATIONALITY) + && yearsInUkraine >= MIN_YEARS_IN_UKRAINE; + } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 57b1ca2e2..e50c234c9 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,8 +1,13 @@ package practice; -import java.util.Collections; +import java.util.Arrays; import java.util.List; +import java.util.NoSuchElementException; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import model.Candidate; +import model.Cat; import model.Person; public class StreamPractice { @@ -14,7 +19,21 @@ public class StreamPractice { * "Can't get min value from list: < Here is our input 'numbers' >" */ public int findMinEvenNumber(List numbers) { - return 0; + final String errorMessage = "Can't get min value from list " + numbers; + + if (numbers.isEmpty()) { + throw new RuntimeException("Can't get min value from list " + numbers); + } + + return Arrays.stream(numbers.stream() + .collect(Collectors.joining(",")) + .split(",")) + .distinct() + .map(Integer::parseInt) + .filter(i -> i % 2 == 0) + .min(Integer::compareTo) + .orElseThrow(() -> new RuntimeException(errorMessage)); + } /** @@ -23,7 +42,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("No even number found in list")); } /** @@ -35,7 +59,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 Collections.emptyList(); + Predicate agePredicate = p -> p.getAge() >= fromAge + && p.getAge() <= toAge + && p.getSex() == Person.Sex.MAN; + + return peopleList.stream() + .filter(agePredicate) + .collect(Collectors.toList()); } /** @@ -50,7 +80,16 @@ public List selectMenByAge(List peopleList, int fromAge, int toA */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { - return Collections.emptyList(); + Predicate workablePredicate = p -> + (p.getSex() == Person.Sex.WOMAN + && p.getAge() <= femaleToAge) + || (p.getSex() == Person.Sex.MAN + && p.getAge() <= maleToAge); + + return peopleList.stream() + .filter(p -> p.getAge() >= fromAge) + .filter(workablePredicate) + .collect(Collectors.toList()); } /** @@ -59,7 +98,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().isEmpty()) + .flatMap(p -> p.getCats().stream()) + .map(Cat::getName) + .collect(Collectors.toList()); } /** @@ -75,6 +120,11 @@ 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 b5f8a8eb2ebd53dd457150800432cae382c41118 Mon Sep 17 00:00:00 2001 From: Mich Hmk Date: Thu, 9 Jan 2025 17:41:09 +0200 Subject: [PATCH 2/3] ai-feedback --- src/main/java/practice/StreamPractice.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index e50c234c9..5c872d5ef 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -19,10 +19,10 @@ public class StreamPractice { * "Can't get min value from list: < Here is our input 'numbers' >" */ public int findMinEvenNumber(List numbers) { - final String errorMessage = "Can't get min value from list " + numbers; + final String errorMessage = "Can't get min value from list: " + numbers; if (numbers.isEmpty()) { - throw new RuntimeException("Can't get min value from list " + numbers); + throw new RuntimeException(errorMessage); } return Arrays.stream(numbers.stream() @@ -47,7 +47,7 @@ public Double getOddNumsAverage(List numbers) { .filter(n -> n % 2 != 0) .average() .orElseThrow(() -> - new NoSuchElementException("No even number found in list")); + new NoSuchElementException("No odd number found in list")); } /** From b2ca685e575b592d0ab26a6be15d304fcbb948de Mon Sep 17 00:00:00 2001 From: Mich Hmk Date: Fri, 10 Jan 2025 13:07:37 +0200 Subject: [PATCH 3/3] mentor-feedback --- .../java/practice/CandidateValidator.java | 7 ++-- src/main/java/practice/StreamPractice.java | 34 ++++++++----------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 3e4f3547f..ae1752f2c 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -7,16 +7,17 @@ public class CandidateValidator implements Predicate { private static final int MINIMUM_AGE = 35; private static final int MIN_YEARS_IN_UKRAINE = 10; private static final String REQUIRED_NATIONALITY = "Ukrainian"; + private static final String SEPARATOR = "-"; @Override public boolean test(Candidate candidate) { - int startYear = Integer.parseInt(candidate.getPeriodsInUkr().split("-")[0]); - int endYear = Integer.parseInt(candidate.getPeriodsInUkr().split("-")[1]); + int startYear = Integer.parseInt(candidate.getPeriodsInUkr().split(SEPARATOR)[0]); + int endYear = Integer.parseInt(candidate.getPeriodsInUkr().split(SEPARATOR)[1]); int yearsInUkraine = endYear - startYear; return candidate.isAllowedToVote() - && candidate.getAge() >= MINIMUM_AGE && candidate.getNationality().equals(REQUIRED_NATIONALITY) + && candidate.getAge() >= MINIMUM_AGE && yearsInUkraine >= MIN_YEARS_IN_UKRAINE; } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 5c872d5ef..4ab515f1c 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -4,13 +4,14 @@ import java.util.List; import java.util.NoSuchElementException; import java.util.function.Predicate; -import java.util.stream.Collectors; import java.util.stream.IntStream; import model.Candidate; import model.Cat; import model.Person; public class StreamPractice { + public static final CandidateValidator validator = new CandidateValidator(); + /** * Given list of strings where each element contains 1+ numbers: * input = {"5,30,100", "0,22,7", ...} @@ -24,16 +25,12 @@ public int findMinEvenNumber(List numbers) { if (numbers.isEmpty()) { throw new RuntimeException(errorMessage); } - - return Arrays.stream(numbers.stream() - .collect(Collectors.joining(",")) + return Arrays.stream(String.join(",", numbers) .split(",")) - .distinct() - .map(Integer::parseInt) - .filter(i -> i % 2 == 0) - .min(Integer::compareTo) - .orElseThrow(() -> new RuntimeException(errorMessage)); - + .map(Integer::parseInt) + .filter(i -> i % 2 == 0) + .min(Integer::compareTo) + .orElseThrow(() -> new RuntimeException(errorMessage)); } /** @@ -59,13 +56,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) { - Predicate agePredicate = p -> p.getAge() >= fromAge - && p.getAge() <= toAge - && p.getSex() == Person.Sex.MAN; + Predicate manByAgePredicate = p -> p.getSex() == Person.Sex.MAN + && p.getAge() >= fromAge + && p.getAge() <= toAge; return peopleList.stream() - .filter(agePredicate) - .collect(Collectors.toList()); + .filter(manByAgePredicate) + .toList(); } /** @@ -89,7 +86,7 @@ public List getWorkablePeople(int fromAge, int femaleToAge, return peopleList.stream() .filter(p -> p.getAge() >= fromAge) .filter(workablePredicate) - .collect(Collectors.toList()); + .toList(); } /** @@ -104,7 +101,7 @@ public List getCatsNames(List peopleList, int femaleAge) { && !p.getCats().isEmpty()) .flatMap(p -> p.getCats().stream()) .map(Cat::getName) - .collect(Collectors.toList()); + .toList(); } /** @@ -120,11 +117,10 @@ public List getCatsNames(List peopleList, int femaleAge) { * parametrized with Candidate in CandidateValidator. */ public List validateCandidates(List candidates) { - CandidateValidator validator = new CandidateValidator(); return candidates.stream() .filter(validator) .map(Candidate::getName) .sorted() - .collect(Collectors.toList()); + .toList(); } }