Skip to content

Commit

Permalink
hw-exmpl4
Browse files Browse the repository at this point in the history
  • Loading branch information
Anasteisha4 committed Jan 21, 2025
1 parent 8c75919 commit 3cdbbda
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
16 changes: 9 additions & 7 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@
import model.Candidate;

public class CandidateValidator implements Predicate<Candidate> {
private static final String NATIONALITY = "Ukrainian";
private static final int AGE_LIMIT = 35;
private static final int REQUIRED_PERIOD_IN_UKR = 10;
private static final String PERIOD_PATTERN = "\\d{4}-\\d{4}";

@Override
public boolean test(Candidate candidate) {
final String nationality = "Ukrainian";
final int ageLimit = 35;
return candidate.getAge() >= ageLimit
return candidate.getAge() >= AGE_LIMIT
&& candidate.isAllowedToVote()
&& nationality.equals(candidate.getNationality())
&& NATIONALITY.equals(candidate.getNationality())
&& isEligibleResidency(candidate.getPeriodsInUkr());
}

private static boolean isEligibleResidency(String periodInUkr) {
final int requiredPeriodInUkr = 10;
if (periodInUkr == null || !periodInUkr.matches("\\d{4}-\\d{4}")) {
if (periodInUkr == null || !periodInUkr.matches(PERIOD_PATTERN)) {
return false;
}
String[] years = periodInUkr.split("-");
int startYear = Integer.parseInt((years[0]));
int endYear = Integer.parseInt(years[1]);
return endYear - startYear >= requiredPeriodInUkr;
return endYear - startYear >= REQUIRED_PERIOD_IN_UKR;
}
}
11 changes: 3 additions & 8 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class StreamPractice {
public int findMinEvenNumber(List<String> numbers) {
return numbers.stream()
.flatMap(s -> Arrays.stream(s.split(",")))
.map(String::trim)
.map(Integer::parseInt)
.filter(n -> n % 2 == 0)
.min(Integer::compareTo)
Expand Down Expand Up @@ -70,13 +69,9 @@ public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
int maleToAge, List<Person> peopleList) {
return peopleList.stream()
.filter(person -> person.getAge() >= fromAge)
.filter(person -> {
if (person.getSex() == Person.Sex.WOMAN) {
return person.getAge() <= femaleToAge;
} else {
return person.getAge() <= maleToAge;
}
})
.filter(person -> person.getSex() == Person.Sex.WOMAN
? person.getAge() <= femaleToAge
: person.getAge() <= maleToAge)
.toList();
}
/**
Expand Down

0 comments on commit 3cdbbda

Please sign in to comment.