From ba1e04f232b21488084b7f23f508df67fe9c5940 Mon Sep 17 00:00:00 2001 From: Igor Korolevich Date: Thu, 26 Oct 2023 10:26:52 +0300 Subject: [PATCH 1/5] Simplifyed if method in CandidateValidator, changed names in test to more descriptive , changed user name from expected to user --- .../java/practice/CandidateValidator.java | 17 ++++++++- src/main/java/practice/StreamPractice.java | 37 ++++++++++++++++--- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 8d2e56c0e..0d55f7ac4 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,5 +1,18 @@ package practice; -public class CandidateValidator { - //write your code here +import model.Candidate; + +import java.util.function.Predicate; + +public class CandidateValidator implements Predicate { + @Override + public boolean test(Candidate candidate) { + String[] ages = candidate.getPeriodsInUkr().split("-"); + int ageFrom = Integer.parseInt(ages[0]); + int ageTo = Integer.parseInt(ages[1]); + return candidate.getAge() >= 35 + && candidate.isAllowedToVote() + && candidate.getNationality().equals("Ukrainian") + && ageTo - ageFrom >= 10; + } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 57b1ca2e2..36f32f064 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,7 +1,13 @@ package practice; +import java.util.Arrays; import java.util.Collections; +import java.util.Date; import java.util.List; +import java.util.NoSuchElementException; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + import model.Candidate; import model.Person; @@ -14,7 +20,13 @@ 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(nums ->{ + String[] num = nums.split(","); + return Arrays.stream(num) + .map(Integer::parseInt) + .filter(n -> n % 2 == 0); + }).min(Integer::compareTo) + .orElseThrow(() -> new RuntimeException("Can't get min value from list: < Here is our input 'numbers' >")); } /** @@ -23,7 +35,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 numbers.stream() + .mapToInt(number -> number).map(num -> { + if (numbers.indexOf(num) % 2 == 0) { + return num -1; + } + return num; + }).filter(num -> num % 2 == 0).average().orElseThrow(NoSuchElementException::new); + } /** @@ -35,7 +54,7 @@ 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.getSex() == Person.Sex.MAN && person.getAge() > fromAge && person.getAge() <= toAge).collect(Collectors.toList()); } /** @@ -50,7 +69,12 @@ 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(pepl -> pepl.getSex() == Person.Sex.MAN ? + pepl.getAge() >= fromAge + && pepl.getAge() <= maleToAge + : pepl.getAge() >= fromAge + && pepl.getAge() <= femaleToAge + ).collect(Collectors.toList()); } /** @@ -59,7 +83,7 @@ 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(pepl -> pepl.getAge() >= femaleAge && pepl.getSex() == Person.Sex.WOMAN).flatMap(pepl -> pepl.getCats().stream().map(cat -> cat.getName())).collect(Collectors.toList()); } /** @@ -75,6 +99,7 @@ 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(candidate -> candidateValidator.test(candidate)).map(cand -> cand.getName()).sorted().collect(Collectors.toList()); } } From f2e58baa0b65593a9c56ff69746e864f4830f9e1 Mon Sep 17 00:00:00 2001 From: Igor Korolevich Date: Sun, 29 Oct 2023 15:16:53 +0200 Subject: [PATCH 2/5] added method implementations and created a CandidateValidator --- .../java/practice/CandidateValidator.java | 3 +- src/main/java/practice/StreamPractice.java | 51 ++++++++++++------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 0d55f7ac4..cf5d6ae06 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,8 +1,7 @@ package practice; -import model.Candidate; - import java.util.function.Predicate; +import model.Candidate; public class CandidateValidator implements Predicate { @Override diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 36f32f064..93cce45f8 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,13 +1,10 @@ package practice; import java.util.Arrays; -import java.util.Collections; -import java.util.Date; import java.util.List; import java.util.NoSuchElementException; import java.util.stream.Collectors; import java.util.stream.IntStream; - import model.Candidate; import model.Person; @@ -20,13 +17,14 @@ public class StreamPractice { * "Can't get min value from list: < Here is our input 'numbers' >" */ public int findMinEvenNumber(List numbers) { - return numbers.stream().flatMap(nums ->{ - String[] num = nums.split(","); - return Arrays.stream(num) + return numbers.stream().flatMap(num -> { + String[] numString = num.split(","); + return Arrays.stream(numString) .map(Integer::parseInt) .filter(n -> n % 2 == 0); }).min(Integer::compareTo) - .orElseThrow(() -> new RuntimeException("Can't get min value from list: < Here is our input 'numbers' >")); + .orElseThrow(() -> new RuntimeException("Can't get min value from list: " + + numbers)); } /** @@ -35,13 +33,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 numbers.stream() - .mapToInt(number -> number).map(num -> { - if (numbers.indexOf(num) % 2 == 0) { - return num -1; - } - return num; - }).filter(num -> num % 2 == 0).average().orElseThrow(NoSuchElementException::new); + return IntStream.range(0,numbers.size()) + .mapToObj(index -> index % 2 != 0 ? numbers.get(index) - 1 : numbers.get(index)) + .filter(num -> num % 2 != 0) + .mapToInt(n -> n) + .average() + .orElseThrow(NoSuchElementException::new); } @@ -54,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 peopleList.stream().filter(person -> person.getSex() == Person.Sex.MAN && person.getAge() > fromAge && person.getAge() <= toAge).collect(Collectors.toList()); + return peopleList.stream() + .filter(person -> person.getSex() == Person.Sex.MAN + && person.getAge() > fromAge && person.getAge() <= toAge) + .collect(Collectors.toList()); } /** @@ -69,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(pepl -> pepl.getSex() == Person.Sex.MAN ? - pepl.getAge() >= fromAge + return peopleList + .stream() + .filter(pepl -> pepl.getSex() == Person.Sex.MAN + ? pepl.getAge() >= fromAge && pepl.getAge() <= maleToAge : pepl.getAge() >= fromAge && pepl.getAge() <= femaleToAge @@ -83,7 +85,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 peopleList.stream().filter(pepl -> pepl.getAge() >= femaleAge && pepl.getSex() == Person.Sex.WOMAN).flatMap(pepl -> pepl.getCats().stream().map(cat -> cat.getName())).collect(Collectors.toList()); + return peopleList.stream() + .filter(pepl -> pepl.getAge() >= femaleAge + && pepl.getSex() == Person.Sex.WOMAN) + .flatMap(pepl -> pepl.getCats() + .stream() + .map(cat -> cat.getName())) + .collect(Collectors.toList()); } /** @@ -100,6 +108,11 @@ public List getCatsNames(List peopleList, int femaleAge) { */ public List validateCandidates(List candidates) { CandidateValidator candidateValidator = new CandidateValidator(); - return candidates.stream().filter(candidate -> candidateValidator.test(candidate)).map(cand -> cand.getName()).sorted().collect(Collectors.toList()); + return candidates + .stream() + .filter(candidate -> candidateValidator.test(candidate)) + .map(candidate -> candidate.getName()) + .sorted() + .collect(Collectors.toList()); } } From 2a62d3d6b65486c49ae82fb4288b02644c3024c9 Mon Sep 17 00:00:00 2001 From: Igor Korolevich Date: Sun, 29 Oct 2023 15:30:21 +0200 Subject: [PATCH 3/5] fixed mistakes --- src/main/java/practice/StreamPractice.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 93cce45f8..47ac2139c 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -86,8 +86,8 @@ public List getWorkablePeople(int fromAge, int femaleToAge, */ public List getCatsNames(List peopleList, int femaleAge) { return peopleList.stream() - .filter(pepl -> pepl.getAge() >= femaleAge - && pepl.getSex() == Person.Sex.WOMAN) + .filter(person -> person.getAge() >= femaleAge + && person.getSex() == Person.Sex.WOMAN) .flatMap(pepl -> pepl.getCats() .stream() .map(cat -> cat.getName())) From 8f81734f5d234e4816de9037fcc5ba15f7cc8ad7 Mon Sep 17 00:00:00 2001 From: Igor Korolevich Date: Mon, 30 Oct 2023 00:46:09 +0200 Subject: [PATCH 4/5] fixed errors mentioned in the pool request --- .../java/practice/CandidateValidator.java | 16 ++++-- src/main/java/practice/StreamPractice.java | 51 ++----------------- 2 files changed, 14 insertions(+), 53 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index cf5d6ae06..1771558db 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -4,14 +4,20 @@ import model.Candidate; public class CandidateValidator implements Predicate { + public static final int MINIMAL_ACCEPTABLE_CANDIDATE_AGE = 35; + public static final int INDEX_OF_SINCE_YEAR_IN_COUNTRY = 0; + public static final int INDEX_OF_TO_YEAR_IN_COUNTRY = 1; + public static final String CANDIDATE_ACCEPTABLE_COUNTRY = "Ukrainian"; + public static final int MINIMAL_ACCEPTABLE_AGE_IN_COUNTRY = 10; + @Override public boolean test(Candidate candidate) { String[] ages = candidate.getPeriodsInUkr().split("-"); - int ageFrom = Integer.parseInt(ages[0]); - int ageTo = Integer.parseInt(ages[1]); - return candidate.getAge() >= 35 + int ageFrom = Integer.parseInt(ages[INDEX_OF_SINCE_YEAR_IN_COUNTRY]); + int ageTo = Integer.parseInt(ages[INDEX_OF_TO_YEAR_IN_COUNTRY]); + return candidate.getAge() >= MINIMAL_ACCEPTABLE_CANDIDATE_AGE && candidate.isAllowedToVote() - && candidate.getNationality().equals("Ukrainian") - && ageTo - ageFrom >= 10; + && candidate.getNationality().equals(CANDIDATE_ACCEPTABLE_COUNTRY) + && ageTo - ageFrom >= MINIMAL_ACCEPTABLE_AGE_IN_COUNTRY; } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 47ac2139c..476772766 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -9,13 +9,8 @@ import model.Person; public class StreamPractice { - /** - * Given list of strings where each element contains 1+ numbers: - * input = {"5,30,100", "0,22,7", ...} - * return min integer value. One more thing - we're interested in even numbers. - * If there is no needed data throw RuntimeException with message - * "Can't get min value from list: < Here is our input 'numbers' >" - */ + public static final String RUNTIME_EXCEPTION_MESSAGE = "Can't get min value from list: "; + public int findMinEvenNumber(List numbers) { return numbers.stream().flatMap(num -> { String[] numString = num.split(","); @@ -23,15 +18,10 @@ public int findMinEvenNumber(List numbers) { .map(Integer::parseInt) .filter(n -> n % 2 == 0); }).min(Integer::compareTo) - .orElseThrow(() -> new RuntimeException("Can't get min value from list: " + .orElseThrow(() -> new RuntimeException(RUNTIME_EXCEPTION_MESSAGE + 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()) .mapToObj(index -> index % 2 != 0 ? numbers.get(index) - 1 : numbers.get(index)) @@ -42,14 +32,6 @@ public Double getOddNumsAverage(List numbers) { } - /** - * Given a List of `Person` instances (having `name`, `age` and `sex` fields), - * for example, `Arrays.asList( new Person(«Victor», 16, Sex.MAN), - * new Person(«Helen», 42, Sex.WOMAN))`, - * select from the List only men whose age is from `fromAge` to `toAge` inclusively. - *

- * 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 peopleList.stream() .filter(person -> person.getSex() == Person.Sex.MAN @@ -57,16 +39,6 @@ public List selectMenByAge(List peopleList, int fromAge, int toA .collect(Collectors.toList()); } - /** - * Given a List of `Person` instances (having `name`, `age` and `sex` fields), - * for example, `Arrays.asList( new Person(«Victor», 16, Sex.MAN), - * new Person(«Helen», 42, Sex.WOMAN))`, - * select from the List only people whose age is from `fromAge` and to `maleToAge` (for men) - * or to `femaleToAge` (for women) inclusively. - *

- * Example: select people of working age - * (from 18 y.o. and to 60 y.o. for men and to 55 y.o. for women inclusively). - */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { return peopleList @@ -79,11 +51,6 @@ public List getWorkablePeople(int fromAge, int femaleToAge, ).collect(Collectors.toList()); } - /** - * Given a List of `Person` instances (having `name`, `age`, `sex` and `cats` fields, - * and each `Cat` having a `name` and `age`), - * return the names of all cats whose owners are women from `femaleAge` years old inclusively. - */ public List getCatsNames(List peopleList, int femaleAge) { return peopleList.stream() .filter(person -> person.getAge() >= femaleAge @@ -94,18 +61,6 @@ public List getCatsNames(List peopleList, int femaleAge) { .collect(Collectors.toList()); } - /** - * Your help with a election is needed. Given list of candidates, where each element - * has Candidate.class type. - * Check which candidates are eligible to apply for president position and return their - * names sorted alphabetically. - * 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 List validateCandidates(List candidates) { CandidateValidator candidateValidator = new CandidateValidator(); return candidates From 2299e7e818405d4d9286b9f555cfc7036f095167 Mon Sep 17 00:00:00 2001 From: Igor Korolevich Date: Mon, 30 Oct 2023 20:03:50 +0200 Subject: [PATCH 5/5] fixed errors mentioned in the pool request again --- .../java/practice/CandidateValidator.java | 21 +++---- src/main/java/practice/StreamPractice.java | 60 +++++++++---------- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 1771558db..0c23e2aea 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -4,20 +4,21 @@ import model.Candidate; public class CandidateValidator implements Predicate { - public static final int MINIMAL_ACCEPTABLE_CANDIDATE_AGE = 35; - public static final int INDEX_OF_SINCE_YEAR_IN_COUNTRY = 0; - public static final int INDEX_OF_TO_YEAR_IN_COUNTRY = 1; - public static final String CANDIDATE_ACCEPTABLE_COUNTRY = "Ukrainian"; - public static final int MINIMAL_ACCEPTABLE_AGE_IN_COUNTRY = 10; + private static final int MINIMAL_ACCEPTABLE_CANDIDATE_AGE = 35; + private static final int INDEX_OF_SINCE_YEAR_IN_COUNTRY = 0; + private static final int INDEX_OF_TO_YEAR_IN_COUNTRY = 1; + private static final String CANDIDATE_ACCEPTABLE_COUNTRY = "Ukrainian"; + private static final int MINIMAL_ACCEPTABLE_YEARS_IN_COUNTRY = 10; + private static final String SEPARATOR = "-"; @Override public boolean test(Candidate candidate) { - String[] ages = candidate.getPeriodsInUkr().split("-"); - int ageFrom = Integer.parseInt(ages[INDEX_OF_SINCE_YEAR_IN_COUNTRY]); - int ageTo = Integer.parseInt(ages[INDEX_OF_TO_YEAR_IN_COUNTRY]); + String[] years = candidate.getPeriodsInUkr().split(SEPARATOR); + int yearsFrom = Integer.parseInt(years[INDEX_OF_SINCE_YEAR_IN_COUNTRY]); + int yearsTo = Integer.parseInt(years[INDEX_OF_TO_YEAR_IN_COUNTRY]); return candidate.getAge() >= MINIMAL_ACCEPTABLE_CANDIDATE_AGE && candidate.isAllowedToVote() - && candidate.getNationality().equals(CANDIDATE_ACCEPTABLE_COUNTRY) - && ageTo - ageFrom >= MINIMAL_ACCEPTABLE_AGE_IN_COUNTRY; + && CANDIDATE_ACCEPTABLE_COUNTRY.equals(candidate.getNationality()) + && yearsTo - yearsFrom >= MINIMAL_ACCEPTABLE_YEARS_IN_COUNTRY; } } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 476772766..1ff69bc5b 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -2,72 +2,70 @@ 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; import model.Person; public class StreamPractice { - public static final String RUNTIME_EXCEPTION_MESSAGE = "Can't get min value from list: "; + private static final String RUNTIME_EXCEPTION_MESSAGE = "Can't get min value from list: "; + private static final String SEPARATOR = ","; + private Predicate predicateIfOdd = num -> num % 2 != 0; public int findMinEvenNumber(List numbers) { - return numbers.stream().flatMap(num -> { - String[] numString = num.split(","); - return Arrays.stream(numString) - .map(Integer::parseInt) - .filter(n -> n % 2 == 0); - }).min(Integer::compareTo) - .orElseThrow(() -> new RuntimeException(RUNTIME_EXCEPTION_MESSAGE - + numbers)); + return numbers.stream() + .flatMap(num -> Arrays.stream(num.split(SEPARATOR)) + .map(Integer::parseInt) + .filter(n -> n % 2 == 0)) + .min(Integer::compareTo) + .orElseThrow(() -> new RuntimeException(RUNTIME_EXCEPTION_MESSAGE + numbers)); } public Double getOddNumsAverage(List numbers) { - return IntStream.range(0,numbers.size()) - .mapToObj(index -> index % 2 != 0 ? numbers.get(index) - 1 : numbers.get(index)) - .filter(num -> num % 2 != 0) - .mapToInt(n -> n) + return IntStream.range(0, numbers.size()) + .map(index -> predicateIfOdd.test(index) + ? numbers.get(index) - 1 : numbers.get(index)) + .filter(predicateIfOdd::test) .average() - .orElseThrow(NoSuchElementException::new); - + .getAsDouble(); } public List selectMenByAge(List peopleList, int fromAge, int toAge) { return peopleList.stream() .filter(person -> person.getSex() == Person.Sex.MAN && person.getAge() > fromAge && person.getAge() <= toAge) - .collect(Collectors.toList()); + .toList(); } public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { return peopleList .stream() - .filter(pepl -> pepl.getSex() == Person.Sex.MAN - ? pepl.getAge() >= fromAge - && pepl.getAge() <= maleToAge - : pepl.getAge() >= fromAge - && pepl.getAge() <= femaleToAge - ).collect(Collectors.toList()); + .filter(people -> people.getAge() >= fromAge + && people.getAge() + <= (people.getSex() == Person.Sex.MAN + ? maleToAge : femaleToAge) + ).toList(); } public List getCatsNames(List peopleList, int femaleAge) { return peopleList.stream() .filter(person -> person.getAge() >= femaleAge && person.getSex() == Person.Sex.WOMAN) - .flatMap(pepl -> pepl.getCats() - .stream() - .map(cat -> cat.getName())) - .collect(Collectors.toList()); + .map(Person::getCats) + .flatMap(List::stream) + .map(Cat::getName) + .toList(); } public List validateCandidates(List candidates) { CandidateValidator candidateValidator = new CandidateValidator(); return candidates .stream() - .filter(candidate -> candidateValidator.test(candidate)) - .map(candidate -> candidate.getName()) + .filter(candidateValidator) + .map(Candidate::getName) .sorted() - .collect(Collectors.toList()); + .toList(); } }