Skip to content

Commit

Permalink
improved my solution
Browse files Browse the repository at this point in the history
  • Loading branch information
FroGitHub committed Jan 20, 2025
1 parent b37ef4e commit be5b217
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class CandidateValidator implements Predicate<Candidate> {
private static final String REQUIREMENT_NATIONALITY = "Ukrainian";
private static final int REQUIREMENT_AGE = 35;
private static final int REQUIREMENT_PERIOD_IN_UKR = 10;
private static final String PERIOD_SEPARATOR = "-";

@Override
public boolean test(Candidate candidate) {
Expand All @@ -18,20 +19,22 @@ public boolean test(Candidate candidate) {
|| !candidate.isAllowedToVote()) {
return false;
}

try {
List<Integer> periodInUkraine = Arrays.stream(candidate.getPeriodsInUkr().split("-"))
.map(String::trim)
List<Integer> periodInUkraine = Arrays
.stream(candidate.getPeriodsInUkr().split(PERIOD_SEPARATOR))
.map(Integer::valueOf)
.toList();
if (periodInUkraine.size() != 2
|| periodInUkraine.get(1)
- periodInUkraine.get(0) < REQUIREMENT_PERIOD_IN_UKR) {
return false;
}
} catch (NumberFormatException | IndexOutOfBoundsException e) {
} catch (NumberFormatException e) {
return false;
}
return true;
}

}

20 changes: 13 additions & 7 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import model.Candidate;
import model.Cat;
import model.Person;

public class StreamPractice {

public static final CandidateValidator candidateValidator = new CandidateValidator();

/**
* Given list of strings where each element contains 1+ numbers:
* input = {"5,30,100", "0,22,7", ...}
Expand All @@ -23,7 +27,7 @@ public int findMinEvenNumber(List<String> numbers) {
.flatMap(s -> Arrays.stream(s.split(",")))
.map(Integer::valueOf)
.filter(i -> i % 2 == 0)
.min(Integer::compareTo)
.min(Integer::compareTo) // .min() -> I got error of compilation :(
.orElseThrow(() -> new RuntimeException(
"Can't get min value from list: " + numbers
));
Expand Down Expand Up @@ -54,8 +58,8 @@ public Double getOddNumsAverage(List<Integer> numbers) {
public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toAge) {

return peopleList.stream()
.filter(p -> p.getAge() >= fromAge && p.getAge() <= toAge)
.filter(p -> p.getSex() == Person.Sex.MAN)
.filter(person -> person.getAge() >= fromAge && person.getAge() <= toAge)
.filter(person -> person.getSex() == Person.Sex.MAN)
.collect(Collectors.toList());
}

Expand All @@ -72,11 +76,13 @@ 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> ageAndSexFilter = person ->
(person.getSex() == Person.Sex.WOMAN && person.getAge() <= femaleToAge)
|| (person.getSex() == Person.Sex.MAN && person.getAge() <= maleToAge);

return peopleList.stream()
.filter(person -> person.getAge() >= fromAge)
.filter(person -> (person.getSex() == Person.Sex.WOMAN
&& person.getAge() <= femaleToAge
|| person.getSex() == Person.Sex.MAN && person.getAge() <= maleToAge))
.filter(ageAndSexFilter)
.toList();
}

Expand Down Expand Up @@ -110,7 +116,7 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
public List<String> validateCandidates(List<Candidate> candidates) {

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

0 comments on commit be5b217

Please sign in to comment.