-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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) |
There was a problem hiding this comment.
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())
liveInUkraine.substring(0,liveInUkraine.indexOf(DIVIDER_IN_LIVE_PERIOD))); | ||
int upToYear = Integer.parseInt( | ||
liveInUkraine.substring(liveInUkraine.indexOf(DIVIDER_IN_LIVE_PERIOD) + 1)); |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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)
.map(i -> (i % 2 != 0) ? numbers.get(i) - 1 : numbers.get(i)) | ||
.filter(n -> n % 2 == 1) | ||
.mapToInt(n -> n) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
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 ==
(person.getSex() == Person.Sex.MAN && person.getAge() <= maleToAge) | ||
|| (person.getSex() == Person.Sex.WOMAN && person.getAge() <= femaleToAge); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
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)); |
There was a problem hiding this comment.
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 = '-'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.map(Integer::parseInt) | |
.mapToInt(Integer::parseInt) |
.flatMap(Arrays::stream) | ||
.map(Integer::parseInt) | ||
.filter(number -> number % 2 == 0) | ||
.min(Comparator.naturalOrder()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.min(Comparator.naturalOrder()) | |
.min() |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.peek(System.out::println) |
public Double getOddNumsAverage(List<Integer> numbers) { | ||
return 0D; | ||
return Stream.iterate(0, integer -> integer + 1).limit(numbers.size()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.mapToInt(number -> number) |
public List<String> validateCandidates(List<Candidate> candidates) { | ||
return Collections.emptyList(); | ||
CandidateValidator candidateValidator = new CandidateValidator(); |
There was a problem hiding this comment.
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
There was a problem hiding this 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
int fromYear = Integer.parseInt(liveInUkraine.split(DIVIDER_DASH)[FROM_YEAR_INDEX]); | ||
int upToYear = Integer.parseInt(liveInUkraine.split(DIVIDER_DASH)[UP_TO_YEAR_INDEX]); |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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))
&& person.getAge() >= fromAge | ||
&& person.getAge() <= toAge; |
There was a problem hiding this comment.
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
- Thus, we avoid code duplication
- 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
(person.getSex() == Person.Sex.MAN && person.getAge() <= maleToAge) | ||
|| (person.getSex() == Person.Sex.WOMAN && person.getAge() <= femaleToAge); |
There was a problem hiding this comment.
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 -> |
There was a problem hiding this comment.
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
No description provided.