Skip to content
Open

done #1411

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
25 changes: 23 additions & 2 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
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_leaving_years = 10;
private static final int min_Age = 35;
private static final String nationality = "Ukrainian";

private static boolean validateYears(String years) {
if (years == null) {
throw new RuntimeException("Years of leaving is missing");
}
String[] year = years.split("-");
return (Integer.parseInt(year[1]) - Integer.parseInt(year[0])) >= min_leaving_years;
}

@Override
public boolean test(Candidate candidate) {
return validateYears(candidate.getPeriodsInUkr())
&& candidate.getAge() >= min_Age
&& candidate.getNationality().equals(nationality)
&& candidate.isAllowedToVote();
}
}
62 changes: 55 additions & 7 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package practice;

import java.util.Collections;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import model.Candidate;
import model.Cat;
import model.Person;

public class StreamPractice {
Expand All @@ -14,7 +17,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()
.map(number -> number.split(","))
.flatMap(Arrays::stream)
.map(Integer::parseInt)
.filter(number -> number % 2 == 0)
.reduce(Integer::min)
.orElseThrow(() -> new RuntimeException(
String.format("Can't get min value from list: %s", numbers)
));
}

/**
Expand All @@ -23,7 +34,19 @@ 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;
AtomicInteger index = new AtomicInteger(0);
return numbers.stream()
.map(numb -> {
int ind = index.getAndIncrement();
if (ind % 2 == 1) {
return numb - 1;
}
return numb;
})
.filter(num -> num % 2 == 1)
.mapToInt(Integer::intValue)
.average()
.orElseThrow();
}

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

/**
Expand All @@ -50,7 +77,17 @@ 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(person -> person.getAge() >= fromAge)
.filter(person -> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checklist item #X violation: Use equals() for Enum value comparisons instead of ==. The Sex enum should be compared using .equals() method.

if (person.getSex().equals(Person.Sex.WOMAN)) {
return person.getAge() <= femaleToAge;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checklist item #X violation: Use equals() for Enum value comparisons instead of ==. The Sex enum should be compared using .equals() method.

if (person.getSex().equals(Person.Sex.MAN)) {
return person.getAge() <= maleToAge;
}
return false;
}).collect(Collectors.toList());
}

/**
Expand All @@ -59,7 +96,13 @@ 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(person -> person.getSex().equals(Person.Sex.WOMAN)
&& person.getAge() >= femaleAge)
.map(Person::getCats)
.flatMap(List::stream)
.map(Cat::getName)
.collect(Collectors.toList());
}

/**
Expand All @@ -75,6 +118,11 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
* parametrized with Candidate in CandidateValidator.
*/
public List<String> validateCandidates(List<Candidate> candidates) {
return Collections.emptyList();
CandidateValidator validator = new CandidateValidator();
return candidates.stream()
.filter(validator)
.map(Candidate::getName)
.sorted()
.collect(Collectors.toList());
}
}
Loading