-
Notifications
You must be signed in to change notification settings - Fork 1.3k
solution for jv-stream-github-practise #1404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,26 @@ | ||
| package practice; | ||
|
|
||
| public class CandidateValidator { | ||
| //write your code here | ||
| import java.util.function.Predicate; | ||
| import model.Candidate; | ||
| /*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 class CandidateValidator implements Predicate<Candidate> { | ||
| public static final int MIN_AGE = 35; | ||
| public static final String NATIONALITY = "Ukrainian"; | ||
| public static final int MIN_PERIOD = 10; | ||
|
|
||
| @Override | ||
| public boolean test(Candidate candidate) { | ||
| String[] period = candidate.getPeriodsInUkr().split("-"); | ||
| int periodInt = Integer.parseInt(period[1]) - Integer.parseInt(period[0]); | ||
| return candidate.getAge() >= MIN_AGE | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This violates checklist item #11 - the exception message is missing a colon and space after "list". It should be |
||
| && candidate.getNationality().equals(NATIONALITY) | ||
| && periodInt >= MIN_PERIOD && candidate.isAllowedToVote(); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checklist item #11 violation: The exception message must include the input parameter. Change to: |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| package practice; | ||
|
|
||
| import java.util.Collections; | ||
| 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 { | ||
|
|
@@ -14,7 +18,15 @@ public class StreamPractice { | |
| * "Can't get min value from list: < Here is our input 'numbers' >" | ||
| */ | ||
| public int findMinEvenNumber(List<String> numbers) { | ||
| return 0; | ||
| if (numbers.isEmpty()) { | ||
| throw new RuntimeException("Can't get min value from list" + numbers); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This violates checklist item #11: the exception message must include the
Comment on lines
+21
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The exception message format is incorrect - it's missing a colon and space. Per the checklist requirement, it should be: |
||
| } | ||
| return numbers.stream() | ||
| .flatMap(s -> Arrays.stream(s.split(","))) | ||
| .mapToInt(Integer::parseInt) | ||
| .filter(i -> i % 2 == 0) | ||
| .min() | ||
| .orElseThrow(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -23,7 +35,24 @@ public int findMinEvenNumber(List<String> numbers) { | |
| * But before that subtract 1 from each element on an odd position (having the odd index). | ||
| */ | ||
| public Double getOddNumsAverage(List<Integer> numbers) { | ||
| return 0D; | ||
| IntStream.range(0, numbers.size()) | ||
| .filter(x -> x % 2 == 1) | ||
| .forEach(i -> numbers.set(i, numbers.get(i) - 1)); | ||
| long count = numbers.stream() | ||
| .filter(i -> i % 2 != 0) | ||
| .count(); | ||
|
|
||
| if (count == 0) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
|
|
||
| long sum = numbers.stream() | ||
| .mapToInt(i -> i) | ||
| .filter(i -> i % 2 != 0) | ||
| .sum(); | ||
|
|
||
| return (double) sum / count; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -35,7 +64,11 @@ public Double getOddNumsAverage(List<Integer> numbers) { | |
| * Example: select men who can be recruited to army (from 18 to 27 years old inclusively). | ||
| */ | ||
| public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toAge) { | ||
| return Collections.emptyList(); | ||
| return peopleList.stream() | ||
| .filter(s -> s.getSex().equals(Person.Sex.MAN)) | ||
| .filter(s -> s.getAge() >= fromAge) | ||
| .filter(s -> s.getAge() <= toAge) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -50,7 +83,14 @@ public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toA | |
| */ | ||
| public List<Person> getWorkablePeople(int fromAge, int femaleToAge, | ||
| int maleToAge, List<Person> peopleList) { | ||
| return Collections.emptyList(); | ||
| return peopleList.stream() | ||
| .filter(s -> (s.getSex().equals(Person.Sex.MAN) | ||
| && s.getAge() <= maleToAge | ||
| && s.getAge() >= fromAge) | ||
| || (s.getSex().equals(Person.Sex.WOMAN) | ||
| && s.getAge() <= femaleToAge | ||
| && s.getAge() >= fromAge)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -59,7 +99,13 @@ public List<Person> getWorkablePeople(int fromAge, int femaleToAge, | |
| * return the names of all cats whose owners are women from `femaleAge` years old inclusively. | ||
| */ | ||
| public List<String> getCatsNames(List<Person> peopleList, int femaleAge) { | ||
| return Collections.emptyList(); | ||
| return peopleList.stream() | ||
| .filter(s -> s.getSex().equals(Person.Sex.WOMAN) | ||
| && s.getAge() >= femaleAge | ||
| && !s.getCats().isEmpty()) | ||
| .flatMap(person -> person.getCats().stream()) | ||
| .map(Cat::getName) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -75,6 +121,10 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) { | |
| * parametrized with Candidate in CandidateValidator. | ||
| */ | ||
| public List<String> validateCandidates(List<Candidate> candidates) { | ||
| return Collections.emptyList(); | ||
| return candidates.stream() | ||
| .filter(new CandidateValidator()) | ||
| .map(Candidate::getName) | ||
| .sorted() | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception message format does not match checklist #11 requirement. It should include a colon and space before the numbers parameter. Change to: "Can't get min value from list: " + numbers