Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

FroGitHub
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a 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:

  1. Handling Null or Empty Lists: It's important to check if lists like candidates, peopleList, and numbers are null or empty before processing them. This will help avoid potential NullPointerException errors and ensure your code is robust.

  2. 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.

  3. 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())) {

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) {

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) {

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()

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) {

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) {

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,

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) {

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) {

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.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a 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:

  1. Use of Constants: You've correctly defined constants for neededAge and neededPeriod, but remember to follow Java naming conventions by using uppercase (e.g., NEEDED_AGE, NEEDED_PERIOD).

  2. 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 like 0D to maintain consistency across your methods.

  3. Exception Handling: The exception message in the findMinEvenNumber method is now formatted correctly. Ensure that you validate the format of strings like periodsInUkr 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! ✨

Comment on lines 10 to 11
private static final int neededAge = 35;
private static final int neededPeriod = 10;

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix comment

Comment on lines 28 to 31
List<Integer> periodInUkraine = Arrays.stream(candidate.getPeriodsInUkr().split("-"))
.map(String::trim)
.map(Integer::valueOf)
.collect(Collectors.toList());

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.

Comment on lines 36 to 37
} catch (NumberFormatException | IndexOutOfBoundsException e) {
return false;

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");

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' >".

Comment on lines 43 to 44
if (numbers == null || numbers.isEmpty()) {
return 0D;

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.

Comment on lines 10 to 11
private static final int neededAge = 35;
private static final int neededPeriod = 10;

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")) {

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) {

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)

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)

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());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for all places

Suggested change
.collect(Collectors.toList());
.toList();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix comment

Copy link
Contributor

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()) {

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

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";

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());

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)

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)

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

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());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.collect(Collectors.toList());
.toList();

.filter(new CandidateValidator())
.map(Candidate::getName)
.sorted()
.collect(Collectors.toList());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.collect(Collectors.toList());
.toList();

return peopleList.stream()
.filter(p -> p.getSex() == Person.Sex.WOMAN)
.filter(p -> p.getAge() >= femaleAge)
.flatMap(c -> c.getCats().stream())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename c

Comment on lines 77 to 78
.filter((p -> (p.getSex() == Person.Sex.WOMAN && p.getAge() <= femaleToAge)
|| (p.getSex() == Person.Sex.MAN && p.getAge() <= maleToAge)))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.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))

Copy link

@Elena-Bruyako Elena-Bruyako left a 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";

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

Copy link
Contributor

@liliia-ponomarenko liliia-ponomarenko left a 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.map(String::trim)

- periodInUkraine.get(0) < REQUIREMENT_PERIOD_IN_UKR) {
return false;
}
} catch (NumberFormatException | IndexOutOfBoundsException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch (NumberFormatException | IndexOutOfBoundsException e) {
} catch (NumberFormatException e) {

you validated the collection size

return false;
}
try {
List<Integer> periodInUkraine = Arrays.stream(candidate.getPeriodsInUkr().split("-"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make separator constant field

}

Copy link
Contributor

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.min(Integer::compareTo)
.min()

It is the default behavior

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got error of compilation :(

изображение_2025-01-20_132455322

return peopleList.stream()
.filter(p -> p.getAge() >= fromAge && p.getAge() <= toAge)
.filter(p -> p.getSex() == Person.Sex.MAN)
.collect(Collectors.toList());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not fixed

Comment on lines 76 to 80
.filter(person -> person.getAge() >= fromAge)
.filter(person -> (person.getSex() == Person.Sex.WOMAN
&& person.getAge() <= femaleToAge
|| person.getSex() == Person.Sex.MAN && person.getAge() <= maleToAge))
.toList();
Copy link
Contributor

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())
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants