Skip to content

Commit

Permalink
correcting my code x2
Browse files Browse the repository at this point in the history
  • Loading branch information
FroGitHub committed Jan 19, 2025
1 parent 2d0550a commit 9a44c01
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
21 changes: 9 additions & 12 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,29 @@
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
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;

@Override
public boolean test(Candidate candidate) {
if (candidate == null) {
return false;
}
if (!candidate.getNationality().equals("Ukrainian")) {
return false;
}
if (candidate.getAge() < neededAge) {
return false;
}
if (!candidate.isAllowedToVote()) {
if (candidate == null
|| !candidate.getNationality().equals(neededNational)
|| candidate.getAge() < neededAge
|| !candidate.isAllowedToVote()) {
return false;
}
try {
List<Integer> periodInUkraine = Arrays.stream(candidate.getPeriodsInUkr().split("-"))
.map(String::trim)
.map(Integer::valueOf)
.collect(Collectors.toList());
.toList();
if (periodInUkraine.size() != 2
|| periodInUkraine.get(1) - periodInUkraine.get(0) < neededPeriod) {
return false;
Expand Down
29 changes: 7 additions & 22 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package practice;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
Expand All @@ -19,13 +18,9 @@ public class StreamPractice {
* "Can't get min value from list: < Here is our input 'numbers' >"
*/
public int findMinEvenNumber(List<String> numbers) {
if (numbers == null || numbers.isEmpty()) {
throw new RuntimeException("Can't get min value from list");
}

return numbers.stream()
.map(s -> s.split(","))
.flatMap(Arrays::stream)
.map(String::trim)
.flatMap(s -> Arrays.stream(s.split(",")))
.map(Integer::valueOf)
.filter(i -> i % 2 == 0)
.min(Integer::compareTo)
Expand All @@ -40,9 +35,7 @@ public int findMinEvenNumber(List<String> numbers) {
* But before that subtract 1 from each element on an odd position (having the odd index).
*/
public Double getOddNumsAverage(List<Integer> numbers) {
if (numbers == null || numbers.isEmpty()) {
return 0D;
}

return IntStream.range(0, numbers.size())
.map(i -> i % 2 == 1 ? numbers.get(i) - 1 : numbers.get(i))
.filter(num -> num % 2 != 0)
Expand All @@ -59,9 +52,7 @@ public Double getOddNumsAverage(List<Integer> numbers) {
* Example: select men who can be recruited to army (from 18 to 27 years old inclusively).
*/
public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toAge) {
if (peopleList == null || peopleList.isEmpty()) {
return Collections.emptyList();
}

return peopleList.stream()
.filter(p -> p.getAge() >= fromAge && p.getAge() <= toAge)
.filter(p -> p.getSex() == Person.Sex.MAN)
Expand All @@ -80,9 +71,7 @@ public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toA
*/
public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
int maleToAge, List<Person> peopleList) {
if (peopleList == null || peopleList.isEmpty()) {
return Collections.emptyList();
}

return peopleList.stream()
.filter(p -> p.getAge() >= fromAge)
.filter((p -> (p.getSex() == Person.Sex.WOMAN && p.getAge() <= femaleToAge)
Expand All @@ -96,9 +85,7 @@ public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
* return the names of all cats whose owners are women from `femaleAge` years old inclusively.
*/
public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
if (peopleList == null || peopleList.isEmpty()) {
return Collections.emptyList();
}

return peopleList.stream()
.filter(p -> p.getSex() == Person.Sex.WOMAN)
.filter(p -> p.getAge() >= femaleAge)
Expand All @@ -120,9 +107,7 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
* parametrized with Candidate in CandidateValidator.
*/
public List<String> validateCandidates(List<Candidate> candidates) {
if (candidates == null || candidates.isEmpty()) {
return Collections.emptyList();
}

return candidates.stream()
.filter(new CandidateValidator())
.map(Candidate::getName)
Expand Down

0 comments on commit 9a44c01

Please sign in to comment.