Skip to content

Commit

Permalink
Fixed problems v2
Browse files Browse the repository at this point in the history
  • Loading branch information
vDmytriv01 committed Nov 20, 2024
1 parent fb2e785 commit 937abe5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class CandidateValidator implements Predicate<Candidate> {
private static final String NATIONALITY = "Ukrainian";
private static final int MIN_AGE = 35;
private static final int MIN_PERIOD_IN_UKRAINE = 10;
private static final String PERIOD_SEPARATOR = "-";

@Override
public boolean test(Candidate candidate) {
Expand All @@ -17,7 +18,7 @@ && periodMoreThanTen(candidate.getPeriodsInUkr())
}

private boolean periodMoreThanTen(String periodsInUkr) {
String[] period = periodsInUkr.split("-");
String[] period = periodsInUkr.split(PERIOD_SEPARATOR);
int start = Integer.parseInt(period[0]);
int end = Integer.parseInt(period[1]);
return end - start >= MIN_PERIOD_IN_UKRAINE;
Expand Down
29 changes: 17 additions & 12 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.function.Predicate;
import java.util.stream.IntStream;
import model.Candidate;
import model.Cat;
Expand Down Expand Up @@ -54,7 +54,7 @@ public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toA
.filter(person -> person.getSex() == Person.Sex.MAN
&& person.getAge() >= fromAge
&& person.getAge() <= toAge)
.collect(Collectors.toList());
.toList();
}

/**
Expand All @@ -69,14 +69,18 @@ public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toA
*/
public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
int maleToAge, List<Person> peopleList) {

Predicate<Person> isManWithAgeRange = person -> person.getSex() == Person.Sex.MAN
&& person.getAge() >= fromAge
&& person.getAge() <= maleToAge;

Predicate<Person> isWomanWithAgeRange = person -> person.getSex() == Person.Sex.WOMAN
&& person.getAge() >= fromAge
&& person.getAge() <= femaleToAge;

return peopleList.stream()
.filter(person -> Person.Sex.MAN.equals(person.getSex())
&& person.getAge() >= fromAge
&& person.getAge() <= maleToAge
|| Person.Sex.WOMAN.equals(person.getSex())
&& person.getAge() >= fromAge
&& person.getAge() <= femaleToAge)
.collect(Collectors.toList());
.filter(isManWithAgeRange.or(isWomanWithAgeRange))
.toList();
}

/**
Expand All @@ -86,12 +90,12 @@ public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
*/
public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
return peopleList.stream()
.filter(person -> Person.Sex.WOMAN.equals(person.getSex())
.filter(person -> person.getSex() == Person.Sex.WOMAN
&& person.getAge() >= femaleAge
&& person.getCats() != null)
.flatMap(person -> person.getCats().stream())
.map(Cat::getName)
.collect(Collectors.toList());
.toList();
}

/**
Expand All @@ -111,6 +115,7 @@ public List<String> validateCandidates(List<Candidate> candidates) {
.filter(new CandidateValidator())
.map(Candidate::getName)
.sorted()
.collect(Collectors.toList());
.toList();
}

}

0 comments on commit 937abe5

Please sign in to comment.