Skip to content

Commit

Permalink
Methods created and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaHMK committed Dec 20, 2024
1 parent efce797 commit 2ae9dd2
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
package core.basesyntax;

import java.time.format.DateTimeFormatter;
import java.time.LocalDate;

public class SalaryInfo {
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
return null;
StringBuilder result = new StringBuilder();
result.append("Report for period ").append(dateFrom).append(" - ").append(dateTo)
.append(System.lineSeparator());
for (String name : names) {
int userSalary = 0;
for (String row : data) {
String[] dataSplit = row.split(" ");
String foundDate = dataSplit[0];
String foundName = dataSplit[1];
String foundHours = dataSplit[2];
String foundHourSalary = dataSplit[3];
if (name.equals(foundName) && isDateIncluded(dateFrom, dateTo, foundDate)) {
userSalary += Integer.parseInt(foundHours) * Integer.parseInt(foundHourSalary);
}
}
result.append(name).append(" - ").append(userSalary)
.append(System.lineSeparator());
}
return result.toString().trim();
}

public boolean isDateIncluded(String dateFrom, String dateTo, String searchDate) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate beginDate = LocalDate.parse(dateFrom, formatter);
LocalDate endDate = LocalDate.parse(dateTo, formatter);
LocalDate targetDate = LocalDate.parse(searchDate, formatter);
return targetDate.isEqual(beginDate) || targetDate.isEqual(endDate)
|| targetDate.isAfter(beginDate) && targetDate.isBefore(endDate);
}
}

0 comments on commit 2ae9dd2

Please sign in to comment.