-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathSalaryInfo.java
More file actions
47 lines (40 loc) · 1.74 KB
/
Copy pathSalaryInfo.java
File metadata and controls
47 lines (40 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package core.basesyntax;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class SalaryInfo {
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(" ");
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();
}
}