diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index d6d40c65b..ad64f0716 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,18 +1,25 @@ package practice; -import model.Candidate; - import java.util.function.Predicate; +import model.Candidate; public class CandidateValidator implements Predicate { - @Override public boolean test(Candidate candidate) { - String[] PeriodsInUkr = candidate.getPeriodsInUkr().split("-"); + periodsInUkrValidator(candidate); + String[] periodsInUkr = candidate.getPeriodsInUkr().split("-"); return candidate.getAge() >= 35 && candidate.getNationality().equals("Ukrainian") && candidate.isAllowedToVote() - && Integer.parseInt(PeriodsInUkr[1]) - Integer.parseInt(PeriodsInUkr[0]) > 10; + && Integer.parseInt(periodsInUkr[1]) - Integer.parseInt(periodsInUkr[0]) > 10; + } + + private void periodsInUkrValidator(Candidate candidate) { + for (int i = 0; i < candidate.getPeriodsInUkr().length(); i++) { + if (Character.isLetter(candidate.getPeriodsInUkr().charAt(i))) { + throw new RuntimeException("Data contains letters"); + } + } } //write your code here } diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 9c73c9ca8..807eaf490 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,9 +1,10 @@ package practice; -import java.util.*; -import java.util.function.Predicate; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.NoSuchElementException; import java.util.stream.IntStream; - import model.Candidate; import model.Cat; import model.Person; @@ -17,11 +18,13 @@ public class StreamPractice { * "Can't get min value from list: < Here is our input 'numbers' >" */ public int findMinEvenNumber(List numbers) { + dataValidatorForFindMinEvenNumber(numbers); return numbers.stream().map(s -> s.split(",")) .flatMap(Arrays::stream) .mapToInt(Integer::valueOf) .filter(i -> i % 2 == 0) - .min().orElseThrow(() -> new RuntimeException("Can't get min value from list: " + numbers)); + .min().orElseThrow(() -> new RuntimeException("Can't get min value from list: " + + numbers)); } /** @@ -47,7 +50,9 @@ public Double getOddNumsAverage(List numbers) { */ public List selectMenByAge(List peopleList, int fromAge, int toAge) { return peopleList.stream() - .filter(i -> i.getAge() >= fromAge && i.getAge() <= toAge && i.getSex().equals(Person.Sex.MAN)) + .filter(i -> i.getAge() >= fromAge + && i.getAge() <= toAge + && i.getSex().equals(Person.Sex.MAN)) .toList(); } @@ -77,7 +82,7 @@ public List getWorkablePeople(int fromAge, int femaleToAge, */ public List getCatsNames(List peopleList, int femaleAge) { return peopleList.stream() - .filter(i -> i.getAge() > femaleAge && i.getSex().equals(Person.Sex.WOMAN)) + .filter(i -> i.getAge() >= femaleAge && i.getSex().equals(Person.Sex.WOMAN)) .map(Person::getCats) .flatMap(Collection::stream) .map(Cat::getName) @@ -104,4 +109,15 @@ public List validateCandidates(List candidates) { .sorted() .toList(); } + + private void dataValidatorForFindMinEvenNumber(List data) { + for (String datas : data) { + for (int i = 0; i < datas.length(); i++) { + if (Character.isLetter(datas.charAt(i))) { + throw new RuntimeException("Data contains letters"); + + } + } + } + } }