Skip to content

Commit

Permalink
fixed (almost), need suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
YevheniiVlasenko committed Oct 27, 2023
1 parent b949cb8 commit 6e7fa4d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
12 changes: 9 additions & 3 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ public class CandidateValidator implements Predicate<Candidate> {
private static final int MIN_CANDIDATE_AGE = 35;
private static final int MIN_PERIOD_IN_UKRAINE = 10;

private static final String SPLIT_REGEX_HYPHEN = "-";
private static final int STAR_YEAR_INDEX = 0;
private static final int END_YEAR_INDEX = 1;
private static final String NEEDED_NATIONALITY = "Ukrainian";

@Override
public boolean test(Candidate candidate) {
if (candidate.isAllowedToVote()
&& candidate.getAge() >= MIN_CANDIDATE_AGE
&& candidate.getNationality().equals("Ukrainian")) {
&& candidate.getNationality().equals(NEEDED_NATIONALITY)) {
String[] inUkrYears = candidate.getPeriodsInUkr().split(SPLIT_REGEX_HYPHEN);
int startYear = Integer.parseInt(
candidate.getPeriodsInUkr().split("-")[0]);
inUkrYears[STAR_YEAR_INDEX]);
int endYear = Integer.parseInt(
candidate.getPeriodsInUkr().split("-")[1]);
inUkrYears[END_YEAR_INDEX]);
return endYear - startYear >= MIN_PERIOD_IN_UKRAINE;
}
return false;
Expand Down
39 changes: 22 additions & 17 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@
import model.Candidate;
import model.Cat;
import model.Person;
import model.Person.Sex;

public class StreamPractice {
private static final String NO_MIN_VALUE_EXCEPTION_MESSAGE = "Can't get min value from list: ";
private static final String EMPTY_LIST_EXCEPTION_MESSAGE = "Empty List provided";
private static final String SPLIT_REGEX_COMA = ",";
private final Predicate<Candidate> isAbleToRunForPresident = new CandidateValidator();

public int findMinEvenNumber(List<String> numbers) {
return numbers.stream()
.map(e -> e.split(","))
.map(stringNumber -> stringNumber.split(SPLIT_REGEX_COMA))
.flatMap(Arrays::stream)
.map(Integer::parseInt)
.filter(e -> e % 2 == 0)
.filter(number -> number % 2 == 0)
.sorted()
.findFirst()
.orElseThrow(() -> new RuntimeException(NO_MIN_VALUE_EXCEPTION_MESSAGE + numbers));
}

public Double getOddNumsAverage(List<Integer> numbers) {
return IntStream.range(0, numbers.size())
.map(index -> {
if (index % 2 != 0) {
return numbers.get(index) - 1;
}
return numbers.get(index);
Integer numByIndex = numbers.get(index);
return index % 2 != 0 ? numByIndex - 1 : numByIndex;
})
.filter(number -> number % 2 != 0)
.average()
Expand All @@ -40,29 +40,34 @@ public Double getOddNumsAverage(List<Integer> numbers) {

public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toAge) {
return peopleList.stream()
.filter(person -> person.getAge() >= fromAge && person.getAge() <= toAge)
.filter(person -> person.getSex().name().equals("MAN"))
.filter(person -> {
int personAge = person.getAge();
;
return personAge >= fromAge
&& personAge <= toAge
&& person.getSex().equals(Sex.MAN);
})
.toList();
}

public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
int maleToAge, List<Person> peopleList) {
Predicate<Person> isWorkable = new Predicate<Person>() {
@Override
public boolean test(Person person) {
if (person.getSex().name().equals("MAN")) {
return person.getAge() >= fromAge && person.getAge() <= maleToAge;
}
return person.getAge() >= fromAge && person.getAge() <= femaleToAge;
Predicate<Person> isWorkable = person -> {
int personAge = person.getAge();
if (person.getSex().equals(Sex.MAN)) {
return personAge >= fromAge
&& personAge <= maleToAge;
}
return personAge >= fromAge
&& personAge <= femaleToAge;
};
return peopleList.stream()
.filter(isWorkable)
.toList();
}

public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
Predicate<Person> isNeededCatOwner = person -> person.getSex().name().equals("WOMAN")
Predicate<Person> isNeededCatOwner = person -> person.getSex().equals(Sex.WOMAN)
&& person.getAge() >= femaleAge;
return peopleList.stream()
.filter(isNeededCatOwner)
Expand Down

0 comments on commit 6e7fa4d

Please sign in to comment.