Skip to content

Commit

Permalink
resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
DankevichMisha committed Nov 24, 2024
1 parent 9eea730 commit c7bdc3d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
21 changes: 19 additions & 2 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package practice;

public class CandidateValidator {
//write your code here
import java.util.function.Predicate;
import model.Candidate;

public class CandidateValidator implements Predicate<Candidate> {
private static final int MIN_AGE = 35;
private static final int MIN_YEARS_IN_UKRAINE = 10;
private static final String NATIONALITY = "Ukrainian";
private static final int FROM_YEAR_INDEX = 0;
private static final int TO_YEAR_INDEX = 1;
private static final String LINE_SEPARATOR = "-";

public boolean test (Candidate candidate) {
String[] period = candidate.getPeriodsInUkr().split(LINE_SEPARATOR);
int yearsInUkraine = Integer.parseInt(period[TO_YEAR_INDEX])
- Integer.parseInt(period[FROM_YEAR_INDEX]);
return candidate.getAge() >= MIN_AGE && candidate.isAllowedToVote()
&& candidate.getNationality().equals(NATIONALITY)
&& yearsInUkraine >= MIN_YEARS_IN_UKRAINE;
}
}
49 changes: 41 additions & 8 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
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.IntStream;

import model.Candidate;
import model.Cat;
import model.Person;

public class StreamPractice {
private final CandidateValidator candidateValidator = new CandidateValidator();
/**
* Given list of strings where each element contains 1+ numbers:
* input = {"5,30,100", "0,22,7", ...}
Expand All @@ -14,16 +20,26 @@ 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(str -> Arrays.stream(str.split(",")))
.map(Integer::parseInt)
.filter(n -> n % 2 == 0)
.mapToInt(n -> n)
.min()
.orElseThrow(() -> new RuntimeException("Can`t get min value from list: " + numbers));
}

/**
* Given a List of Integer numbers,
* return the average of all odd numbers from the list or throw NoSuchElementException.
* 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 != 0 ? numbers.get(i) - 1 : numbers.get(i))
.filter(num -> num % 2 != 0)
.average()
.orElseThrow(() -> new NoSuchElementException("Can`t get average value from list : "
+ numbers));
}

/**
Expand All @@ -35,7 +51,11 @@ 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();
Predicate<Person> manFromAgeToAge = p -> p.getSex() == Person.Sex.MAN && p.getAge() >= fromAge
&& p.getAge() <= toAge;
return peopleList.stream()
.filter(manFromAgeToAge)
.toList();
}

/**
Expand All @@ -50,7 +70,12 @@ 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();
Predicate<Person> workablePerson = p -> p.getSex() == Person.Sex.MAN && p.getAge() >= fromAge
&& p.getAge() <= maleToAge || (p.getSex().equals(Person.Sex.WOMAN) && p.getAge() >= fromAge
&& p.getAge() <= femaleToAge);
return peopleList.stream()
.filter(workablePerson)
.toList();
}

/**
Expand All @@ -59,7 +84,11 @@ 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)
.toList();
}

/**
Expand All @@ -75,6 +104,10 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
* parametrized with Candidate in CandidateValidator.
*/
public List<String> validateCandidates(List<Candidate> candidates) {
return Collections.emptyList();
return candidates.stream()
.filter(candidateValidator)
.map(Candidate::getName)
.sorted()
.toList();
}
}

0 comments on commit c7bdc3d

Please sign in to comment.