-
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
implement all methods #1220
base: main
Are you sure you want to change the base?
implement all methods #1220
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 meets most of the task requirements, there are a few critical issues that need to be addressed before we can approve it:
-
Handling Null or Empty Lists: It's important to check if lists like
candidates
,peopleList
, andnumbers
are null or empty before processing them. This will help avoid potentialNullPointerException
errors and ensure your code is robust. -
Use of Constants: To improve readability and maintainability, consider using constants for magic numbers and strings, such as the minimum residency period, age requirement, and nationality string. This aligns with best practices and will make your code easier to understand and modify in the future.
-
Exception Message Format: Ensure that the exception message in the
findMinEvenNumber
method is formatted correctly as per the task description.
Addressing these issues will enhance the functionality and quality of your code. Keep up the great work, and I look forward to seeing your improvements! 😊
✨ 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.
if (candidate == null) { | ||
return false; | ||
} | ||
if (!"Ukrainian".equals(candidate.getNationality())) { |
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 using a constant for the nationality string "Ukrainian" to improve readability and maintainability. This aligns with the checklist advice to avoid magic strings.
if (!"Ukrainian".equals(candidate.getNationality())) { | ||
return false; | ||
} | ||
if (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.
Consider using a constant for the age requirement (35) to improve readability and maintainability. This aligns with the checklist advice to avoid magic numbers.
.map(Integer::valueOf) | ||
.collect(Collectors.toList()); | ||
if (periodInUkraine.size() != 2 | ||
|| periodInUkraine.get(1) - periodInUkraine.get(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.
Consider using a constant for the minimum residency period (10 years) to improve readability and maintainability. This aligns with the checklist advice to avoid magic numbers.
@@ -14,7 +18,16 @@ 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() |
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 checking if the numbers
list is null or empty before processing to avoid potential NullPointerException
.
@@ -23,7 +36,12 @@ 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) { |
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 checking if the numbers
list is null or empty before processing to avoid potential NullPointerException
.
@@ -35,7 +53,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) { |
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 checking if the peopleList
is null or empty before processing to avoid potential NullPointerException
.
@@ -50,7 +71,11 @@ public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toA | |||
*/ | |||
public List<Person> getWorkablePeople(int fromAge, int 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.
Consider checking if the peopleList
is null or empty before processing to avoid potential NullPointerException
.
@@ -59,10 +84,15 @@ 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) { |
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 checking if the peopleList
is null or empty before processing to avoid potential NullPointerException
.
@@ -75,6 +105,10 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) { | |||
* parametrized with Candidate in CandidateValidator. | |||
*/ | |||
public List<String> validateCandidates(List<Candidate> candidates) { |
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 checking if the candidates
list is null or empty before processing to avoid potential NullPointerException
.
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 addressing the issues from the previous review! 🎉 Your code now meets the task requirements and constraints. Here are some key points to consider for further improvement:
-
Use of Constants: You've correctly defined constants for
neededAge
andneededPeriod
, but remember to follow Java naming conventions by using uppercase (e.g.,NEEDED_AGE
,NEEDED_PERIOD
). -
Handling Null or Empty Lists: You've added checks for null or empty lists in various methods, which is excellent. However, consider throwing a
NoSuchElementException
instead of returning default values like0D
to maintain consistency across your methods. -
Exception Handling: The exception message in the
findMinEvenNumber
method is now formatted correctly. Ensure that you validate the format of strings likeperiodsInUkr
before parsing to improve exception handling and avoid potential errors.
Your efforts to improve the code are commendable. Keep up the good work, and make sure to address these points in future projects to enhance your coding skills further. Remember, learning from feedback is a crucial part of your development journey. 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
private static final int neededAge = 35; | ||
private static final int neededPeriod = 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.
The constants neededAge
and neededPeriod
should be named in uppercase (e.g., NEEDED_AGE
, NEEDED_PERIOD
) to follow Java naming conventions for constants.
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.
fix comment
List<Integer> periodInUkraine = Arrays.stream(candidate.getPeriodsInUkr().split("-")) | ||
.map(String::trim) | ||
.map(Integer::valueOf) | ||
.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.
Before splitting and processing the periodsInUkr
string, ensure it is not null or empty to avoid potential exceptions.
} catch (NumberFormatException | IndexOutOfBoundsException e) { | ||
return false; |
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 validating the format of the periodsInUkr
string before parsing to integers to improve exception handling.
@@ -14,7 +19,19 @@ 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 == null || numbers.isEmpty()) { | |||
throw new RuntimeException("Can't get min value from list"); |
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 should include the input list as specified in the task description: "Can't get min value from list: < Here is our input 'numbers' >".
if (numbers == null || numbers.isEmpty()) { | ||
return 0D; |
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 throwing a NoSuchElementException
instead of returning 0D when the list is null or empty, to maintain consistency with other methods that throw exceptions when no valid data is found.
private static final int neededAge = 35; | ||
private static final int neededPeriod = 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.
fix comment
if (candidate == null) { | ||
return false; | ||
} | ||
if (!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"
|
||
@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.
can you simplify solution by using logical operators?
} | ||
return numbers.stream() | ||
.map(s -> s.split(",")) | ||
.flatMap(Arrays::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.
combine into one map
return numbers.stream() | ||
.map(s -> s.split(",")) | ||
.flatMap(Arrays::stream) | ||
.map(String::trim) |
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.
remove
return peopleList.stream() | ||
.filter(p -> p.getAge() >= fromAge && p.getAge() <= toAge) | ||
.filter(p -> p.getSex() == Person.Sex.MAN) | ||
.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.
for all places
.collect(Collectors.toList()); | |
.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.
fix comment
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.
not fixed
@@ -14,7 +19,19 @@ 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 == null || numbers.isEmpty()) { |
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.
remove (for all other methods also)
import model.Candidate; | ||
|
||
public class CandidateValidator implements Predicate<Candidate> { | ||
// National for being candidate |
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.
remove comments
|
||
public class CandidateValidator implements Predicate<Candidate> { | ||
// National for being candidate | ||
private static final String neededNational = "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.
check naming convention for constants
return peopleList.stream() | ||
.filter(p -> p.getAge() >= fromAge && p.getAge() <= toAge) | ||
.filter(p -> p.getSex() == Person.Sex.MAN) | ||
.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.
fix comment
return Collections.emptyList(); | ||
|
||
return peopleList.stream() | ||
.filter(p -> 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.
rename p
return Collections.emptyList(); | ||
|
||
return peopleList.stream() | ||
.filter(p -> 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
} | ||
|
||
/** | ||
/** V - wtf MA, wrong article |
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 changes
.filter(p -> p.getAge() >= femaleAge) | ||
.flatMap(c -> c.getCats().stream()) | ||
.map(Cat::getName) | ||
.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(); |
.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.
.collect(Collectors.toList()); | |
.toList(); |
return peopleList.stream() | ||
.filter(p -> p.getSex() == Person.Sex.WOMAN) | ||
.filter(p -> p.getAge() >= femaleAge) | ||
.flatMap(c -> c.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.
rename c
.filter((p -> (p.getSex() == Person.Sex.WOMAN && p.getAge() <= femaleToAge) | ||
|| (p.getSex() == Person.Sex.MAN && p.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.
.filter((p -> (p.getSex() == Person.Sex.WOMAN && p.getAge() <= femaleToAge) | |
|| (p.getSex() == Person.Sex.MAN && p.getAge() <= maleToAge))) | |
.filter(p -> (p.getSex() == Person.Sex.WOMAN && p.getAge() <= femaleToAge | |
|| p.getSex() == Person.Sex.MAN && p.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.
fix previous comments
import model.Candidate; | ||
|
||
public class CandidateValidator implements Predicate<Candidate> { | ||
private static final String requirementNationality = "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.
read about constant naming convention and rename
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 ;)
} | ||
try { | ||
List<Integer> periodInUkraine = Arrays.stream(candidate.getPeriodsInUkr().split("-")) | ||
.map(String::trim) |
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.
.map(String::trim) |
- periodInUkraine.get(0) < REQUIREMENT_PERIOD_IN_UKR) { | ||
return false; | ||
} | ||
} catch (NumberFormatException | IndexOutOfBoundsException e) { |
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.
} catch (NumberFormatException | IndexOutOfBoundsException e) { | |
} catch (NumberFormatException e) { |
you validated the collection size
return false; | ||
} | ||
try { | ||
List<Integer> periodInUkraine = Arrays.stream(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.
make separator constant field
} | ||
|
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.
leave only one line at the end of the file
.flatMap(s -> Arrays.stream(s.split(","))) | ||
.map(Integer::valueOf) | ||
.filter(i -> i % 2 == 0) | ||
.min(Integer::compareTo) |
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.
.min(Integer::compareTo) | |
.min() |
It is the default behavior
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.
return peopleList.stream() | ||
.filter(p -> p.getAge() >= fromAge && p.getAge() <= toAge) | ||
.filter(p -> p.getSex() == Person.Sex.MAN) | ||
.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.
not fixed
.filter(person -> person.getAge() >= fromAge) | ||
.filter(person -> (person.getSex() == Person.Sex.WOMAN | ||
&& person.getAge() <= femaleToAge | ||
|| person.getSex() == Person.Sex.MAN && person.getAge() <= maleToAge)) | ||
.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.
for complicated conditional, it is better to use Predicate
return Collections.emptyList(); | ||
|
||
return candidates.stream() | ||
.filter(new CandidateValidator()) |
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.
better make CandidateValidator class-level variable
No description provided.