diff --git a/config/checkstyle.xml b/config/checkstyle.xml new file mode 100644 index 000000000..ea0a5295c --- /dev/null +++ b/config/checkstyle.xml @@ -0,0 +1,250 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 398a873c1..01702da99 100644 --- a/pom.xml +++ b/pom.xml @@ -40,7 +40,7 @@ - ${maven.checkstyle.plugin.configLocation} + config/checkstyle.xml true true false diff --git a/src/main/java/practice/CandidateValidator.java b/src/main/java/practice/CandidateValidator.java index 8d2e56c0e..1b57986e4 100644 --- a/src/main/java/practice/CandidateValidator.java +++ b/src/main/java/practice/CandidateValidator.java @@ -1,5 +1,28 @@ package practice; -public class CandidateValidator { - //write your code here +import java.util.function.Predicate; +import model.Candidate; + +public class CandidateValidator implements Predicate { + public static final int MIN_AGE = 35; + public static final String REQUIRED_NATIONALITY = "Ukrainian"; + public static final int MIN_LIVE_PERIOD = 10; + + private int getYearsInUkraine(Candidate candidate) { + String period = candidate.getPeriodsInUkr(); + int years = Math.abs( + Integer.parseInt(period.split("-")[0]) + - Integer.parseInt(period.split("-")[1]) + ); + return years; + } + + @Override + public boolean test(Candidate candidate) { + return candidate.getAge() >= MIN_AGE + && candidate.isAllowedToVote() + && REQUIRED_NATIONALITY.equals(candidate.getNationality()) + && getYearsInUkraine(candidate) >= MIN_LIVE_PERIOD; + } } + diff --git a/src/main/java/practice/StreamPractice.java b/src/main/java/practice/StreamPractice.java index 57b1ca2e2..9c9707fab 100644 --- a/src/main/java/practice/StreamPractice.java +++ b/src/main/java/practice/StreamPractice.java @@ -1,11 +1,15 @@ package practice; -import java.util.Collections; +import java.util.Arrays; import java.util.List; +import java.util.NoSuchElementException; +import java.util.stream.IntStream; import model.Candidate; +import model.Cat; import model.Person; public class StreamPractice { + /** * Given list of strings where each element contains 1+ numbers: * input = {"5,30,100", "0,22,7", ...} @@ -14,7 +18,14 @@ 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(s -> Arrays.stream(s.split(","))) + .map(String::trim) + .mapToInt(Integer::parseInt) + .filter(n -> n % 2 == 0) + .min() + .orElseThrow(() -> new RuntimeException( + "Can't get min value from list: " + numbers)); } /** @@ -23,7 +34,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 0D; + return IntStream.range(0, numbers.size()) + .map(i -> i % 2 == 1 ? numbers.get(i) - 1 : numbers.get(i)) + .filter(n -> n % 2 != 0) + .average() + .orElseThrow(() -> new NoSuchElementException( + "No odd numbers in the list: " + numbers)); } /** @@ -35,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 Collections.emptyList(); + return peopleList.stream() + .filter(p -> p.getSex().equals(Person.Sex.MAN) + && (p.getAge() >= fromAge && p.getAge() <= toAge)) + .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(p -> (p.getSex() == Person.Sex.MAN + && (p.getAge() >= fromAge && p.getAge() <= maleToAge)) + || (p.getSex().equals(Person.Sex.WOMAN) + && (p.getAge() >= fromAge && p.getAge() <= femaleToAge))) + .toList(); } /** @@ -59,7 +83,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(p -> p.getCats().stream()) + .map(Cat::getName) + .toList(); } /** @@ -75,6 +103,10 @@ public List getCatsNames(List peopleList, int femaleAge) { * parametrized with Candidate in CandidateValidator. */ public List validateCandidates(List candidates) { - return Collections.emptyList(); + return candidates.stream() + .filter(new CandidateValidator()) + .map(Candidate::getName) + .sorted() + .toList(); } }