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

Solve tasks from Readme file #929

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

DEz-1996
Copy link

No description provided.

Copy link

@MinoDentori MinoDentori left a comment

Choose a reason for hiding this comment

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

Good job! There are some points to check, but they have high occurrence rates.

public boolean test(Candidate candidate) {
return candidate.isAllowedToVote()
&& candidate.getAge() >= MIN_VALID_AGE
&& candidate.getNationality().equals(VALID_NATIONALITY)

Choose a reason for hiding this comment

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

We should build construction in this way to avoid NPE.
VALID_NATIONALITY.equals(candidate.getNationality())

Comment on lines 23 to 25
liveInUkraine.substring(0,liveInUkraine.indexOf(DIVIDER_IN_LIVE_PERIOD)));
int upToYear = Integer.parseInt(
liveInUkraine.substring(liveInUkraine.indexOf(DIVIDER_IN_LIVE_PERIOD) + 1));

Choose a reason for hiding this comment

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

We can make constants for the indexes.

Supplier<RuntimeException> exceptionSupplier = () ->
new RuntimeException("Can't get min value from list: " + numbers);
return numbers.stream()
.map(s -> s.split(NUMBERS_DIVIDER))

Choose a reason for hiding this comment

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

We should use full-named names for the variables. (we know, that it depends on the project, but for this task, it is possible to be rejected in that case, so comment)

Comment on lines 33 to 35
.map(i -> (i % 2 != 0) ? numbers.get(i) - 1 : numbers.get(i))
.filter(n -> n % 2 == 1)
.mapToInt(n -> n)

Choose a reason for hiding this comment

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

We should use full-named names for the variables.

