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

01-jv-salary-info #1330

Open
wants to merge 9 commits 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
52 changes: 50 additions & 2 deletions src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
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 int DATE_STR_INDEX = 0;
private static final int EMPLOYEE_NAME_INDEX = 1;
private static final int HOURS_WORKED_INDEX = 2;
private static final int INCOME_PER_HOUR_INDEX = 3;
private static final DateTimeFormatter DATE_FORMATTER =
DateTimeFormatter.ofPattern("dd.MM.yyyy");

public String getSalaryInfo(String[] names, String[] data,
String dateFrom, String dateTo) {
LocalDate dateFromComparable = convertDateToComparable(dateFrom);
LocalDate dateToComparable = convertDateToComparable(dateTo);
int[] totalEarnings = new int[names.length];

Choose a reason for hiding this comment

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

Don't begin class or method implementation with an empty line.


for (String entry : data) {

Choose a reason for hiding this comment

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

Remember about informative names of variables and methods. The variable 'data' is too generic. A name like 'records' or 'workLogs' would be more descriptive.

String[] parts = entry.split(" ");
String dateStr = parts[DATE_STR_INDEX];
String employeeName = parts[EMPLOYEE_NAME_INDEX];
int hoursWorked = Integer.parseInt(parts[HOURS_WORKED_INDEX]);
int incomePerHour = Integer.parseInt(parts[INCOME_PER_HOUR_INDEX]);
LocalDate entryDateComparable = convertDateToComparable(dateStr);
if (!entryDateComparable.isBefore(dateFromComparable)
&& !entryDateComparable.isAfter(dateToComparable)) {
for (int i = 0; i < names.length; i++) {
if (names[i].equals(employeeName)) {
int earnings = hoursWorked * incomePerHour;
totalEarnings[i] += earnings;
break;
}
}
}
}

StringBuilder report = new StringBuilder();

Choose a reason for hiding this comment

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

Any magic numbers should be constants. The string literals in the report generation, such as 'Report for period ', should be extracted to constant fields with informative names.

report.append("Report for period ").append(dateFrom)
.append(" - ").append(dateTo);

Choose a reason for hiding this comment

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

Use StringBuilder for appending strings in loops. You're already using StringBuilder, which is good, but make sure to use it consistently for all string concatenations within loops.

for (int i = 0; i < names.length; i++) {
Copy link

Choose a reason for hiding this comment

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

third loop, try to do it in two loops

report.append(System.lineSeparator())
.append(names[i]).append(" - ")
.append(totalEarnings[i]);
}

return report.toString();
}

private static LocalDate convertDateToComparable(String date) {
return LocalDate.parse(date, DATE_FORMATTER);
}
}
Loading