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

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

public class SalaryInfo {
private static final String DATE_FORMAT_PATTERN = "dd.MM.yyyy";
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT_PATTERN);

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

result.append("Report for period ")
.append(dateFrom)
.append(" - ")
.append(dateTo);

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

for (String line : data) {
String[] info = line.split(" ");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The magic number " " (space character used for splitting data strings) should be extracted as a private static final constant with an informative name, as required by checklist item #8.

LocalDate date = LocalDate.parse(info[0], formatter);

if (info[1].equals(name) && !date.isBefore(from) && !date.isAfter(to)) {
salary += Integer.parseInt(info[2]) * Integer.parseInt(info[3]);
}

}

result.append(System.lineSeparator())
.append(name)
.append(" - ")
.append(salary);
}

return result.toString();
}
}
Loading