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

Pull_reques_1 #1336

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
44 changes: 43 additions & 1 deletion src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,49 @@
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) {

Choose a reason for hiding this comment

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

Don't begin method implementation with an empty line. Please remove the empty line at the start of the method.

return null;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");

Choose a reason for hiding this comment

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

Since the formatter is used multiple times in your code, it should be declared as a constant field. This will make your code cleaner and easier to maintain. Consider moving it to a static final field at the class level.

LocalDate startDate = LocalDate.parse(dateFrom, formatter);
LocalDate endDate = LocalDate.parse(dateTo, formatter);

int[] employeesSalaries = new int[names.length];

for (String record : data) {

Choose a reason for hiding this comment

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

It's possible to optimize the algorithm by avoiding the nested loop. You can check if the date is within range first and then iterate only once through the names to update the salaries.

String[] partsOfData = record.split(" ");
String dataDate = partsOfData[0];
LocalDate currentDate = LocalDate.parse(dataDate, formatter);
String dataName = partsOfData[1];
int dataHoursWorked = Integer.parseInt(partsOfData[2]);
int dataRatePerHour = Integer.parseInt(partsOfData[3]);

if (currentDate.isEqual(startDate) || currentDate.isAfter(startDate)) {

Choose a reason for hiding this comment

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

You can simplify the if-statement by using !currentDate.isBefore(startDate) instead of currentDate.isEqual(startDate) || currentDate.isAfter(startDate).

if (currentDate.isEqual(endDate) || currentDate.isBefore(endDate)) {

Choose a reason for hiding this comment

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

Similarly, you can simplify the if-statement by using !currentDate.isAfter(endDate) instead of currentDate.isEqual(endDate) || currentDate.isBefore(endDate).

for (int i = 0; i < names.length; i++) {
if (names[i].equals(dataName)) {
employeesSalaries[i] += dataHoursWorked * dataRatePerHour;
}
}
}
}
}

StringBuilder salaryInfoReport = new StringBuilder();
salaryInfoReport.append("Report for period ")
.append(dateFrom).append(" - ")
.append(dateTo)
.append(System.lineSeparator());

for (int i = 0; i < names.length; i++) {
salaryInfoReport.append(names[i])
.append(" - ")
.append(employeesSalaries[i]);
if (i < names.length - 1) {
salaryInfoReport.append(System.lineSeparator());
}
}
return salaryInfoReport.toString();
}
}
Loading