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

done #1392

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

done #1392

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
60 changes: 58 additions & 2 deletions src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,63 @@
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 DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("dd.MM.yyyy");
private static final int date = 0;
private static final int name = 1;
private static final int hours = 2;
private static final int salary = 3;

public static String getSalaryInfo(String[] names,
String[] data,
String dateFrom,
String dateTo) {

final LocalDate dataFrom = LocalDate
.parse(dateFrom, formatter);
final LocalDate dataTo = LocalDate
.parse(dateTo, formatter);

int[] salariesByName = new int[names.length];

StringBuilder builder = new StringBuilder("Report for period "
+ dateFrom
+ " - "
+ dateTo);

for (String datum : data) {
String[] splitData = datum.split(" ");
LocalDate dataNow = LocalDate.parse(splitData[date], formatter);
if ((dataFrom.isBefore(dataNow)
|| dataFrom.isEqual(dataNow))
&& (dataTo.isAfter(dataNow)
|| dataTo.isEqual(dataNow))) {
calculateSalariesByName(names, splitData, salariesByName);
}
}

for (int k = 0; k < names.length; k++) {
builder.append(System.lineSeparator())
.append(names[k])
.append(" - ")
.append(salariesByName[k]);
}

return builder.toString();
}

private static void calculateSalariesByName(String[] names,
String[] splitData,
int[] salariesByName) {
for (int j = 0; j < names.length; j++) {

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

Maybe you can give me a small hint how i can reduse loops count? Cuz i honestly have no idea how to remove one of that loops

if (splitData[name].equals(names[j])) {
int countOfHours = Integer.parseInt(splitData[hours]);
int hourSalary = Integer.parseInt(splitData[salary]);
salariesByName[j] += countOfHours * hourSalary;
}
}
}
}
Loading