Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

f-hw-jv-stream-github-practice #1208

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/main/java/practice/CandidateValidator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
package practice;

public class CandidateValidator {
//write your code here
import java.util.function.Predicate;
import model.Candidate;

public class CandidateValidator implements Predicate<Candidate> {
private static final int FIRST_INDEX_START = 0;
private static final int LAST_INDEX_END = 1;
private static final int MIN_AGE_FOR_CANDIDATE = 35;
private static final int MIN_PERIOD_IN_UKRAINE = 10;
private static final String NATIONALITY_FOR_CANDIDATE = "Ukrainian";
private static final String SPLIT_REGEX = "-";

@Override
public boolean test(Candidate candidate) {
return candidate.getAge() >= MIN_AGE_FOR_CANDIDATE
&& candidate.isAllowedToVote()
&& candidate.getNationality().equals(NATIONALITY_FOR_CANDIDATE)
&& getLivePeriodInUkraine(candidate.getPeriodsInUkr()) >= MIN_PERIOD_IN_UKRAINE;
}

private int getLivePeriodInUkraine(String periodsInUkr) {
String[] split = periodsInUkr.split(SPLIT_REGEX);
int period = 0;
try {
period = Integer.parseInt(split[LAST_INDEX_END])
- Integer.parseInt(split[FIRST_INDEX_START]);
} catch (NumberFormatException e) {
throw new RuntimeException(periodsInUkr + " does not contain a parsable integer");
}
return period;
}
}
51 changes: 44 additions & 7 deletions src/main/java/practice/StreamPractice.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package practice;

import java.util.Collections;
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;

public class StreamPractice {
private final CandidateValidator candidateValidator = new CandidateValidator();

/**
* Given list of strings where each element contains 1+ numbers:
* input = {"5,30,100", "0,22,7", ...}
Expand All @@ -14,7 +20,13 @@ public class StreamPractice {
* "Can't get min value from list: < Here is our input 'numbers' >"
*/
public int findMinEvenNumber(List<String> numbers) {
return 0;
return numbers.stream()
.flatMap(s -> Arrays.stream(s.split(",")))
.mapToInt(Integer::parseInt)
.filter(n -> n % 2 == 0)
.min()
.orElseThrow(() -> new RuntimeException(
"Can't get min value from list:" + numbers));
}

/**
Expand All @@ -23,7 +35,12 @@ public int findMinEvenNumber(List<String> numbers) {
* But before that subtract 1 from each element on an odd position (having the odd index).
*/
public Double getOddNumsAverage(List<Integer> numbers) {
return 0D;
return IntStream.range(0, numbers.size())
.map(index -> index % 2 != 0 ? numbers.get(index) - 1 : numbers.get(index))
.filter(n -> n % 2 != 0)
.average()
.orElseThrow(() -> new NoSuchElementException(
"List of Integer numbers doesn't contains an odd numbers"));
}

/**
Expand All @@ -35,7 +52,10 @@ public Double getOddNumsAverage(List<Integer> numbers) {
* Example: select men who can be recruited to army (from 18 to 27 years old inclusively).
*/
public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toAge) {
return Collections.emptyList();
return peopleList.stream()
.filter(p -> p.getSex().equals(Person.Sex.MAN)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.filter(p -> p.getSex().equals(Person.Sex.MAN)
.filter(p -> p.getSex() == Person.Sex.MAN

the same

&& p.getAge() >= fromAge && p.getAge() <= toAge)
.toList();
}

/**
Expand All @@ -50,7 +70,14 @@ 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 Collections.emptyList();
return peopleList.stream()
.filter(person -> ((person.getSex() == Person.Sex.MAN
&& person.getAge() > fromAge
&& person.getAge() <= maleToAge)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the getWorkablePeople method, the condition person.getAge() > fromAge should be person.getAge() >= fromAge to include the lower bound age for men, as specified in the task description.

|| (person.getSex().equals(Person.Sex.WOMAN)
&& person.getAge() >= fromAge
&& person.getAge() <= femaleToAge)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for complicated conditional, it is better to use Predicate

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how I can do this? if fromAge, femaleToAge and maleToAge is a variable that we accept as input to the method. They are not constant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.filter(person -> ((person.getSex() == Person.Sex.MAN
&& person.getAge() > fromAge
&& person.getAge() <= maleToAge)
|| (person.getSex().equals(Person.Sex.WOMAN)
&& person.getAge() >= fromAge
&& person.getAge() <= femaleToAge)))
Predicate<Person> malePredicate = p -> p.getSex() == Person.Sex.MAN
&& p.getAge() >= fromAge && p.getAge() <= maleToAge;
Predicate<Person> femalePredicate = p -> p.getSex() == Person.Sex.WOMAN
&& p.getAge() >= fromAge && p.getAge() <= femaleToAge;
return peopleList.stream()
.filter(malePredicate.or(femalePredicate))

You can do it in this way

.toList();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method toList() is used here, which is available from Java 16 onwards. If you are using an earlier version of Java, consider using collect(Collectors.toList()) instead.

}

/**
Expand All @@ -59,7 +86,13 @@ public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
* return the names of all cats whose owners are women from `femaleAge` years old inclusively.
*/
public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
return Collections.emptyList();
return peopleList.stream()
.filter(person -> person.getSex().equals(Person.Sex.WOMAN)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.filter(person -> person.getSex().equals(Person.Sex.WOMAN)
.filter(person -> person.getSex() == Person.Sex.WOMAN

the same

&& person.getAge() >= femaleAge)
.map(Person::getCats)
.flatMap(Collection::stream)
.map(Cat::getName)
.toList();
}

/**
Expand All @@ -75,6 +108,10 @@ public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
* parametrized with Candidate in CandidateValidator.
*/
public List<String> validateCandidates(List<Candidate> candidates) {
return Collections.emptyList();
return candidates.stream()
.filter(candidateValidator)
.map(Candidate::getName)
.sorted()
.toList();
}
}
Loading