From 4c622af630ba09cf8694755b5fb98b0df71dc866 Mon Sep 17 00:00:00 2001 From: Piotr Hic Date: Sun, 22 Dec 2024 21:53:55 +0100 Subject: [PATCH] v1 --- src/main/java/core/basesyntax/SalaryInfo.java | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/SalaryInfo.java b/src/main/java/core/basesyntax/SalaryInfo.java index 4a34339b5..2bcec6361 100644 --- a/src/main/java/core/basesyntax/SalaryInfo.java +++ b/src/main/java/core/basesyntax/SalaryInfo.java @@ -1,7 +1,41 @@ 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; + + StringBuilder majorSB = new StringBuilder("Report for period"); + String [] dateFromAR = dateFrom.split( " "); + String [] dateToAR = dateTo.split( " "); + String date1 = dateFromAR[dateFromAR.length -2].substring( 1); + String date2 = dateToAR[dateFromAR.length -2].substring(1,dateToAR[dateFromAR.length -2].length()-1); + majorSB.append(date1).append(" - ").append(date2).append(" ").append("\n"); + + final DateTimeFormatter pattern = DateTimeFormatter.ofPattern("dd-MM-yyyy"); + date1 = date1.replace(".","-"); + LocalDate dateFrom1 = LocalDate.parse(date1,pattern); + date2 = date2.replace(".","-"); + LocalDate dateTo1 = LocalDate.parse(date2,pattern); + + for (int i = 0; i < names.length; i++){ + int money = 0; + majorSB.append(names[i]).append(" - "); + for (int j = 0; j < data.length; j++){ + String[] elements = data[j].split(" "); + String workingDay = elements[0]; + workingDay = workingDay.replace(".","-"); + LocalDate workDayDate = LocalDate.parse(workingDay,pattern); + if (workDayDate.compareTo(dateFrom1) >= 0 && workDayDate.compareTo(dateTo1) <= 0){ + if (names[i].equals(elements[1])){ + money += Integer.valueOf (Integer.valueOf(elements[2]) * Integer.valueOf(elements[3])); + } + } + } + majorSB.append(money).append("\n"); + } + return majorSB.toString(); } }