Skip to content

Commit

Permalink
Created ComparisonWithDefferentAges.java; Modified CandidateValidator…
Browse files Browse the repository at this point in the history
….java and StreamPractice.java after mentor's checking
  • Loading branch information
Danila2006 committed Jan 16, 2025
1 parent 1e71974 commit 046d440
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
28 changes: 13 additions & 15 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class CandidateValidator implements Predicate<Candidate> {
private static final int MIN_AGE = 35;
private static final int MIN_LIVE_AGE = 10;
private static final String COUNTRY = "Ukrainian";
private static final String DASH = "-";

@Override
public boolean test(Candidate candidate) {
Expand All @@ -19,21 +20,18 @@ public boolean test(Candidate candidate) {

private boolean checkYear(Candidate candidate) {
String yearStr = candidate.getPeriodsInUkr();
try {
int indexOfDash = yearStr.indexOf("-");
if (indexOfDash == -1) {
throw new IllegalArgumentException("Invalid input format. The input string "
+ "must contain a '-' character.");
}

String year1 = yearStr.substring(0, indexOfDash);
String year2 = yearStr.substring(indexOfDash + 1);

int liveAge = Integer.parseInt(year2) - Integer.parseInt(year1);
return liveAge >= MIN_LIVE_AGE;
} catch (NumberFormatException e) {
throw new NumberFormatException("One of the years is not a valid integer."
+ "Input string: " + yearStr);
int indexOfDash = yearStr.indexOf(DASH);

if (indexOfDash == -1) {
throw new IllegalArgumentException("Invalid input format. The input string "
+ "must contain a '-' character.");
}

String year1 = yearStr.substring(0, indexOfDash);
String year2 = yearStr.substring(indexOfDash + 1);

int liveAge = Integer.parseInt(year2) - Integer.parseInt(year1);

return liveAge >= MIN_LIVE_AGE;
}
}
27 changes: 27 additions & 0 deletions src/main/java/practice/ComparisonWithDifferentAges.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package practice;

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

public class ComparisonWithDifferentAges implements Predicate<Person> {
private int fromAge;
private int maleToAge;
private int femaleToAge;

public ComparisonWithDifferentAges(int fromAge, int maleToAge, int femaleToAge) {
this.fromAge = fromAge;
this.maleToAge = maleToAge;
this.femaleToAge = femaleToAge;
}

@Override
public boolean test(Person person) {
if (person.getSex() == Person.Sex.MAN) {
return person.getAge() >= fromAge
&& person.getAge() <= maleToAge;
} else {
return person.getAge() >= fromAge
&& person.getAge() <= femaleToAge;
}
}
}
26 changes: 11 additions & 15 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import model.Candidate;
import model.Cat;
import model.Person;

public class StreamPractice {
private CandidateValidator validator = new CandidateValidator();

/**
* Given list of strings where each element contains 1+ numbers:
* input = {"5,30,100", "0,22,7", ...}
Expand All @@ -20,7 +21,6 @@ public class StreamPractice {
public int findMinEvenNumber(List<String> numbers) {
return numbers.stream()
.flatMap(nums -> Arrays.stream(nums.split(",")))
.map(String::trim)
.mapToInt(Integer::parseInt)
.filter(num -> num % 2 == 0)
.min()
Expand Down Expand Up @@ -51,10 +51,10 @@ public Double getOddNumsAverage(List<Integer> numbers) {
*/
public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toAge) {
return peopleList.stream()
.filter(person -> person.getSex().equals(Person.Sex.MAN)
.filter(person -> person.getSex() == Person.Sex.MAN
&& person.getAge() >= fromAge
&& person.getAge() <= toAge)
.collect(Collectors.toList());
.toList();
}

/**
Expand All @@ -69,14 +69,11 @@ public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toA
*/
public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
int maleToAge, List<Person> peopleList) {
ComparisonWithDifferentAges comparison = new ComparisonWithDifferentAges(fromAge,
maleToAge, femaleToAge);
return peopleList.stream()
.filter(person -> (person.getSex().equals(Person.Sex.MAN)
&& person.getAge() >= fromAge
&& person.getAge() <= maleToAge)
|| (person.getSex().equals(Person.Sex.WOMAN)
&& person.getAge() >= fromAge
&& person.getAge() <= femaleToAge))
.collect(Collectors.toList());
.filter(comparison)
.toList();
}

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

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

0 comments on commit 046d440

Please sign in to comment.