Skip to content

Commit

Permalink
resolve2
Browse files Browse the repository at this point in the history
  • Loading branch information
DankevichMisha committed Nov 24, 2024
1 parent c7bdc3d commit df93766
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class CandidateValidator implements Predicate<Candidate> {
private static final int TO_YEAR_INDEX = 1;
private static final String LINE_SEPARATOR = "-";

public boolean test (Candidate candidate) {
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]);
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.NoSuchElementException;
import java.util.function.Predicate;
import java.util.stream.IntStream;

import model.Candidate;
import model.Cat;
import model.Person;
Expand All @@ -19,27 +18,30 @@ public class StreamPractice {
* If there is no needed data throw RuntimeException with message
* "Can't get min value from list: < Here is our input 'numbers' >"
*/

public int findMinEvenNumber(List<String> numbers) {
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));
.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 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));
.orElseThrow(() -> new NoSuchElementException("Can`t get average "
+ "value from list : " + numbers));
}

/**
Expand All @@ -51,8 +53,9 @@ 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) {
Predicate<Person> manFromAgeToAge = p -> p.getSex() == Person.Sex.MAN && p.getAge() >= fromAge
&& p.getAge() <= toAge;
Predicate<Person> manFromAgeToAge = p -> p.getSex() == Person.Sex.MAN
&& p.getAge() >= fromAge
&& p.getAge() <= toAge;
return peopleList.stream()
.filter(manFromAgeToAge)
.toList();
Expand All @@ -70,8 +73,9 @@ 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> workablePerson = p -> p.getSex() == Person.Sex.MAN && p.getAge() >= fromAge
&& p.getAge() <= maleToAge || (p.getSex().equals(Person.Sex.WOMAN) && p.getAge() >= fromAge
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)
Expand Down

0 comments on commit df93766

Please sign in to comment.