Skip to content
Open
Changes from all 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
41 changes: 40 additions & 1 deletion src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
package core.basesyntax;

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

public class SalaryInfo {
private static final DateTimeFormatter DATE_TIME_FORMATTER =
DateTimeFormatter.ofPattern("dd.MM.yyyy");
private static final int DATE_INDEX = 0;
private static final int NAME_INDEX = 1;
private static final int HOURS_INDEX = 2;
private static final int RATE_INDEX = 3;

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

salaryInfo.append("Report for period ")
.append(dateFrom)
.append(" - ")
.append(dateTo)
.append(System.lineSeparator());

for (int i = 0; i < names.length; i++) {
String name = names[i];
int salary = 0;
for (String datum : data) {
String[] parts = datum.split("\\s+");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This likely violates checklist item #9: "Any magic numbers should be constants" because indices parts[0], parts[1], parts[2], parts[3] are hardcoded. Consider introducing named constants for these positions to make the code more self-documenting and avoid magic numbers.

LocalDate currentDate = LocalDate.parse(parts[DATE_INDEX], DATE_TIME_FORMATTER);
if (name.equals(parts[NAME_INDEX])) {
if (!currentDate.isBefore(from) && !currentDate.isAfter(to)) {
salary += Integer.parseInt(parts[HOURS_INDEX])
* Integer.parseInt(parts[RATE_INDEX]);
}
}
}
salaryInfo.append(name).append(" - ").append(salary);

if (i < names.length - 1) {
salaryInfo.append(System.lineSeparator());
}
}
return salaryInfo.toString();
}
}
Loading