From b9885660935351af11e212f7ca296bdca750bfe9 Mon Sep 17 00:00:00 2001 From: Kate Kraska Date: Fri, 25 Oct 2024 18:59:42 +0300 Subject: [PATCH] Initial commit for hw-solution --- src/main/java/core/basesyntax/SalaryInfo.java | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/SalaryInfo.java b/src/main/java/core/basesyntax/SalaryInfo.java index 4a34339b5..11a556ae5 100644 --- a/src/main/java/core/basesyntax/SalaryInfo.java +++ b/src/main/java/core/basesyntax/SalaryInfo.java @@ -1,7 +1,55 @@ 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; + int johnSalary = 0; + int andrewSalary = 0; + int kateSalary = 0; + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + + LocalDate startDate = LocalDate.parse(dateFrom, formatter); + LocalDate endDate = LocalDate.parse(dateTo, formatter); + + for (String record : data) { + String[] parts = record.split(" "); + String dateStr = parts[0]; + String name = parts[1]; + int hours = Integer.parseInt(parts[2]); + int rate = Integer.parseInt(parts[3]); + LocalDate date = LocalDate.parse(dateStr, formatter); + + if (date.isBefore(startDate) || date.isAfter(endDate)) { + continue; + } + + boolean isEmployeeInList = false; + for (String employeeName : names) { + if (employeeName.equals(name)) { + isEmployeeInList = true; + break; + } + } + if (!isEmployeeInList) { + continue; + } + + int salary = hours * rate; + + if (name.equals("John")) { + johnSalary += salary; + } else if (name.equals("Andrew")) { + andrewSalary += salary; + } else if (name.equals("Kate")) { + kateSalary += salary; + } + } + return ("Report for period " + dateFrom + " - " + dateTo + "\n" + + "John - " + johnSalary + "\n" + + "Andrew - " + andrewSalary + "\n" + + "Kate - " + kateSalary); } }