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

Initial commit. #1380

Open
wants to merge 3 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
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;
import java.util.Objects;

public class SalaryInfo {
private static final int DATE = 0;
private static final int NAME = 1;
private static final int HOURS = 2;
private static final int INCOME = 3;
private static final int DAY = 1;

Choose a reason for hiding this comment

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

The DAY constant is incorrectly defined as 1. This constant is not necessary for the logic and should be removed to avoid confusion.

private static final String DASH = " - ";
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy");

public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
return null;
LocalDate dateFromDateFormat = changeDateFormat(dateFrom);
LocalDate dateToDateFormat = changeDateFormat(dateTo);
int[] salary = new int[names.length];

for (String dataRow : data) {
String[] dataInfo = dataRow.split(" ");
if (changeDateFormat(dataInfo[DATE]).isAfter(dateFromDateFormat.minusDays(DAY))
&& changeDateFormat(dataInfo[DATE]).isBefore(dateToDateFormat.plusDays(DAY))) {

Choose a reason for hiding this comment

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

The date range check should ensure inclusivity by using !isBefore(dateFromDateFormat) and !isAfter(dateToDateFormat) instead of adjusting the dates with minusDays and plusDays.

for (int i = 0; i < names.length; i++) {
if (Objects.equals(names[i], dataInfo[NAME])) {
salary[i] += Integer.parseInt(dataInfo[HOURS])
* Integer.parseInt(dataInfo[INCOME]);
}
}
}
}
StringBuilder result = new StringBuilder("Report for period " + dateFrom
+ " - " + dateTo + System.lineSeparator());
for (int i = 0; i < names.length; i++) {
if (i != names.length - 1) {
result.append(names[i]).append(DASH).append(salary[i])
.append(System.lineSeparator());
} else {
result.append(names[i]).append(DASH).append(salary[i]);
}
}
return result.toString();
}

public LocalDate changeDateFormat(String stringDate) {
return LocalDate.parse(stringDate, FORMATTER);
}
}
Loading