Skip to content

Commit

Permalink
adding validation to validateCandidates and findMinEvenNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
Rzarcik97 committed Dec 4, 2024
1 parent 0e20d9b commit dc15f62
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
17 changes: 12 additions & 5 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package practice;

import model.Candidate;

import java.util.function.Predicate;
import model.Candidate;

public class CandidateValidator implements Predicate<Candidate> {

@Override
public boolean test(Candidate candidate) {
String[] PeriodsInUkr = candidate.getPeriodsInUkr().split("-");
periodsInUkrValidator(candidate);
String[] periodsInUkr = candidate.getPeriodsInUkr().split("-");
return candidate.getAge() >= 35
&& candidate.getNationality().equals("Ukrainian")
&& candidate.isAllowedToVote()
&& Integer.parseInt(PeriodsInUkr[1]) - Integer.parseInt(PeriodsInUkr[0]) > 10;
&& Integer.parseInt(periodsInUkr[1]) - Integer.parseInt(periodsInUkr[0]) > 10;
}

private void periodsInUkrValidator(Candidate candidate) {
for (int i = 0; i < candidate.getPeriodsInUkr().length(); i++) {
if (Character.isLetter(candidate.getPeriodsInUkr().charAt(i))) {
throw new RuntimeException("Data contains letters");
}
}
}
//write your code here
}
28 changes: 22 additions & 6 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package practice;

import java.util.*;
import java.util.function.Predicate;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.IntStream;

import model.Candidate;
import model.Cat;
import model.Person;
Expand All @@ -17,11 +18,13 @@ public class StreamPractice {
* "Can't get min value from list: < Here is our input 'numbers' >"
*/
public int findMinEvenNumber(List<String> numbers) {
dataValidatorForFindMinEvenNumber(numbers);
return numbers.stream().map(s -> s.split(","))
.flatMap(Arrays::stream)
.mapToInt(Integer::valueOf)
.filter(i -> i % 2 == 0)
.min().orElseThrow(() -> new RuntimeException("Can't get min value from list: " + numbers));
.min().orElseThrow(() -> new RuntimeException("Can't get min value from list: "
+ numbers));
}

/**
Expand All @@ -47,7 +50,9 @@ public Double getOddNumsAverage(List<Integer> numbers) {
*/
public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toAge) {
return peopleList.stream()
.filter(i -> i.getAge() >= fromAge && i.getAge() <= toAge && i.getSex().equals(Person.Sex.MAN))
.filter(i -> i.getAge() >= fromAge
&& i.getAge() <= toAge
&& i.getSex().equals(Person.Sex.MAN))
.toList();
}

Expand Down Expand Up @@ -77,7 +82,7 @@ public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
*/
public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
return peopleList.stream()
.filter(i -> i.getAge() > femaleAge && i.getSex().equals(Person.Sex.WOMAN))
.filter(i -> i.getAge() >= femaleAge && i.getSex().equals(Person.Sex.WOMAN))
.map(Person::getCats)
.flatMap(Collection::stream)
.map(Cat::getName)
Expand All @@ -104,4 +109,15 @@ public List<String> validateCandidates(List<Candidate> candidates) {
.sorted()
.toList();
}

private void dataValidatorForFindMinEvenNumber(List<String> data) {
for (String datas : data) {
for (int i = 0; i < datas.length(); i++) {
if (Character.isLetter(datas.charAt(i))) {
throw new RuntimeException("Data contains letters");

}
}
}
}
}

0 comments on commit dc15f62

Please sign in to comment.