public List<Person> selectMenByAge(List<Person> peopleList, int fromAge, int toAge) {
return Collections.emptyList();
Predicate<Person> predicate = person ->
person.getSex() == Person.Sex.MAN

Choose a reason for hiding this comment

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

We should build construction in this way to avoid NPE and MB use equals.
Person.Sex.MAN == person.getSex()

Copy link
Author

Choose a reason for hiding this comment

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

We compare two enums with equal references, in this case we can use ==

Comment on lines +53 to +54
(person.getSex() == Person.Sex.MAN && person.getAge() <= maleToAge)
|| (person.getSex() == Person.Sex.WOMAN && person.getAge() <= femaleToAge);

Choose a reason for hiding this comment

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

We should build construction in this way to avoid NPE and MB use equals.
Can we create variables to store person.getSex() and person.getAge() values?

Copy link
Author

Choose a reason for hiding this comment

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

We compare two enums with equal references, in this case we can use ==
I think create variables is redundant in this place

return peopleList.stream()
.filter(p -> p.getAge() >= fromAge)
.filter(predicate)
.collect(Collectors.toList());

Choose a reason for hiding this comment

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

Use .toList if it is possible.

.map(Person::getCats)
.flatMap(Collection::stream)
.map(Cat::getName)
.collect(Collectors.toList());

Choose a reason for hiding this comment

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

Use .toList if it is possible.

public List<String> getCatsNames(List<Person> peopleList, int femaleAge) {
return Collections.emptyList();
return peopleList.stream()
.filter(p -> p.getSex() == Person.Sex.WOMAN && p.getAge() >= femaleAge)

Choose a reason for hiding this comment

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

We should build construction in this way to avoid NPE and MB use equals.
Person.Sex.MAN == person.getSex()

Copy link
Author

Choose a reason for hiding this comment

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

(null == enum.value) return boolean false
(null.eauals(enum.value)) throw NPE

.filter(candidateValidator)
.map(Candidate::getName)
.sorted()
.collect(Collectors.toList());

Choose a reason for hiding this comment

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

Use .toList if it is possible.

Copy link

@d1sam d1sam left a comment

Choose a reason for hiding this comment

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

Good job, but some changes should be done.

Comment on lines 24 to 29
int fromYear = Integer.parseInt(
liveInUkraine.substring(START_SUBSTRING_INDEX,
liveInUkraine.indexOf(DIVIDER_IN_LIVE_PERIOD)));
int upToYear = Integer.parseInt(
liveInUkraine.substring(liveInUkraine.indexOf(DIVIDER_IN_LIVE_PERIOD)
+ SLIDER_TO_START_SUBSTRING_INDEX));
Copy link

Choose a reason for hiding this comment

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

please better use .split(DIVIDER_IN_LIVE_PERIOD) to make code more readable.

private static final int START_SUBSTRING_INDEX = 0;
private static final int SLIDER_TO_START_SUBSTRING_INDEX = 1;
private static final String VALID_NATIONALITY = "Ukrainian";
private static final char DIVIDER_IN_LIVE_PERIOD = '-';
Copy link

Choose a reason for hiding this comment

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

Suggested change
private static final char DIVIDER_IN_LIVE_PERIOD = '-';
private static final char DIVIDER_DASH = '-';

.map(string -> string.split(NUMBERS_DIVIDER))
.flatMap(Arrays::stream)
.map(Integer::parseInt)
.filter(number -> number % 2 == 0)
Copy link

Choose a reason for hiding this comment

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

extract i % 2 == 0 to a separate method isEven

return numbers.stream()
.map(string -> string.split(NUMBERS_DIVIDER))
.flatMap(Arrays::stream)
.map(Integer::parseInt)
Copy link

Choose a reason for hiding this comment

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

Suggested change
.map(Integer::parseInt)
.mapToInt(Integer::parseInt)

.flatMap(Arrays::stream)
.map(Integer::parseInt)
.filter(number -> number % 2 == 0)
.min(Comparator.naturalOrder())
Copy link

Choose a reason for hiding this comment

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

Suggested change
.min(Comparator.naturalOrder())
.min()

Copy link

Choose a reason for hiding this comment

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

When you have IntStream Comparator.naturalOrder() is redundant

return Stream.iterate(0, integer -> integer + 1).limit(numbers.size())
.peek(System.out::println)
.map(number -> (number % 2 != 0) ? numbers.get(number) - 1 : numbers.get(number))
.filter(number -> number % 2 == 1)
Copy link

Choose a reason for hiding this comment

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

extract i % 2 == 0 to a separate method isEven

public Double getOddNumsAverage(List<Integer> numbers) {
return 0D;
return Stream.iterate(0, integer -> integer + 1).limit(numbers.size())
.peek(System.out::println)
Copy link

Choose a reason for hiding this comment

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

Suggested change
.peek(System.out::println)

public Double getOddNumsAverage(List<Integer> numbers) {
return 0D;
return Stream.iterate(0, integer -> integer + 1).limit(numbers.size())
Copy link

Choose a reason for hiding this comment

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

Suggested change
return Stream.iterate(0, integer -> integer + 1).limit(numbers.size())
return IntStream.range(0, numbers.size())

.peek(System.out::println)
.map(number -> (number % 2 != 0) ? numbers.get(number) - 1 : numbers.get(number))
.filter(number -> number % 2 == 1)
.mapToInt(number -> number)
Copy link

Choose a reason for hiding this comment

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

Suggested change
.mapToInt(number -> number)

public List<String> validateCandidates(List<Candidate> candidates) {
return Collections.emptyList();
CandidateValidator candidateValidator = new CandidateValidator();
Copy link

Choose a reason for hiding this comment

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

please declare candidateValidator variable in the class-level access

Copy link

@mnyshenko mnyshenko left a comment

Choose a reason for hiding this comment

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

Approved, but pay attention to the comments

Comment on lines +24 to +25
int fromYear = Integer.parseInt(liveInUkraine.split(DIVIDER_DASH)[FROM_YEAR_INDEX]);
int upToYear = Integer.parseInt(liveInUkraine.split(DIVIDER_DASH)[UP_TO_YEAR_INDEX]);

Choose a reason for hiding this comment

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

liveInUkraine.split(DIVIDER_DASH) can be extracted to a separate variable, so we can split only once

public Double getOddNumsAverage(List<Integer> numbers) {
return 0D;
return IntStream.range(0, numbers.size())
.map(number -> (!isEven(number)) ? numbers.get(number) - 1 : numbers.get(number))

Choose a reason for hiding this comment

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

seems to be redundant parentheses (!isEven(number))

Comment on lines +43 to +44
&& person.getAge() >= fromAge
&& person.getAge() <= toAge;

Choose a reason for hiding this comment

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

We should always extract identical expressions into variables

  1. Thus, we avoid code duplication
  2. With current implementation If tomorrow you'll need to have person.getAge() + 10 expression here, you'll need to make changes in two places instead of one

Comment on lines +50 to +51
(person.getSex() == Person.Sex.MAN && person.getAge() <= maleToAge)
|| (person.getSex() == Person.Sex.WOMAN && person.getAge() <= femaleToAge);

Choose a reason for hiding this comment

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

person.getAge() and person.getSex() duplication

public List<Person> getWorkablePeople(int fromAge, int femaleToAge,
int maleToAge, List<Person> peopleList) {
return Collections.emptyList();
Predicate<Person> predicate = person ->

Choose a reason for hiding this comment

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

predicate name is too generic, it's better to give some name that explains what this predicate does

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants