Skip to content
Open
Changes from 6 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
34 changes: 32 additions & 2 deletions src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
package core.basesyntax;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remove this empty line - checklist item #1 prohibits empty lines at the beginning of class implementation.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remove this empty line - checklist item #1 prohibits empty lines at the beginning of class implementation.

public class SalaryInfo {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Variable names such as from, to, datum, dataArray, and localDate could be more descriptive. For example, dateFrom, dateTo, dataRecord, recordParts, and workDate would make the logic easier to understand.

private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

formatter should be renamed to follow constant naming conventions, for example DATE_FORMATTER. It is declared as static final, so according to the checklist its name should be uppercase.

Suggested change
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy");

private static final String REPORT = "Report for period ";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remove this empty line - checklist item #1 prohibits empty lines at the beginning of method implementation.

public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
return null;
LocalDate from = LocalDate.parse(dateFrom, formatter);
LocalDate to = LocalDate.parse(dateTo, formatter);

StringBuilder report = new StringBuilder();
report.append(REPORT).append(dateFrom).append(" - ").append(dateTo).append(System.lineSeparator());

for (String name : names) {
int salary = 0;

for (String datum : data) {
String[] dataArray = datum.split(" ");
LocalDate localDate = LocalDate.parse(dataArray[0], formatter);
if (!localDate.isBefore(from)
&& !localDate.isAfter(to)
&& name.equals(dataArray[1])) {
salary += Integer.parseInt(dataArray[2])
* Integer.parseInt(dataArray[3]);
}
Comment on lines +22 to +29

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider extracting the indexes from dataArray into constants instead of using magic numbers 0, 1, 2, and 3. These positions represent date, employee name, worked hours, and hourly rate, so named constants would improve readability.

}
report.append(name).append(" - ").append(salary);
if (!name.equals(names[names.length - 1])) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Avoid checking whether the current employee is the last one using: name.equals(names[names.length - 1]).

This can give incorrect formatting if the names array contains duplicate names. A safer approach is to build each employee line consistently and remove the final line separator at the end, or iterate using an index.

report.append(System.lineSeparator());
}
}
return report.toString();
}
}
}
Loading