Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
package practice;

public class CandidateValidator {
import java.util.function.Predicate;
import model.Candidate;

public class CandidateValidator implements Predicate<Candidate> {
//write your code here
private static final int MIN_ALLOWED_AGE = 35;
private static final String REQUIRED_NATIONALITY = "Ukrainian";
private static final int REQUIRED_YEARS_IN_UKR = 10;

@Override
public boolean test(Candidate candidate) {
if (candidate == null) {
return false;
}
if (candidate.getAge() < MIN_ALLOWED_AGE) {
return false;
}
if (!candidate.isAllowedToVote()) {
return false;
}
if (!REQUIRED_NATIONALITY.equals(candidate.getNationality())) {
return false;
}

String periods = candidate.getPeriodsInUkr();

if (periods == null || periods.trim().isEmpty()) {
return false;
}

int totalYears = 0;

String[] parts = periods.split(",");

for (String part : parts) {
String p = part.trim();
if (p.isEmpty()) {
continue;
}

String[] range = p.split("-");
if (range.length != 2) {
return false;
}

try {
int from = Integer.parseInt(range[0].trim());
int to = Integer.parseInt(range[1].trim());

if (to < from) {
return false;
}

totalYears += (to - from + 1);
} catch (NumberFormatException e) {
return false;
}
}
return totalYears >= REQUIRED_YEARS_IN_UKR;
}
}
51 changes: 44 additions & 7 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package practice;

import java.util.Collections;
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 {
Expand All @@ -14,7 +19,15 @@ public class StreamPractice {
* "Can't get min value from list: < Here is our input 'numbers' >"
*/
public int findMinEvenNumber(List<String> numbers) {
return 0;
return numbers.stream()
.flatMap(s -> Arrays.stream(s.split(",")))
.map(String::trim)
.mapToInt(Integer::parseInt)
.filter(n -> n % 2 == 0)
.min()
.orElseThrow(() ->
new RuntimeException("Can't get min value from list: " + numbers)
);
}

/**
Expand All @@ -23,7 +36,13 @@ 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) {
return 0D;
return IntStream.range(0, numbers.size())
.map(i -> i % 2 == 1
? numbers.get(i) - 1
: numbers.get(i))
.filter(n -> n % 2 != 0)
.average()
.orElseThrow(NoSuchElementException::new);
}

/**
Expand All @@ -35,7 +54,10 @@ 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) {
return Collections.emptyList();
return peopleList.stream()
.filter(p -> p.getSex() == Person.Sex.MAN && p.getAge() >= fromAge
&& p.getAge() <= toAge)
.collect(Collectors.toList());
}

/**
Expand All @@ -50,7 +72,11 @@ public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toA
*/
public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
int maleToAge, List<Person> peopleList) {
return Collections.emptyList();
return peopleList.stream()
.filter(p -> p.getAge() >= fromAge
&& ((p.getSex() == Person.Sex.WOMAN && p.getAge() <= femaleToAge)
|| (p.getSex() == Person.Sex.MAN && p.getAge() <= maleToAge)))
.collect(Collectors.toList());
}

/**
Expand All @@ -59,7 +85,12 @@ 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) {
return Collections.emptyList();
return peopleList.stream()
.filter(p -> p.getSex() == Person.Sex.WOMAN && p.getAge() >= femaleAge)
.flatMap(p -> p.getCats().stream())
.map(Cat::getName)
.distinct()
.collect(Collectors.toList());
}

/**
Expand All @@ -75,6 +106,12 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
* parametrized with Candidate in CandidateValidator.
*/
public List<String> validateCandidates(List<Candidate> candidates) {
return Collections.emptyList();
Predicate<Candidate> validator = new CandidateValidator();

return candidates.stream()
.filter(validator)
.map(Candidate::getName)
.sorted()
.collect(Collectors.toList());
}
}
Loading