Skip to content

Commit

Permalink
correcting my code x3
Browse files Browse the repository at this point in the history
  • Loading branch information
FroGitHub committed Jan 19, 2025
1 parent 9a44c01 commit 9c2b0b4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
15 changes: 6 additions & 9 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
import model.Candidate;

public class CandidateValidator implements Predicate<Candidate> {
// National for being candidate
private static final String neededNational = "Ukrainian";
// Minimum required age for being candidate
private static final int neededAge = 35;
// Minimum required years for being candidate
private static final int neededPeriod = 10;
private static final String requirementNationality = "Ukrainian";
private static final int requirementAge = 35;
private static final int requirementPeriodInUkr = 10;

@Override
public boolean test(Candidate candidate) {
if (candidate == null
|| !candidate.getNationality().equals(neededNational)
|| candidate.getAge() < neededAge
|| !candidate.getNationality().equals(requirementNationality)
|| candidate.getAge() < requirementAge
|| !candidate.isAllowedToVote()) {
return false;
}
Expand All @@ -27,7 +24,7 @@ public boolean test(Candidate candidate) {
.map(Integer::valueOf)
.toList();
if (periodInUkraine.size() != 2
|| periodInUkraine.get(1) - periodInUkraine.get(0) < neededPeriod) {
|| periodInUkraine.get(1) - periodInUkraine.get(0) < requirementPeriodInUkr) {
return false;
}
} catch (NumberFormatException | IndexOutOfBoundsException e) {
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
int maleToAge, List<Person> peopleList) {

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

/**
Expand All @@ -87,14 +88,14 @@ public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {

return peopleList.stream()
.filter(p -> p.getSex() == Person.Sex.WOMAN)
.filter(p -> p.getAge() >= femaleAge)
.flatMap(c -> c.getCats().stream())
.filter(person -> person.getSex() == Person.Sex.WOMAN)
.filter(person -> person.getAge() >= femaleAge)
.flatMap(cat -> cat.getCats().stream())
.map(Cat::getName)
.collect(Collectors.toList());
.toList();
}

/** V - wtf MA, wrong article
/**
* Your help with a election is needed. Given list of candidates, where each element
* has Candidate.class type.
* Check which candidates are eligible to apply for president position and return their
Expand All @@ -112,6 +113,6 @@ public List<String> validateCandidates(List<Candidate> candidates) {
.filter(new CandidateValidator())
.map(Candidate::getName)
.sorted()
.collect(Collectors.toList());
.toList();
}
}

0 comments on commit 9c2b0b4

Please sign in to comment.