|
1 | 1 | package core.basesyntax; |
2 | 2 |
|
| 3 | +import java.time.LocalDate; |
| 4 | +import java.time.format.DateTimeFormatter; |
| 5 | + |
3 | 6 | public class SalaryInfo { |
4 | 7 | public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) { |
5 | | - return null; |
| 8 | + private static final String REGEX = "\\s+"; |
| 9 | + private static final int INDEX_DATE = 0; |
| 10 | + private static final int INDEX_NAME = 1; |
| 11 | + private static final int INDEX_WORKING_HOUR = 2; |
| 12 | + private static final int INDEX_SALARY = 3; |
| 13 | + private static final String DATE_FORMAT = "dd.MM.yyyy"; |
| 14 | + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT); |
| 15 | + LocalDate salaryDateFrom = LocalDate.parse(dateFrom, dateTimeFormatter); |
| 16 | + LocalDate salaryDateTo = LocalDate.parse(dateTo, dateTimeFormatter); |
| 17 | + StringBuilder stringBuilder = new StringBuilder(); |
| 18 | + |
| 19 | + stringBuilder.append("Report for period ").append(dateFrom).append(" - ").append(dateTo); |
| 20 | + for (String name : names) { |
| 21 | + int sumSalary = 0; |
| 22 | + for (String datum : data) { |
| 23 | + String[] parsDatum = datum.split(REGEX); |
| 24 | + LocalDate currentDate = LocalDate.parse(parsDatum[INDEX_DATE], dateTimeFormatter); |
| 25 | + if (!currentDate.isBefore(salaryDateFrom) && !currentDate.isAfter(salaryDateTo) |
| 26 | + && parsDatum[INDEX_NAME].equals(name)) { |
| 27 | + sumSalary += Integer.parseInt(parsDatum[INDEX_WORKING_HOUR]) |
| 28 | + * Integer.parseInt(parsDatum[INDEX_SALARY]); |
| 29 | + } |
| 30 | + } |
| 31 | + if (sumSalary >= 0) { |
| 32 | + stringBuilder.append(System.lineSeparator()) |
| 33 | + .append(name).append(" - ") |
| 34 | + .append(sumSalary); |
| 35 | + } |
| 36 | + } |
| 37 | + return stringBuilder.toString(); |
6 | 38 | } |
7 | 39 | } |
0 commit comments