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

f-hw-jv-stream-github-practice #1208

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

Conversation

ihor-sydorenko
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 mostly meets the task requirements, there are a few critical issues that need to be addressed before we can approve it:

  1. Inclusivity in Age Filtering: The condition in selectMenByAge should include the lower bound fromAge as specified in the task description. Please change the condition to p.getAge() >= fromAge && p.getAge() <= toAge.

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

  3. Unnecessary Type Check: The check candidate.getClass().equals(Candidate.class) is redundant since the parameter is already of type Candidate. 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)

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

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)

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

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',

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.

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 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:

  1. Inclusivity in Age Filtering: In the getWorkablePeople method, ensure that the age condition for men includes the lower bound by using person.getAge() >= fromAge. This will align with the task description and ensure accurate filtering.

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

  3. 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! ✨

Comment on lines 27 to 28
} catch (NumberFormatException e) {
System.out.println("String does not contain a parsable integer");

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.

Comment on lines 21 to 26
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]);

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.

Comment on lines 75 to 76
&& person.getAge() > fromAge
&& person.getAge() <= maleToAge)

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.

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

}

public static int getLivePeriodInUkraine(String periodsInUkr) {
String[] split = periodsInUkr.split("-");
Copy link
Contributor

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

Choose a reason for hiding this comment

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

Why public static?

Suggested change
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");
Copy link
Contributor

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

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());
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
.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)
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
.filter(person -> ((person.getSex().equals(Person.Sex.MAN)
.filter(person -> ((person.getSex() == Person.Sex.MAN

Let’s compare enums in this way

Comment on lines 77 to 79
|| (person.getSex().equals(Person.Sex.WOMAN)
&& person.getAge() >= fromAge
&& person.getAge() <= femaleToAge)))
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

Copy link
Author

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());
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
.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',
Copy link
Contributor

Choose a reason for hiding this comment

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

revert task description

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! 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)
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
.filter(p -> p.getSex().equals(Person.Sex.MAN)
.filter(p -> p.getSex() == Person.Sex.MAN

the same

Comment on lines 74 to 79
.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)))
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
.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)
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
.filter(person -> person.getSex().equals(Person.Sex.WOMAN)
.filter(person -> person.getSex() == Person.Sex.WOMAN

the same

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.

3 participants