-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathSalaryInfo.java
More file actions
51 lines (42 loc) · 1.89 KB
/
Copy pathSalaryInfo.java
File metadata and controls
51 lines (42 loc) · 1.89 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
48
49
50
51
package core.basesyntax;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class SalaryInfo {
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
LocalDate startDate = LocalDate.parse(dateFrom, formatter);
LocalDate endDate = LocalDate.parse(dateTo, formatter);
int[] salaries = getSalaries(names, data, startDate, endDate);
return getReport(names, startDate, endDate, salaries);
}
private int[] getSalaries(String[] names, String[] data,
LocalDate startDate, LocalDate endDate) {
int[] salaries = new int[names.length];
for (int i = 0; i < names.length; i++) {
for (String userData : data) {
String[] workday = userData.split(" ");
LocalDate workDate = LocalDate.parse(workday[0], formatter);
if (!workDate.isBefore(startDate) && !workDate.isAfter(endDate)) {
if (names[i].equals(workday[1])) {
salaries[i] += Integer.parseInt(workday[2]) * Integer.parseInt(workday[3]);
}
}
}
}
return salaries;
}
private String getReport(String[] names, LocalDate startDate,
LocalDate endDate, int[] salaries) {
StringBuilder report = new StringBuilder("Report for period ")
.append(startDate.format(formatter))
.append(" - ")
.append(endDate.format(formatter));
for (int i = 0; i < names.length; i++) {
report.append(System.lineSeparator())
.append(names[i])
.append(" - ")
.append(salaries[i]);
}
return report.toString();
}
}