Skip to content

Commit

Permalink
fixed constants
Browse files Browse the repository at this point in the history
  • Loading branch information
BolsunovaNataliia committed Oct 30, 2023
1 parent eb6cd92 commit 363c758
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
13 changes: 9 additions & 4 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
import model.Candidate;

public class CandidateValidator implements Predicate<Candidate> {
private static final int MIN_EDGE_AGE = 35;
private static final String NATIONALITY = "Ukrainian";
private static final String REGEX_DUSH = "-";
private static final int MIN_EDGE_PERIOD_IN_UKR = 10;

@Override
public boolean test(Candidate candidate) {
return candidate.getAge() >= 35
return candidate.getAge() >= MIN_EDGE_AGE
&& candidate.isAllowedToVote()
&& candidate.getNationality().equals("Ukrainian")
&& candidate.getNationality().equals(NATIONALITY)
&& Integer.parseInt(candidate.getPeriodsInUkr()
.substring(candidate.getPeriodsInUkr().indexOf("-") + 1))
.substring(candidate.getPeriodsInUkr().indexOf(REGEX_DUSH) + 1))
- Integer.parseInt(candidate.getPeriodsInUkr()
.substring(0, candidate.getPeriodsInUkr().indexOf("-"))) >= 10;
.substring(0, candidate.getPeriodsInUkr()
.indexOf(REGEX_DUSH))) >= MIN_EDGE_PERIOD_IN_UKR;
}
}
12 changes: 7 additions & 5 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import model.Person;

public class StreamPractice {
private static final String REGEX_COMMA = ",";

/**
* Given list of strings where each element contains 1+ numbers:
* input = {"5,30,100", "0,22,7", ...}
Expand All @@ -21,9 +23,9 @@ public class StreamPractice {
*/
public int findMinEvenNumber(List<String> numbers) {
return numbers.stream()
.flatMap(line -> Stream.of(line.split(",")))
.mapToInt(n -> Integer.parseInt(n))
.filter(n -> Integer.valueOf(n) % 2 == 0)
.flatMap(line -> Stream.of(line.split(REGEX_COMMA)))
.mapToInt(Integer::parseInt)
.filter(n -> n % 2 == 0)
.min()
.orElseThrow(() -> new RuntimeException("Can't get min value from list: "
+ numbers.toString()));
Expand All @@ -38,12 +40,12 @@ public Double getOddNumsAverage(List<Integer> numbers) {
Stream<Integer> oddNumbersStream = IntStream
.range(0, numbers.size())
.filter(index -> index % 2 == 1)
.mapToObj(index -> numbers.get(index))
.mapToObj(numbers::get)
.map(n -> n - 1);
Stream<Integer> everNumbersStream = IntStream
.range(0, numbers.size())
.filter(index -> index % 2 == 0)
.mapToObj(index -> numbers.get(index));
.mapToObj(numbers::get);
return Stream.concat(oddNumbersStream, everNumbersStream)
.filter(n -> n % 2 == 1)
.mapToDouble(n -> n)
Expand Down

0 comments on commit 363c758

Please sign in to comment.