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 4 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
53 changes: 51 additions & 2 deletions src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
package core.basesyntax;

public class SalaryInfo {
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
return null;
static final int intDateStr = 0;
static final int intEmployeeName = 1;
static final int intHoursWorked = 2;
static final int intIncomePerHour = 3;
Copy link

Choose a reason for hiding this comment

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

add private


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.

this method and the next ones shouldn't be static

String dateFrom, String dateTo) {
String dateFromComparable = convertDateToComparable(dateFrom);

Choose a reason for hiding this comment

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

If you create a formatter, make it a constant field. This will help with maintainability and performance if the formatter is used multiple times.

String 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[intDateStr];
String employeeName = parts[intEmployeeName];
int hoursWorked = Integer.parseInt(parts[intHoursWorked]);
int incomePerHour = Integer.parseInt(parts[intIncomePerHour]);

String entryDateComparable = convertDateToComparable(dateStr);

if (entryDateComparable.compareTo(dateFromComparable) >= 0
&& entryDateComparable.compareTo(dateToComparable) <= 0) {
Copy link

Choose a reason for hiding this comment

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

don't use compareTO, use isBefore, isAfter

for (int i = 0; i < names.length; i++) {
if (names[i].equals(employeeName)) {
int earnings = hoursWorked * incomePerHour;
totalEarnings[i] += earnings;
break;
}
}
}
}

return buildReport(names, totalEarnings, dateFrom, dateTo);
}

private static String buildReport(String[] names, int[] totalEarnings,
String dateFrom, String dateTo) {
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();
Copy link

Choose a reason for hiding this comment

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

integrate this into main public method, you still have 3 for loops

}

private static String convertDateToComparable(String date) {

Choose a reason for hiding this comment

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

This method name 'convertDateToComparable' is not very informative. The name should reflect that it's converting a date to a string format that can be compared, something like 'formatDateForComparison' might be clearer.

Choose a reason for hiding this comment

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

Use LocalDate instead of Date. This method is currently relying on string manipulation for date comparison, which is less reliable and less readable than using LocalDate.

String[] parts = date.split("\\.");
return parts[2] + parts[1] + parts[0];

Choose a reason for hiding this comment

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

Magic numbers

Copy link

Choose a reason for hiding this comment

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

not fixed

}
}
Loading