Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completed the task #1377

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/InvalidDataException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax;

public class InvalidDataException extends RuntimeException {
public InvalidDataException(String message) {
super(message);
}
}
51 changes: 50 additions & 1 deletion src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
package core.basesyntax;

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

public class SalaryInfo {

private static final DateTimeFormatter DATES = DateTimeFormatter.ofPattern("dd.MM.yyyy");

public LocalDate parseDate(String date) {
try {
return LocalDate.parse(date, DATES);
} catch (Exception e) {
throw new InvalidDataException(
"Invalid date format. Only dd.MM.yyyy format is accepted"
);
}
}

public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
return null;
if (names == null || data == null || dateFrom == null || dateTo == null) {
throw new InvalidDataException("Invalid data: Please check your input");
}

LocalDate dateFromFormatted = parseDate(dateFrom);
LocalDate dateToFormatted = parseDate(dateTo);

StringBuilder output = new StringBuilder("Report for period "
+ DATES.format(dateFromFormatted)
+ " - "
+ DATES.format(dateToFormatted));

for (String name : names) {
int salary = 0;
for (String part : data) {
String[] dataPart = part.split(" ");
LocalDate datePart = LocalDate.parse(dataPart[0], DATES);

if (dataPart[1].equals(name)
&& !datePart.isBefore(dateFromFormatted)
&& !datePart.isAfter((dateToFormatted))) {
salary += Integer.parseInt(dataPart[2]) * Integer.parseInt(dataPart[3]);
}
}

output.append(System.lineSeparator())
.append(name)
.append(" - ")
.append(salary);

}

return output.toString();
}

}
Loading