-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Task solution #1322
base: master
Are you sure you want to change the base?
Task solution #1322
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,44 @@ | ||
package core.basesyntax; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class SalaryInfo { | ||
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d.MM.yyyy"); | ||
private final int startPositionForSubstringDate = 0; | ||
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. Consider encapsulating the logic for parsing the date from a string into a separate method. This would avoid the need for these constants and make the code more readable. |
||
private final int endPositionForSubstringDate = 10; | ||
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. Magic numbers should be avoided. The positions used for substrings should be constants with informative names to explain their purpose. |
||
private final int startPositionForSubstringIncomeData = 12; | ||
private final int arrayPositionOfWorkingHours = 0; | ||
private final int arrayPositionOfIncomePerHour = 1; | ||
|
||
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) { | ||
return null; | ||
LocalDate localDateFrom = LocalDate.parse(dateFrom, formatter); | ||
LocalDate localDateTo = LocalDate.parse(dateTo, formatter); | ||
|
||
StringBuilder salaryInfo = new StringBuilder(); | ||
salaryInfo.append("Report for period ").append(dateFrom).append(" - ").append(dateTo); | ||
|
||
for (String name : names) { | ||
salaryInfo.append(System.lineSeparator()).append(name).append(" - "); | ||
int salary = 0; | ||
for (String simpleData : data) { | ||
if (simpleData.contains(name)) { | ||
LocalDate rowDate = LocalDate.parse(simpleData.substring( | ||
startPositionForSubstringDate, endPositionForSubstringDate), | ||
formatter); | ||
if ((rowDate.isAfter(localDateFrom) || rowDate.isEqual(localDateFrom)) | ||
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 logic for checking if the date is within the range can be simplified and made more readable by using |
||
&& rowDate.isBefore(localDateTo) || rowDate.isEqual(localDateTo)) { | ||
|
||
String[] dayIncomeInfo = simpleData.substring( | ||
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. Extracting the income data substring can be error-prone and hard to maintain. Consider using a different approach to parse the data, such as using a regular expression or splitting the string into parts and then extracting the relevant information. |
||
startPositionForSubstringIncomeData | ||
+ name.length()).split(" "); | ||
salary += Integer.parseInt(dayIncomeInfo[arrayPositionOfWorkingHours]) | ||
* Integer.parseInt(dayIncomeInfo[arrayPositionOfIncomePerHour]); | ||
} | ||
} | ||
} | ||
salaryInfo.append(salary); | ||
} | ||
return salaryInfo.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.
Since the formatter is a constant and will not change, it should be declared as a static final field and follow the naming conventions for constants (all uppercase letters with underscores). Consider renaming it to 'DATE_FORMATTER'.