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

created SalaryInfo() methods, solved task #1351

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

Good job on making the DateTimeFormatter a constant. This is a good practice as it enhances readability and maintainability of your code.

Choose a reason for hiding this comment

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

The constant formatter should be in uppercase, as per the Java naming conventions for constants. It should look like this: public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy");


public 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.

The method name getSalaryInfo is not very descriptive. Consider renaming it to something more informative, such as calculateSalariesForPeriod.

String dateFrom, String dateTo) {
StringBuilder report = new StringBuilder().append("Report for period ")
.append(dateFrom).append(" - ").append(dateTo);
LocalDate localDateFrom = LocalDate.parse(dateFrom, formatter);
LocalDate localDateTo = LocalDate.parse(dateTo, formatter);
for (String name : names) {
int salary = 0;
for (String line: data) {
String[] dataParts = line.split(" ");
LocalDate localDate = LocalDate.parse(dataParts[0], formatter);

Choose a reason for hiding this comment

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

The variable dataParts is not very descriptive. Consider renaming it to something more informative, such as lineParts or parsedLine.

if ((localDate.isAfter(localDateFrom) || localDate.equals(localDateFrom))
&& (localDate.isBefore(localDateTo)
|| localDate.equals(localDateTo)) && dataParts[1].equals(name)) {

Choose a reason for hiding this comment

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

This condition is a bit complex and hard to read. Consider breaking it down into smaller conditions and using descriptive variable names for them. This will make your code easier to understand.

salary += Integer.parseInt(dataParts[2])
* Integer.parseInt(dataParts[3]);

Choose a reason for hiding this comment

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

These magic numbers 2 and 3 should be declared as constants, as per the checklist. Declare them as constants at the beginning of your class and give them descriptive names.


}
}
report.append(System.lineSeparator()).append(name).append(" - ")
.append(salary);
}
return report.toString();
}
}
Loading