-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Implementation of all methods from tasks and class CandidateValidator #1202
base: main
Are you sure you want to change the base?
Implementation of all methods from tasks and class CandidateValidator #1202
Conversation
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.
Great job on implementing the tasks! 🎉 Your code meets the core requirements and effectively uses the Stream API. While there are a few areas for improvement, such as handling potential NullPointerException
and NumberFormatException
, these are minor and don't impact the overall functionality. Keep up the good work and consider these suggestions for future enhancements! 😊
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
String[] ages = candidate.getPeriodsInUkr().split("-"); | ||
boolean liveEnoughInUkraine = Integer.parseInt(ages[1]) | ||
- Integer.parseInt(ages[0]) >= 10; |
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.
Ensure that the getPeriodsInUkr()
method returns a valid string that can be split into two parts representing the start and end years. Consider adding error handling for cases where parsing might fail, such as when the string is not in the expected format or contains non-numeric values.
.flatMap(s -> Arrays.stream(s.split(","))) | ||
.map(Integer::parseInt) |
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.
Consider adding error handling for the Integer.parseInt
operation. If the input string contains non-numeric values or is not in the expected format, this will throw a NumberFormatException
. Adding a try-catch block or validating the input format before parsing could prevent runtime errors.
return Collections.emptyList(); | ||
return peopleList.stream() | ||
.filter(w -> w.getSex() == Person.Sex.WOMAN && w.getAge() >= femaleAge) | ||
.flatMap(w -> w.getCats().stream()) |
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.
Ensure that the getCats()
method returns a non-null list to avoid potential NullPointerException
. You might want to add a null check or use Optional
to handle cases where a person might not have any cats.
return false; | ||
} | ||
|
||
boolean oldEnough = candidate.getAge() >= 35; |
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.
let's create constants for age and nationality, 1 and 0 and "-"
public class CandidateValidator implements Predicate<Candidate> { | ||
@Override | ||
public boolean test(Candidate candidate) { | ||
if (candidate == null) { |
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.
you can just use
if (candidate == null) { | |
return candidate.getAge() >= MIN_AGE | |
&& candidate.isAllowedToVote() | |
&& candidate.getNationality().equals(REQUIRED_NATIONALITY) | |
&& checkTimeLivingInCountry(candidate); |
create separate method to checkTimeLivingInCountry()
@@ -35,7 +50,10 @@ 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(m -> m.getSex() == Person.Sex.MAN) |
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.
same for other places
.filter(m -> m.getSex() == Person.Sex.MAN) | |
.filter(person -> person.getSex() == Person.Sex.MAN) |
return peopleList.stream() | ||
.filter(m -> m.getSex() == Person.Sex.MAN) | ||
.filter(m -> m.getAge() >= fromAge && m.getAge() <= toAge) | ||
.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.
same for other places
.collect(Collectors.toList()); | |
.toList(); |
@Override | ||
public boolean test(Candidate candidate) { | ||
return candidate.getAge() >= MIN_AGE | ||
&& candidate.getNationality().equals("Ukrainian") |
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.
create constant for "Ukrainian"
} | ||
|
||
private boolean checkYear(Candidate candidate) { | ||
String[] ages = candidate.getPeriodsInUkr().split("-"); |
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.
create constant for "-"
|
||
private boolean checkYear(Candidate candidate) { | ||
String[] ages = candidate.getPeriodsInUkr().split("-"); | ||
return Integer.parseInt(ages[1]) |
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.
constants for 1 and 0
@@ -50,7 +67,12 @@ 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() == Person.Sex.MAN && p.getAge() >= fromAge |
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.
rename p
@@ -59,7 +81,11 @@ 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(w -> w.getSex() == Person.Sex.WOMAN && w.getAge() >= femaleAge) |
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.
rename w
this is a person list, so use person for example
No description provided.