Skip to content
Open

salary #1589

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
44 changes: 42 additions & 2 deletions src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
package core.basesyntax;

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

public class SalaryInfo {
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
return null;
private static final DateTimeFormatter FORMATTER =
DateTimeFormatter.ofPattern("dd.MM.yyyy");
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) {
StringBuilder result = new StringBuilder();
result.append("Report for period ").append(dateFrom)
.append(" - ").append(dateTo)
.append(System.lineSeparator());

LocalDate fromDate = LocalDate.parse(dateFrom, FORMATTER);
LocalDate toDate = LocalDate.parse(dateTo, FORMATTER);

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

for (String record : data) {
String[] fragments = record.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.

Variable name fragments is somewhat abstract and not very informative; this touches checklist item #8 about informative names. Consider a clearer name like recordParts or dataParts to express what the array contains.

if (name.equals(fragments[NAME_INDEX])) {
LocalDate recordDate =
LocalDate.parse(fragments[0], FORMATTER);

if (!recordDate.isBefore(fromDate)
&& !recordDate.isAfter(toDate)) {
int hours = Integer.parseInt(fragments[HOURS_INDEX]);
int rate = Integer.parseInt(fragments[RATE_INDEX]);
salary += hours * rate;
}
}
}
result.append(name).append(" - ").append(salary)
.append(System.lineSeparator());
}
return result.toString().trim();
}
}
Loading