-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Release getSalaryInfo #1596
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
base: master
Are you sure you want to change the base?
Release getSalaryInfo #1596
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,38 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| public class SalaryInfo { | ||
| private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); | ||
|
|
||
| public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) { | ||
| return null; | ||
| String regex = "\\s+"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The regex pattern is a constant used throughout the method. Per checklist item #9, extract magic numbers and hardcoded values to constant fields at class level. |
||
| int indexDate = 0; | ||
| int indexName = 1; | ||
| int indexWorkingHour = 2; | ||
| int indexSalary = 3; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Index values 0, 1, 2, 3 are magic numbers. Per checklist item #9, extract these to named constant fields at class level with informative names like INDEX_DATE, INDEX_NAME, etc. |
||
|
|
||
| LocalDate salaryDateFrom = LocalDate.parse(dateFrom, dateTimeFormatter); | ||
| LocalDate salaryDateTo = LocalDate.parse(dateTo, dateTimeFormatter); | ||
| StringBuilder stringBuilder = new StringBuilder(); | ||
|
|
||
| stringBuilder.append("Report for period ").append(dateFrom).append(" - ").append(dateTo); | ||
| for (String name : names) { | ||
| int sumSalary = 0; | ||
| for (String dataEntry : data) { | ||
| String[] parsDatum = dataEntry.split(regex); | ||
| LocalDate currentDate = LocalDate.parse(parsDatum[indexDate], dateTimeFormatter); | ||
| if (!currentDate.isBefore(salaryDateFrom) && !currentDate.isAfter(salaryDateTo) | ||
| && parsDatum[indexName].equals(name)) { | ||
| sumSalary += Integer.parseInt(parsDatum[indexWorkingHour]) | ||
| * Integer.parseInt(parsDatum[indexSalary]); | ||
| } | ||
| } | ||
| stringBuilder.append(System.lineSeparator()) | ||
| .append(name).append(" - ") | ||
| .append(sumSalary); | ||
| } | ||
| return stringBuilder.toString(); | ||
| } | ||
| } | ||
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.
The DateTimeFormatter should be declared as
private static finalper checklist item #2. Add thefinalkeyword to make it a constant field.