-
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
f-hw-jv-stream-github-practice #1208
base: main
Are you sure you want to change the base?
f-hw-jv-stream-github-practice #1208
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.
Thank you for your hard work on this pull request! While your code mostly meets the task requirements, there are a few critical issues that need to be addressed before we can approve it:
-
Inclusivity in Age Filtering: The condition in
selectMenByAge
should include the lower boundfromAge
as specified in the task description. Please change the condition top.getAge() >= fromAge && p.getAge() <= toAge
. -
Exception Handling in
getLivePeriodInUkraine
: The method assumes the input format is always correct, which might lead to exceptions. Consider adding validation or exception handling to manage potential parsing issues. -
Unnecessary Type Check: The check
candidate.getClass().equals(Candidate.class)
is redundant since the parameter is already of typeCandidate
. Removing this will clean up your code.
These changes are essential to ensure the functionality and robustness of your solution. Once you've addressed these points, your code will be in great shape! Keep up the good work, and don't hesitate to reach out if you have any questions. 😊
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review
button in the Reviewers tab.
|
||
@Override | ||
public boolean test(Candidate candidate) { | ||
return candidate.getClass().equals(Candidate.class) |
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 check candidate.getClass().equals(Candidate.class)
is unnecessary. Since the method parameter is already of type Candidate
, this check will always be true. Consider removing it.
&& getLivePeriodInUkraine(candidate.getPeriodsInUkr()) >= MIN_PERIOD_IN_UKRAINE; | ||
} | ||
|
||
public static int getLivePeriodInUkraine(String periodsInUkr) { |
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 method getLivePeriodInUkraine
assumes that the input string periodsInUkr
is always in the correct format (e.g., '2000-2010'). If the format is incorrect or if there are parsing issues, this could throw a NumberFormatException
or ArrayIndexOutOfBoundsException
. Consider adding validation or exception handling to manage such cases.
return Collections.emptyList(); | ||
return peopleList.stream() | ||
.filter(p -> p.getSex().equals(Person.Sex.MAN) | ||
&& p.getAge() > fromAge && p.getAge() <= toAge) |
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 condition p.getAge() > fromAge && p.getAge() <= toAge
should be p.getAge() >= fromAge && p.getAge() <= toAge
to include the lower bound fromAge
in the age range, as the task description specifies inclusivity.
|| (person.getSex().equals(Person.Sex.WOMAN) | ||
&& person.getAge() >= fromAge | ||
&& person.getAge() <= femaleToAge))) | ||
.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 method toList()
is used here, which is available from Java 16 onwards. If you are using an earlier version of Java, consider using collect(Collectors.toList())
instead.
@@ -69,12 +102,16 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) { | |||
* names sorted alphabetically. | |||
* 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, | |||
* and live in Ukraine for 10 years. For the last requirement use field 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.
The comment seems to have a typo: 'Ukrainian' is mentioned twice. Ensure the comment accurately reflects the field name used in the Candidate
class.
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 CandidateValidator
and StreamPractice
classes! 🎉 Your solution is well-structured and mostly meets the task requirements. Here are a few points to consider for improvement:
-
Inclusivity in Age Filtering: In the
getWorkablePeople
method, ensure that the age condition for men includes the lower bound by usingperson.getAge() >= fromAge
. This will align with the task description and ensure accurate filtering. -
Input Validation in
getLivePeriodInUkraine
: The method currently assumes the input string is always correctly formatted. Adding validation to check the format before parsing can prevent potential exceptions or incorrect calculations. -
Exception Handling: The current exception handling in
getLivePeriodInUkraine
only prints an error message. Consider handling exceptions more robustly, such as returning a default value or rethrowing the exception with a custom message, to enhance the robustness of your code.
You've made significant progress, and addressing these points will further strengthen your solution. Keep up the great work and continue learning from these experiences! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
} catch (NumberFormatException e) { | ||
System.out.println("String does not contain a parsable integer"); |
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 current exception handling only prints an error message. Consider handling the exception more robustly, such as by returning a default value or rethrowing the exception with a custom message.
public static int getLivePeriodInUkraine(String periodsInUkr) { | ||
String[] split = periodsInUkr.split("-"); | ||
int period = 0; | ||
try { | ||
period = Integer.parseInt(split[LAST_INDEX_END]) | ||
- Integer.parseInt(split[FIRST_INDEX_START]); |
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 method getLivePeriodInUkraine
assumes the input string is always in the correct format (e.g., 'YYYY-YYYY'). Consider adding validation to ensure the input format is correct before parsing, to prevent potential exceptions or incorrect calculations.
&& person.getAge() > fromAge | ||
&& person.getAge() <= maleToAge) |
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.
In the getWorkablePeople
method, the condition person.getAge() > fromAge
should be person.getAge() >= fromAge
to include the lower bound age for men, as specified in the task description.
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.
Good job! Let’s improve your solution ;)
} | ||
|
||
public static int getLivePeriodInUkraine(String periodsInUkr) { | ||
String[] split = periodsInUkr.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.
it also can be a constant
&& getLivePeriodInUkraine(candidate.getPeriodsInUkr()) >= MIN_PERIOD_IN_UKRAINE; | ||
} | ||
|
||
public static int getLivePeriodInUkraine(String periodsInUkr) { |
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.
Why public static?
public static int getLivePeriodInUkraine(String periodsInUkr) { | |
private int getLivePeriodInUkraine(String periodsInUkr) { |
period = Integer.parseInt(split[LAST_INDEX_END]) | ||
- Integer.parseInt(split[FIRST_INDEX_START]); | ||
} catch (NumberFormatException e) { | ||
System.out.println("String does not contain a parsable integer"); |
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 throw a runtime exception. Remove printing to the console. We'll explore logging a little later.
.map(index -> index % 2 != 0 ? numbers.get(index) - 1 : numbers.get(index)) | ||
.filter(n -> n % 2 != 0) | ||
.average() | ||
.orElseThrow(NoSuchElementException::new); |
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 add a message
return peopleList.stream() | ||
.filter(p -> p.getSex().equals(Person.Sex.MAN) | ||
&& p.getAge() >= fromAge && p.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.
.collect(Collectors.toList()); | |
.toList(); |
@@ -50,7 +70,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(person -> ((person.getSex().equals(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.
.filter(person -> ((person.getSex().equals(Person.Sex.MAN) | |
.filter(person -> ((person.getSex() == Person.Sex.MAN |
Let’s compare enums in this way
|| (person.getSex().equals(Person.Sex.WOMAN) | ||
&& person.getAge() >= fromAge | ||
&& person.getAge() <= femaleToAge))) |
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.
for complicated conditional, it is better to use Predicate
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.
how I can do this? if fromAge, femaleToAge and maleToAge is a variable that we accept as input to the method. They are not constant.
|| (person.getSex().equals(Person.Sex.WOMAN) | ||
&& person.getAge() >= fromAge | ||
&& person.getAge() <= femaleToAge))) | ||
.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.
.collect(Collectors.toList()); | |
.toList(); |
fix it everywhere
@@ -69,12 +102,16 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) { | |||
* names sorted alphabetically. | |||
* 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, | |||
* and live in Ukraine for 10 years. For the last requirement use field 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.
revert task description
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.
Good job! A few minor comments ;)
@@ -35,7 +52,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(p -> p.getSex().equals(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.
.filter(p -> p.getSex().equals(Person.Sex.MAN) | |
.filter(p -> p.getSex() == Person.Sex.MAN |
the same
.filter(person -> ((person.getSex() == Person.Sex.MAN | ||
&& person.getAge() > fromAge | ||
&& person.getAge() <= maleToAge) | ||
|| (person.getSex().equals(Person.Sex.WOMAN) | ||
&& person.getAge() >= fromAge | ||
&& person.getAge() <= femaleToAge))) |
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.
.filter(person -> ((person.getSex() == Person.Sex.MAN | |
&& person.getAge() > fromAge | |
&& person.getAge() <= maleToAge) | |
|| (person.getSex().equals(Person.Sex.WOMAN) | |
&& person.getAge() >= fromAge | |
&& person.getAge() <= femaleToAge))) | |
Predicate<Person> malePredicate = p -> p.getSex() == Person.Sex.MAN | |
&& p.getAge() >= fromAge && p.getAge() <= maleToAge; | |
Predicate<Person> femalePredicate = p -> p.getSex() == Person.Sex.WOMAN | |
&& p.getAge() >= fromAge && p.getAge() <= femaleToAge; | |
return peopleList.stream() | |
.filter(malePredicate.or(femalePredicate)) |
You can do it in this way
@@ -59,7 +86,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(person -> person.getSex().equals(Person.Sex.WOMAN) |
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.
.filter(person -> person.getSex().equals(Person.Sex.WOMAN) | |
.filter(person -> person.getSex() == Person.Sex.WOMAN |
the same
No description provided.