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

create an implementation for the method "getSalaryInfo" #1290

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
54 changes: 52 additions & 2 deletions src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
package core.basesyntax;

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

public class SalaryInfo {
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
return null;
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy");
private static final int DATE_FROM_LIST = 0;
private static final int NAME_FROM_LIST = 1;
private static final int HOURS_FROM_LIST = 2;
private static final int HOURS_RATE_FROM_LIST = 3;
private static final String LINE_SEPARATOR = "\\s+";

public static boolean isDateInRange(String fromDate, String toDate, String dateFromAList) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this method should be private and placed after public methods


try {
LocalDate startDate = LocalDate.parse(fromDate, FORMATTER);
LocalDate endDate = LocalDate.parse(toDate, FORMATTER);
LocalDate dateToCheck = LocalDate.parse(dateFromAList, FORMATTER);
return (!dateToCheck.isBefore(startDate)
&& !dateToCheck.isAfter(endDate));
} catch (DateTimeParseException e) {
System.out.println("Invalid date format" + e);
return false;
}
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't leave an empty line after method/class definition

public String getSalaryInfo(String[] names, String[] dates, String dateFrom, String dateTo) {
StringBuilder builder = new StringBuilder("Report for period ");
builder.append(dateFrom).append(" - ").append(dateTo).append(System.lineSeparator());

try {
for (int i = 0; i < names.length; i++) {
int employeeSalary = 0;
for (String date : dates) {
String[] record = date.split(LINE_SEPARATOR);
String recordDate = record[DATE_FROM_LIST];
String recordName = record[NAME_FROM_LIST];
int totalHours = Integer.parseInt(record[HOURS_FROM_LIST]);
int salaryPerHour = Integer.parseInt(record[HOURS_RATE_FROM_LIST]);

if (recordName.equals(names[i])
&& isDateInRange(dateFrom, dateTo, recordDate)) {
employeeSalary += totalHours * salaryPerHour;
}
}
builder.append(names[i]).append(" - ").append(employeeSalary)
.append(System.lineSeparator());
}
} catch (DateTimeParseException | NumberFormatException e) {
System.out.println("Invalid date format" + e);

}
return builder.toString().trim();
}
}
Loading