From 9cdf373a609467b38d297683be54217aa11b3f99 Mon Sep 17 00:00:00 2001 From: Heorhii Tsarenko Date: Sun, 15 Dec 2024 13:53:55 +0200 Subject: [PATCH 1/6] complete all tasks --- .../java/practice/CandidateValidator.java | 34 ++++++++++++++- src/main/java/practice/StreamPractice.java | 42 ++++++++++++++++--- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 8d2e56c0e..0cd315552 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,5 +1,37 @@ package practice; -public class CandidateValidator { +import model.Candidate; + +import java.util.Arrays; +import java.util.function.Predicate; + +public class CandidateValidator implements Predicate { + + @Override + public boolean test(Candidate candidate) { + if (candidate.getAge() < 35) { + return false; + } + + if (!candidate.isAllowedToVote()) { + return false; + } + + if (!"Ukrainian".equals(candidate.getNationality())) { + return false; + } + + String[] periods = candidate.getPeriodsInUkr().split(","); + int totalYearInUkraine = Arrays.stream(periods) + .mapToInt(period -> { + String[] range = period.trim().split("-"); + int startYear = Integer.parseInt(range[0]); + int endYear = Integer.parseInt(range[1]); + return endYear - startYear + 1; // Учитываем включение последнего года + }) + .sum(); + + return totalYearInUkraine >= 10; + } //write your code here } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 57b1ca2e2..3f23e980a 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,8 +1,15 @@ package practice; +import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.NoSuchElementException; +import java.util.stream.Collectors; +import java.util.stream.DoubleStream; +import java.util.stream.IntStream; + import model.Candidate; +import model.Cat; import model.Person; public class StreamPractice { @@ -14,7 +21,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(","))) + .map(Integer::parseInt) + .filter(num -> num % 2 == 0) + .min(Integer::compareTo) + .orElseThrow(() -> new RuntimeException("Can't get min value from list " + numbers)); } /** @@ -23,7 +35,11 @@ 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 == 1 ? numbers.get(i) - 1 : numbers.get(i)) + .filter(i -> i % 2 == 1) + .average() + .orElseThrow(NoSuchElementException::new); } /** @@ -35,7 +51,9 @@ 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().equals(Person.Sex.MAN) && (p.getAge() >= fromAge && p.getAge() <= toAge)) + .collect(Collectors.toList()); } /** @@ -50,7 +68,10 @@ 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().equals(Person.Sex.MAN) && p.getAge() >= fromAge && p.getAge() <= maleToAge) + || p.getSex().equals(Person.Sex.WOMAN) && p.getAge() >= fromAge && p.getAge() <= femaleToAge) + .collect(Collectors.toList()); } /** @@ -59,7 +80,11 @@ 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().equals(Person.Sex.WOMAN) && p.getAge() >= femaleAge) + .flatMap(person -> person.getCats().stream()) + .map(Cat::getName) + .collect(Collectors.toList()); } /** @@ -75,6 +100,11 @@ public List getCatsNames(List peopleList, int femaleAge) { * parametrized with Candidate in CandidateValidator. */ public List validateCandidates(List candidates) { - return Collections.emptyList(); + CandidateValidator candidateValidator = new CandidateValidator(); + return candidates.stream() + .filter(candidateValidator) + .map(Candidate::getName) + .sorted() + .collect(Collectors.toList()); } } From 8721295de3e348450bfaa9a1640e4008f1f1ac65 Mon Sep 17 00:00:00 2001 From: Heorhii Tsarenko Date: Sun, 15 Dec 2024 13:54:35 +0200 Subject: [PATCH 2/6] delete unused import statement --- src/main/java/practice/StreamPractice.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 3f23e980a..f2944e53f 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,11 +1,9 @@ package practice; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.NoSuchElementException; import java.util.stream.Collectors; -import java.util.stream.DoubleStream; import java.util.stream.IntStream; import model.Candidate; From 1af72543b68a23f82115b421faa6ca15cbd8074f Mon Sep 17 00:00:00 2001 From: Heorhii Tsarenko Date: Sun, 15 Dec 2024 13:56:52 +0200 Subject: [PATCH 3/6] fixed checkstyle executions --- src/main/java/practice/StreamPractice.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index f2944e53f..9eb89752c 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -24,7 +24,8 @@ public int findMinEvenNumber(List numbers) { .map(Integer::parseInt) .filter(num -> num % 2 == 0) .min(Integer::compareTo) - .orElseThrow(() -> new RuntimeException("Can't get min value from list " + numbers)); + .orElseThrow(() -> + new RuntimeException("Can't get min value from list " + numbers)); } /** @@ -50,7 +51,8 @@ public Double getOddNumsAverage(List numbers) { */ public List selectMenByAge(List peopleList, int fromAge, int toAge) { return peopleList.stream() - .filter(p -> p.getSex().equals(Person.Sex.MAN) && (p.getAge() >= fromAge && p.getAge() <= toAge)) + .filter(p -> p.getSex().equals(Person.Sex.MAN) + && (p.getAge() >= fromAge && p.getAge() <= toAge)) .collect(Collectors.toList()); } @@ -67,8 +69,10 @@ public List selectMenByAge(List peopleList, int fromAge, int toA public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { return peopleList.stream() - .filter(p -> (p.getSex().equals(Person.Sex.MAN) && p.getAge() >= fromAge && p.getAge() <= maleToAge) - || p.getSex().equals(Person.Sex.WOMAN) && p.getAge() >= fromAge && p.getAge() <= femaleToAge) + .filter(p -> (p.getSex().equals(Person.Sex.MAN) + && p.getAge() >= fromAge && p.getAge() <= maleToAge) + || p.getSex().equals(Person.Sex.WOMAN) + && p.getAge() >= fromAge && p.getAge() <= femaleToAge) .collect(Collectors.toList()); } From e8acf45b0086f053ec300dcbeb1d629dfe6e34a3 Mon Sep 17 00:00:00 2001 From: Heorhii Tsarenko Date: Sun, 15 Dec 2024 14:01:18 +0200 Subject: [PATCH 4/6] fixed all checkstyle executions --- src/main/java/practice/CandidateValidator.java | 3 +-- src/main/java/practice/StreamPractice.java | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 0cd315552..b6582ecc3 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,9 +1,8 @@ package practice; -import model.Candidate; - import java.util.Arrays; import java.util.function.Predicate; +import model.Candidate; public class CandidateValidator implements Predicate { diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 9eb89752c..1836924f7 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -5,7 +5,6 @@ import java.util.NoSuchElementException; import java.util.stream.Collectors; import java.util.stream.IntStream; - import model.Candidate; import model.Cat; import model.Person; From e74cf45503660c00ce34b848c0029487b93e0a4f Mon Sep 17 00:00:00 2001 From: Heorhii Tsarenko Date: Mon, 16 Dec 2024 19:31:31 +0200 Subject: [PATCH 5/6] delete magic numbers rename p to person --- .../java/practice/CandidateValidator.java | 6 ++--- src/main/java/practice/StreamPractice.java | 24 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index b6582ecc3..e0f81f76e 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -5,10 +5,11 @@ import model.Candidate; public class CandidateValidator implements Predicate { + private static final int PRESIDENT_ELIGIBILITY_AGE = 35; @Override public boolean test(Candidate candidate) { - if (candidate.getAge() < 35) { + if (candidate.getAge() < PRESIDENT_ELIGIBILITY_AGE) { return false; } @@ -26,11 +27,10 @@ public boolean test(Candidate candidate) { String[] range = period.trim().split("-"); int startYear = Integer.parseInt(range[0]); int endYear = Integer.parseInt(range[1]); - return endYear - startYear + 1; // Учитываем включение последнего года + return endYear - startYear + 1; }) .sum(); return totalYearInUkraine >= 10; } - //write your code here } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 1836924f7..d9c2b260d 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -3,7 +3,6 @@ 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; @@ -50,9 +49,9 @@ public Double getOddNumsAverage(List numbers) { */ public List selectMenByAge(List peopleList, int fromAge, int toAge) { return peopleList.stream() - .filter(p -> p.getSex().equals(Person.Sex.MAN) - && (p.getAge() >= fromAge && p.getAge() <= toAge)) - .collect(Collectors.toList()); + .filter(person -> person.getSex().equals(Person.Sex.MAN) + && (person.getAge() >= fromAge && person.getAge() <= toAge)) + .toList(); } /** @@ -68,11 +67,11 @@ public List selectMenByAge(List peopleList, int fromAge, int toA public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { return peopleList.stream() - .filter(p -> (p.getSex().equals(Person.Sex.MAN) - && p.getAge() >= fromAge && p.getAge() <= maleToAge) - || p.getSex().equals(Person.Sex.WOMAN) - && p.getAge() >= fromAge && p.getAge() <= femaleToAge) - .collect(Collectors.toList()); + .filter(person -> (person.getSex().equals(Person.Sex.MAN) + && person.getAge() >= fromAge && person.getAge() <= maleToAge) + || person.getSex().equals(Person.Sex.WOMAN) + && person.getAge() >= fromAge && person.getAge() <= femaleToAge) + .toList(); } /** @@ -82,10 +81,11 @@ public List getWorkablePeople(int fromAge, int femaleToAge, */ public List getCatsNames(List peopleList, int femaleAge) { return peopleList.stream() - .filter(p -> p.getSex().equals(Person.Sex.WOMAN) && p.getAge() >= femaleAge) + .filter(person -> person.getSex().equals(Person.Sex.WOMAN) + && person.getAge() >= femaleAge) .flatMap(person -> person.getCats().stream()) .map(Cat::getName) - .collect(Collectors.toList()); + .toList(); } /** @@ -106,6 +106,6 @@ public List validateCandidates(List candidates) { .filter(candidateValidator) .map(Candidate::getName) .sorted() - .collect(Collectors.toList()); + .toList(); } } From 8a68811c4295d4d4aea951b632076a3d1ea85770 Mon Sep 17 00:00:00 2001 From: Heorhii Tsarenko Date: Wed, 18 Dec 2024 23:30:55 +0200 Subject: [PATCH 6/6] make a few constant field for separators re-work predicate interface in CandidateValidator --- .../java/practice/CandidateValidator.java | 31 +++++++++---------- src/main/java/practice/StreamPractice.java | 19 ++++++++---- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index e0f81f76e..47cde5b1d 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -5,32 +5,31 @@ import model.Candidate; public class CandidateValidator implements Predicate { + private static final int MINIMAL_LIVING_YEARS = 10; private static final int PRESIDENT_ELIGIBILITY_AGE = 35; + private static final String REQUIRED_NATIONALITY = "Ukrainian"; + private static final String COMMA = ","; + private static final String DASH = "-"; @Override public boolean test(Candidate candidate) { - if (candidate.getAge() < PRESIDENT_ELIGIBILITY_AGE) { - return false; - } - - if (!candidate.isAllowedToVote()) { - return false; - } - - if (!"Ukrainian".equals(candidate.getNationality())) { - return false; - } + return candidate.getAge() >= PRESIDENT_ELIGIBILITY_AGE + && candidate.isAllowedToVote() + && candidate.getNationality().equals(REQUIRED_NATIONALITY) + && checkTimeLivingInCountry(candidate); + } - String[] periods = candidate.getPeriodsInUkr().split(","); + private boolean checkTimeLivingInCountry(Candidate candidate) { + String[] periods = candidate.getPeriodsInUkr().split(COMMA); int totalYearInUkraine = Arrays.stream(periods) .mapToInt(period -> { - String[] range = period.trim().split("-"); + String[] range = period.trim().split(DASH); int startYear = Integer.parseInt(range[0]); int endYear = Integer.parseInt(range[1]); - return endYear - startYear + 1; + return endYear - startYear; }) .sum(); - - return totalYearInUkraine >= 10; + return totalYearInUkraine >= MINIMAL_LIVING_YEARS; } + } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index d9c2b260d..4e3a137d2 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -3,6 +3,7 @@ import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; +import java.util.function.Predicate; import java.util.stream.IntStream; import model.Candidate; import model.Cat; @@ -25,18 +26,19 @@ public int findMinEvenNumber(List numbers) { .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) { return IntStream.range(0, numbers.size()) .map(i -> i % 2 == 1 ? numbers.get(i) - 1 : numbers.get(i)) .filter(i -> i % 2 == 1) .average() - .orElseThrow(NoSuchElementException::new); + .orElseThrow(() -> + new NoSuchElementException("The list doesn't have need element")); } /** @@ -66,11 +68,16 @@ public List selectMenByAge(List peopleList, int fromAge, int toA */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { + Predicate isManInRange = person -> + person.getSex().equals(Person.Sex.MAN) + && person.getAge() >= fromAge && person.getAge() <= maleToAge; + + Predicate isWomanInRange = person -> + person.getSex().equals(Person.Sex.WOMAN) + && person.getAge() >= fromAge && person.getAge() <= femaleToAge; + return peopleList.stream() - .filter(person -> (person.getSex().equals(Person.Sex.MAN) - && person.getAge() >= fromAge && person.getAge() <= maleToAge) - || person.getSex().equals(Person.Sex.WOMAN) - && person.getAge() >= fromAge && person.getAge() <= femaleToAge) + .filter(isManInRange.or(isWomanInRange)) .toList(); }