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

Task solution #1322

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
39 changes: 38 additions & 1 deletion src/main/java/core/basesyntax/SalaryInfo.java
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");

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'.

private final int startPositionForSubstringDate = 0;

Choose a reason for hiding this comment

The 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;

Choose a reason for hiding this comment

The 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))

Choose a reason for hiding this comment

The 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(localDateFrom) && !rowDate.isAfter(localDateTo), which is equivalent and avoids the need for multiple isEqual calls.

&& rowDate.isBefore(localDateTo) || rowDate.isEqual(localDateTo)) {

String[] dayIncomeInfo = simpleData.substring(

Choose a reason for hiding this comment

The 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();
}
}
Loading