Skip to content

Commit

Permalink
corrected_first_commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Lupen07 committed Oct 24, 2023
1 parent 82e3fea commit 9eef3b6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ public class CandidateValidator implements Predicate<Candidate> {
private static final int END_INDEX = 1;
private static final int MIN_PERIOD_OF_STAY = 10;
private static final String NATIONALITY = "Ukrainian";
private static final String HYPHEN = "-";

@Override
public boolean test(Candidate candidate) {
if (candidate.getPeriodsInUkr() == null) {
return false;
}
String[] periodResidenceInUkraine = candidate.getPeriodsInUkr().split("-");
String[] periodResidenceInUkraine = candidate.getPeriodsInUkr().split(HYPHEN);
return candidate.getAge() >= MIN_AGE && candidate.isAllowedToVote()
&& candidate.getNationality().equals(NATIONALITY)
&& (Integer.parseInt(periodResidenceInUkraine[END_INDEX])
- Integer.parseInt(periodResidenceInUkraine[BEGINNING_INDEX]))
>= MIN_PERIOD_OF_STAY;
}

}
24 changes: 13 additions & 11 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ public class StreamPractice {
* "Can't get min value from list: < Here is our input 'numbers' >"
*/
public int findMinEvenNumber(List<String> numbers) {
final String comma = ",";
return numbers.stream()
.flatMap(num -> Arrays.stream(num.split(",")))
.flatMap(number -> Arrays.stream(number.split(comma)))
.mapToInt(Integer::parseInt)
.filter(n -> n % 2 == 0)
.filter(number -> number % 2 == 0)
.min()
.orElseThrow(() -> new RuntimeException("Can't get min value from list"));
}
Expand All @@ -34,7 +35,7 @@ public int findMinEvenNumber(List<String> numbers) {
public Double getOddNumsAverage(List<Integer> numbers) {
return IntStream.range(0, numbers.size())
.map(i -> (i % 2 == 1) ? numbers.get(i) - 1 : numbers.get(i))
.filter(n -> n % 2 == 1)
.filter(number -> number % 2 == 1)
.average()
.orElseThrow(()
-> new NoSuchElementException("There is no odd numbers in your list"));
Expand All @@ -50,8 +51,8 @@ public Double getOddNumsAverage(List<Integer> numbers) {
*/
public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toAge) {
return peopleList.stream()
.filter(p -> p.getSex() == Person.Sex.MAN
&& p.getAge() >= fromAge && p.getAge() <= toAge)
.filter(person -> person.getSex() == Person.Sex.MAN
&& person.getAge() >= fromAge && person.getAge() <= toAge)
.collect(Collectors.toList());
}

Expand All @@ -68,10 +69,10 @@ 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 peopleList.stream()
.filter(p -> (p.getSex() == Person.Sex.MAN
&& p.getAge() >= fromAge && p.getAge() <= maleToAge)
|| (p.getSex() == Person.Sex.WOMAN
&& p.getAge() >= fromAge && p.getAge() <= femaleToAge))
.filter(person -> (person.getSex() == Person.Sex.MAN
&& person.getAge() >= fromAge && person.getAge() <= maleToAge)
|| (person.getSex() == Person.Sex.WOMAN
&& person.getAge() >= fromAge && person.getAge() <= femaleToAge))
.collect(Collectors.toList());
}

Expand All @@ -82,8 +83,9 @@ public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
*/
public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
return peopleList.stream()
.filter(p -> p.getSex() == Person.Sex.WOMAN && p.getAge() >= femaleAge
&& !p.getCats().isEmpty())
.filter(person -> person.getSex()
== Person.Sex.WOMAN && person.getAge() >= femaleAge
&& !person.getCats().isEmpty())
.flatMap(person -> person.getCats().stream())
.map(Cat::getName)
.collect(Collectors.toList());
Expand Down

0 comments on commit 9eef3b6

Please sign in to comment.