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

Implemented method getSalaryInfo #1333

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 34 additions & 2 deletions src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
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;
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter
.ofPattern("dd.MM.yyyy");
private static final int DATE_INDEX = 0;
private static final int NAME_INDEX = 1;
private static final int HOURS_INDEX = 2;
private static final int PER_HOUR_INDEX = 3;

public static String getSalaryInfo(String[] names, String[] data,

Choose a reason for hiding this comment

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

Remember to avoid beginning a method implementation with an empty line. In Java, it's a common practice to start the method body immediately after the method signature without an empty line to keep the code compact and readable.

String dateFrom, String dateTo) {
LocalDate fromDate = LocalDate.parse(dateFrom, DATE_FORMATTER);
LocalDate toDate = LocalDate.parse(dateTo, DATE_FORMATTER);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Report for period ").append(dateFrom)

Choose a reason for hiding this comment

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

To ensure consistency across different operating systems, use System.lineSeparator() instead of hardcoded spaces when concatenating strings that are meant to be on separate lines.

.append(" - ").append(dateTo);
for (String name : names) {
int salary = 0;
for (String info : data) {
String[] parts = info.split(" ");

Choose a reason for hiding this comment

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

Consider using more informative variable names. The name 'parts' is quite generic and does not convey the meaning of the data it holds. A name like 'recordParts' or 'workRecord' might be more descriptive.

LocalDate workingDays = LocalDate.parse(parts[DATE_INDEX], DATE_FORMATTER);
String employeeName = parts[NAME_INDEX];
int workedHours = Integer.parseInt(parts[HOURS_INDEX]);
int payPerHour = Integer.parseInt(parts[PER_HOUR_INDEX]);
if (employeeName.equals(name)
&& (workingDays.isAfter(fromDate) || workingDays.isEqual(fromDate))
&& (workingDays.isBefore(toDate) || workingDays.isEqual(toDate))) {
Comment on lines +29 to +31
Copy link

Choose a reason for hiding this comment

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

think how you can shorten this to just two checks instead of for (use ! isBefore, ! isAfter)

salary += payPerHour * workedHours;
}
}
stringBuilder.append(System.lineSeparator()).append(name).append(" - ").append(salary);

Choose a reason for hiding this comment

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

Use System.lineSeparator() for consistency across different operating systems instead of relying on the default behavior of StringBuilder.append() which might not always use the correct line separator.

}
return stringBuilder.toString();
}
}
Loading