From 2d0550a1277c5f3e9276e42ed27eedf5c197e0f6 Mon Sep 17 00:00:00 2001 From: FroGitHub Date: Fri, 17 Jan 2025 19:03:37 +0200 Subject: [PATCH] correcting my code --- .../java/practice/CandidateValidator.java | 9 ++++++--- src/main/java/practice/StreamPractice.java | 20 ++++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 31e161d0..683be9f5 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -7,15 +7,18 @@ import model.Candidate; public class CandidateValidator implements Predicate { + private static final int neededAge = 35; + private static final int neededPeriod = 10; + @Override public boolean test(Candidate candidate) { if (candidate == null) { return false; } - if (!"Ukrainian".equals(candidate.getNationality())) { + if (!candidate.getNationality().equals("Ukrainian")) { return false; } - if (candidate.getAge() < 35) { + if (candidate.getAge() < neededAge) { return false; } if (!candidate.isAllowedToVote()) { @@ -27,7 +30,7 @@ public boolean test(Candidate candidate) { .map(Integer::valueOf) .collect(Collectors.toList()); if (periodInUkraine.size() != 2 - || periodInUkraine.get(1) - periodInUkraine.get(0) < 10) { + || periodInUkraine.get(1) - periodInUkraine.get(0) < neededPeriod) { return false; } } catch (NumberFormatException | IndexOutOfBoundsException e) { diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 528e6e32..555520d0 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,6 +1,7 @@ package practice; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.NoSuchElementException; import java.util.stream.Collectors; @@ -18,6 +19,9 @@ public class StreamPractice { * "Can't get min value from list: < Here is our input 'numbers' >" */ public int findMinEvenNumber(List numbers) { + if (numbers == null || numbers.isEmpty()) { + throw new RuntimeException("Can't get min value from list"); + } return numbers.stream() .map(s -> s.split(",")) .flatMap(Arrays::stream) @@ -36,7 +40,9 @@ 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) { - + if (numbers == null || numbers.isEmpty()) { + return 0D; + } return IntStream.range(0, numbers.size()) .map(i -> i % 2 == 1 ? numbers.get(i) - 1 : numbers.get(i)) .filter(num -> num % 2 != 0) @@ -53,6 +59,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) { + if (peopleList == null || peopleList.isEmpty()) { + return Collections.emptyList(); + } return peopleList.stream() .filter(p -> p.getAge() >= fromAge && p.getAge() <= toAge) .filter(p -> p.getSex() == Person.Sex.MAN) @@ -71,6 +80,9 @@ public List selectMenByAge(List peopleList, int fromAge, int toA */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { + if (peopleList == null || peopleList.isEmpty()) { + return Collections.emptyList(); + } return peopleList.stream() .filter(p -> p.getAge() >= fromAge) .filter((p -> (p.getSex() == Person.Sex.WOMAN && p.getAge() <= femaleToAge) @@ -84,6 +96,9 @@ 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) { + if (peopleList == null || peopleList.isEmpty()) { + return Collections.emptyList(); + } return peopleList.stream() .filter(p -> p.getSex() == Person.Sex.WOMAN) .filter(p -> p.getAge() >= femaleAge) @@ -105,6 +120,9 @@ public List getCatsNames(List peopleList, int femaleAge) { * parametrized with Candidate in CandidateValidator. */ public List validateCandidates(List candidates) { + if (candidates == null || candidates.isEmpty()) { + return Collections.emptyList(); + } return candidates.stream() .filter(new CandidateValidator()) .map(Candidate::getName)