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

tests fails cuz of stupid separator #1310

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
29 changes: 27 additions & 2 deletions src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
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.

The method implementation should not begin with an empty line. Please remove the empty line at the start of the method.

Comment on lines 6 to 7

Choose a reason for hiding this comment

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

Suggested change
public class SalaryInfo {
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
public class SalaryInfo {
private static final DateTimeFormatter DATE_TIME_FORMATTER =
DateTimeFormatter.ofPattern("dd.MM.yyyy");
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {

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 created here and used multiple times, it should be made a constant field. This will make your code easier to understand and maintain.

Choose a reason for hiding this comment

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

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

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

for (String name : names) {
int salary = 0;
for (String line : data) {
String[] parts = line.split(" ");
if (parts[1].equals(name)) {
LocalDate workDate = LocalDate.parse(parts[0], formatter);
if (workDate.isAfter(startDate.minusDays(1)) && workDate.isBefore(endDate.plusDays(1))) {
int hours = Integer.parseInt(parts[2]);
int rate = Integer.parseInt(parts[3]);
salary += hours * rate;
}
}
}
report.append("\n").append(name).append(" - ").append(Integer.toString(salary));

Choose a reason for hiding this comment

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

Instead of using '\n' for line breaks, use System.lineSeparator() to ensure your code is platform-independent.

Choose a reason for hiding this comment

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

It's better to use StringBuilder for appending strings in a loop rather than using + or Integer.toString(). This is because StringBuilder is more efficient as it doesn't create new objects for each concatenation.

}

return report.toString();
}
}
}
Loading