Skip to content

Commit

Permalink
correcting my code
Browse files Browse the repository at this point in the history
  • Loading branch information
FroGitHub committed Jan 17, 2025
1 parent 54356ef commit 2d0550a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
import model.Candidate;

public class CandidateValidator implements Predicate<Candidate> {
private static final int neededAge = 35;
private static final int neededPeriod = 10;

@Override
public boolean test(Candidate candidate) {
if (candidate == null) {
return false;
}
if (!"Ukrainian".equals(candidate.getNationality())) {
if (!candidate.getNationality().equals("Ukrainian")) {
return false;
}
if (candidate.getAge() < 35) {
if (candidate.getAge() < neededAge) {
return false;
}
if (!candidate.isAllowedToVote()) {
Expand All @@ -27,7 +30,7 @@ public boolean test(Candidate candidate) {
.map(Integer::valueOf)
.collect(Collectors.toList());
if (periodInUkraine.size() != 2
|| periodInUkraine.get(1) - periodInUkraine.get(0) < 10) {
|| periodInUkraine.get(1) - periodInUkraine.get(0) < neededPeriod) {
return false;
}
} catch (NumberFormatException | IndexOutOfBoundsException e) {
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -18,6 +19,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)
Expand All @@ -36,7 +40,9 @@ 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 @@ -53,6 +59,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) {
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 @@ -71,6 +80,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) {
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 @@ -84,6 +96,9 @@ 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 @@ -105,6 +120,9 @@ 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 2d0550a

Please sign in to comment.