-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathSalaryInfo.java
More file actions
59 lines (46 loc) · 2.14 KB
/
Copy pathSalaryInfo.java
File metadata and controls
59 lines (46 loc) · 2.14 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
52
53
54
55
56
57
58
59
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 FORMATTER_LENGTH = 10;
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
StringBuilder builder = new StringBuilder("Report for period ");
LocalDate startDate = getLocalDate(dateFrom);
LocalDate endDate = getLocalDate(dateTo);
builder.append(dateFrom).append(" - ")
.append(dateTo);
for (String name : names) {
int totalSalary = 0;
for (String record : data) {
if (record.contains(name)) {
LocalDate recordDate = getLocalDate(record.substring(0, FORMATTER_LENGTH));
if ((recordDate.isAfter(startDate) && recordDate.isBefore(endDate)
|| (recordDate.isEqual(startDate) || recordDate.isEqual(endDate)))) {
int salaryPerPeriod = getSalaryPerPeriod(record);
totalSalary += salaryPerPeriod;
}
}
}
builder.append(System.lineSeparator())
.append(name)
.append(" - ")
.append(totalSalary);
}
return builder.toString();
}
private static LocalDate getLocalDate(String date) {
return LocalDate.parse(date, FORMATTER);
}
private static int getSalaryPerPeriod(String record) {
int lastSpaceIndex = record.lastIndexOf(" ");
int secondLastSpaceIndex = record.lastIndexOf(" ", lastSpaceIndex - 1);
int workingHours = Integer.parseInt(
record.substring(secondLastSpaceIndex, lastSpaceIndex)
.trim()
);
int incomePerHour = Integer.parseInt(record.substring(lastSpaceIndex).trim());
return incomePerHour * workingHours;
}
}