Skip to content

Commit

Permalink
fix problem
Browse files Browse the repository at this point in the history
  • Loading branch information
katrienkraska committed Nov 20, 2024
1 parent c4ac2e1 commit 03ea37f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
17 changes: 11 additions & 6 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package practice;

import model.Candidate;
import java.util.Arrays;
import java.util.function.Predicate;
import model.Candidate;

public class CandidateValidator implements Predicate<Candidate> {
private static final int MIN_AGE = 35;
private static final String REQUIRED_NATIONALITY = "Ukrainian";
private static final String PERIOD_SEPARATOR = ",";
private static final String YEAR_SEPARATOR = "-";
private static final int MIN_YEARS_IN_UKRAINE = 10;

@Override
public boolean test(Candidate candidate) {
return candidate.getAge() >= 35
return candidate.getAge() >= MIN_AGE
&& candidate.isAllowedToVote()
&& "Ukrainian".equals(candidate.getNationality())
&& REQUIRED_NATIONALITY.equals(candidate.getNationality())
&& Arrays.stream(candidate.getPeriodsInUkr()
.split(","))
.split(PERIOD_SEPARATOR))
.anyMatch(period -> {
String[] years = period.split("-");
String[] years = period.split(YEAR_SEPARATOR);
int startYear = Integer.parseInt(years[0]);
int endYear = Integer.parseInt(years[1]);
return endYear - startYear >= 10;
return endYear - startYear >= MIN_YEARS_IN_UKRAINE;
});
}
}
32 changes: 13 additions & 19 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package practice;

import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import model.Candidate;
import model.Cat;
import model.Person;
import java.util.NoSuchElementException;
import java.util.stream.IntStream;
import java.util.stream.Collectors;

public class StreamPractice {
public int findMinEvenNumber(List<String> numbers) {
return numbers.stream()
.flatMap(s -> List.of(s.split(",")).stream())
.flatMap(s -> Arrays.stream(s.split(","))) // Use Arrays.stream directly
.map(Integer::parseInt)
.filter(num -> num % 2 == 0)
.min(Integer::compare)
Expand Down Expand Up @@ -40,19 +41,14 @@ public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toA
.collect(Collectors.toList());
}

public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
int maleToAge, List<Person> peopleList) {
public List<Person> getWorkablePeople(int fromAge, int femaleToAge, int maleToAge, List<Person> peopleList) {
return peopleList.stream()
.filter(person -> {
if (person.getSex() == Person.Sex.MAN) {
return person.getAge() >= fromAge
&& person.getAge() <= maleToAge;
} else if (person.getSex() == Person.Sex.WOMAN) {
return person.getAge() >= fromAge
&& person.getAge() <= femaleToAge;
}
return false;
})
.filter(person -> (person.getSex() == Person.Sex.MAN
&& person.getAge() >= fromAge
&& person.getAge() <= maleToAge)
|| (person.getSex() == Person.Sex.WOMAN
&& person.getAge() >= fromAge
&& person.getAge() <= femaleToAge))
.collect(Collectors.toList());
}

Expand All @@ -66,10 +62,8 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
}

public List<String> validateCandidates(List<Candidate> candidates) {
CandidateValidator validator = new CandidateValidator();

return candidates.stream()
.filter(validator)
.filter(new CandidateValidator())
.map(Candidate::getName)
.sorted()
.collect(Collectors.toList());
Expand Down

0 comments on commit 03ea37f

Please sign in to comment.