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

Implementation of all methods from tasks and class CandidateValidator #1202

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

Conversation

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

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

Comment on lines 16 to 18
String[] ages = candidate.getPeriodsInUkr().split("-");
boolean liveEnoughInUkraine = Integer.parseInt(ages[1])
- Integer.parseInt(ages[0]) >= 10;

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.

Comment on lines +22 to +23
.flatMap(s -> Arrays.stream(s.split(",")))
.map(Integer::parseInt)

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

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;

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

Choose a reason for hiding this comment

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

you can just use

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

Choose a reason for hiding this comment

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

same for other places

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

Choose a reason for hiding this comment

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

same for other places

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

@Override
public boolean test(Candidate candidate) {
return candidate.getAge() >= MIN_AGE
&& 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"

}

private boolean checkYear(Candidate candidate) {
String[] ages = candidate.getPeriodsInUkr().split("-");

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

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

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)

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

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