diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 5adb058d..1e9bb341 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -8,6 +8,7 @@ public class CandidateValidator implements Predicate { private static final int MIN_AGE = 35; private static final int MIN_LIVE_AGE = 10; private static final String COUNTRY = "Ukrainian"; + private static final String DASH = "-"; @Override public boolean test(Candidate candidate) { @@ -19,21 +20,18 @@ public boolean test(Candidate candidate) { private boolean checkYear(Candidate candidate) { String yearStr = candidate.getPeriodsInUkr(); - try { - int indexOfDash = yearStr.indexOf("-"); - if (indexOfDash == -1) { - throw new IllegalArgumentException("Invalid input format. The input string " - + "must contain a '-' character."); - } - - String year1 = yearStr.substring(0, indexOfDash); - String year2 = yearStr.substring(indexOfDash + 1); - - int liveAge = Integer.parseInt(year2) - Integer.parseInt(year1); - return liveAge >= MIN_LIVE_AGE; - } catch (NumberFormatException e) { - throw new NumberFormatException("One of the years is not a valid integer." - + "Input string: " + yearStr); + int indexOfDash = yearStr.indexOf(DASH); + + if (indexOfDash == -1) { + throw new IllegalArgumentException("Invalid input format. The input string " + + "must contain a '-' character."); } + + String year1 = yearStr.substring(0, indexOfDash); + String year2 = yearStr.substring(indexOfDash + 1); + + int liveAge = Integer.parseInt(year2) - Integer.parseInt(year1); + + return liveAge >= MIN_LIVE_AGE; } } diff --git a/src/main/java/practice/ComparisonWithDifferentAges.java b/src/main/java/practice/ComparisonWithDifferentAges.java new file mode 100644 index 00000000..06375ff7 --- /dev/null +++ b/src/main/java/practice/ComparisonWithDifferentAges.java @@ -0,0 +1,27 @@ +package practice; + +import java.util.function.Predicate; +import model.Person; + +public class ComparisonWithDifferentAges implements Predicate { + private int fromAge; + private int maleToAge; + private int femaleToAge; + + public ComparisonWithDifferentAges(int fromAge, int maleToAge, int femaleToAge) { + this.fromAge = fromAge; + this.maleToAge = maleToAge; + this.femaleToAge = femaleToAge; + } + + @Override + public boolean test(Person person) { + if (person.getSex() == Person.Sex.MAN) { + return person.getAge() >= fromAge + && person.getAge() <= maleToAge; + } else { + return person.getAge() >= fromAge + && person.getAge() <= femaleToAge; + } + } +} diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 48c7ecbd..4ad3cd3a 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -3,13 +3,14 @@ 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 { + private CandidateValidator validator = new CandidateValidator(); + /** * Given list of strings where each element contains 1+ numbers: * input = {"5,30,100", "0,22,7", ...} @@ -20,7 +21,6 @@ public class StreamPractice { public int findMinEvenNumber(List numbers) { return numbers.stream() .flatMap(nums -> Arrays.stream(nums.split(","))) - .map(String::trim) .mapToInt(Integer::parseInt) .filter(num -> num % 2 == 0) .min() @@ -51,10 +51,10 @@ public Double getOddNumsAverage(List numbers) { */ public List selectMenByAge(List peopleList, int fromAge, int toAge) { return peopleList.stream() - .filter(person -> person.getSex().equals(Person.Sex.MAN) + .filter(person -> person.getSex() == Person.Sex.MAN && person.getAge() >= fromAge && person.getAge() <= toAge) - .collect(Collectors.toList()); + .toList(); } /** @@ -69,14 +69,11 @@ public List selectMenByAge(List peopleList, int fromAge, int toA */ public List getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List peopleList) { + ComparisonWithDifferentAges comparison = new ComparisonWithDifferentAges(fromAge, + maleToAge, 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)) - .collect(Collectors.toList()); + .filter(comparison) + .toList(); } /** @@ -86,12 +83,12 @@ public List getWorkablePeople(int fromAge, int femaleToAge, */ public List getCatsNames(List peopleList, int femaleAge) { return peopleList.stream() - .filter(person -> person.getSex().equals(Person.Sex.WOMAN) + .filter(person -> person.getSex() == Person.Sex.WOMAN && person.getAge() >= femaleAge) .map(person -> person.getCats()) .flatMap(List::stream) .map(Cat::getName) - .collect(Collectors.toList()); + .toList(); } /** @@ -107,11 +104,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(); } }