-
Notifications
You must be signed in to change notification settings - Fork 1.3k
created task #1410
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
Open
Volodymyr8787
wants to merge
1
commit into
mate-academy:main
Choose a base branch
from
Volodymyr8787:jv-stream-github-practice
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
created task #1410
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,19 @@ | ||
| package practice; | ||
|
|
||
| public class CandidateValidator { | ||
| //write your code here | ||
| import java.util.function.Predicate; | ||
| import model.Candidate; | ||
|
|
||
| public class CandidateValidator implements Predicate<Candidate> { | ||
| @Override | ||
| public boolean test(Candidate candidate) { | ||
| String periods = candidate.getPeriodsInUkr(); | ||
| int years = Integer.parseInt(periods.split("-")[1]) | ||
| - Integer.parseInt(periods.split("-")[0]); | ||
|
|
||
| return candidate.getAge() >= 35 | ||
| && candidate.isAllowedToVote() | ||
| && "Ukrainian".equals(candidate.getNationality()) | ||
| && years >= 10; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,13 @@ public class StreamPractice { | |
| * "Can't get min value from list: < Here is our input 'numbers' >" | ||
| */ | ||
| public int findMinEvenNumber(List<String> numbers) { | ||
| return 0; | ||
| return numbers.stream() | ||
| .flatMap(n -> Arrays.stream(n.split(","))) | ||
| .mapToInt(Integer::parseInt) | ||
| .filter(n -> n % 2 == 0) | ||
| .min() | ||
| .orElseThrow(() -> new RuntimeException("Can't get min value from list:" | ||
|
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 #6 violation: The exception message should have a space after the colon: |
||
| + numbers)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -23,7 +33,13 @@ 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; | ||
| return IntStream.range(0, numbers.size()) | ||
| .map(i -> i % 2 != 0 | ||
| ? numbers.get(i) - 1 | ||
| : numbers.get(i)) | ||
| .filter(n -> n % 2 != 0) | ||
| .average() | ||
| .orElseThrow(NoSuchElementException::new); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -35,7 +51,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(p -> p.getAge() >= fromAge | ||
| && p.getAge() <= toAge | ||
| && p.getSex().equals(Person.Sex.MAN)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -50,7 +70,16 @@ 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(p -> | ||
| (p.getSex().equals(Person.Sex.MAN) | ||
| && p.getAge() >= fromAge | ||
| && p.getAge() <= maleToAge) | ||
| || | ||
| (p.getSex().equals(Person.Sex.WOMAN) | ||
| && p.getAge() >= fromAge | ||
| && p.getAge() <= femaleToAge)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -59,7 +88,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(p -> p.getAge() >= femaleAge | ||
| && p.getSex().equals(Person.Sex.WOMAN)) | ||
| .flatMap(p -> p.getCats().stream()) | ||
| .map(Cat::getName) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -75,6 +110,12 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) { | |
| * parametrized with Candidate in CandidateValidator. | ||
| */ | ||
| public List<String> validateCandidates(List<Candidate> candidates) { | ||
| return Collections.emptyList(); | ||
| CandidateValidator validator = new CandidateValidator(); | ||
|
|
||
| return candidates.stream() | ||
| .filter(validator) | ||
| .map(Candidate::getName) | ||
| .sorted() | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Checklist item #7 violation: Magic numbers and strings should be extracted as constants. The age threshold (35), nationality ('Ukrainian'), and years requirement (10) are used directly in the code.