-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Implemented method getSalaryInfo #1333
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,39 @@ | ||
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 DATE_FORMATTER = DateTimeFormatter | ||
.ofPattern("dd.MM.yyyy"); | ||
private static final int DATE_INDEX = 0; | ||
private static final int NAME_INDEX = 1; | ||
private static final int HOURS_INDEX = 2; | ||
private static final int PER_HOUR_INDEX = 3; | ||
|
||
public static String getSalaryInfo(String[] names, String[] data, | ||
String dateFrom, String dateTo) { | ||
LocalDate fromDate = LocalDate.parse(dateFrom, DATE_FORMATTER); | ||
LocalDate toDate = LocalDate.parse(dateTo, DATE_FORMATTER); | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
stringBuilder.append("Report for period ").append(dateFrom) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To ensure consistency across different operating systems, use |
||
.append(" - ").append(dateTo); | ||
for (String name : names) { | ||
int salary = 0; | ||
for (String info : data) { | ||
String[] parts = info.split(" "); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using more informative variable names. The name 'parts' is quite generic and does not convey the meaning of the data it holds. A name like 'recordParts' or 'workRecord' might be more descriptive. |
||
LocalDate workingDays = LocalDate.parse(parts[DATE_INDEX], DATE_FORMATTER); | ||
String employeeName = parts[NAME_INDEX]; | ||
int workedHours = Integer.parseInt(parts[HOURS_INDEX]); | ||
int payPerHour = Integer.parseInt(parts[PER_HOUR_INDEX]); | ||
if (employeeName.equals(name) | ||
&& (workingDays.isAfter(fromDate) || workingDays.isEqual(fromDate)) | ||
&& (workingDays.isBefore(toDate) || workingDays.isEqual(toDate))) { | ||
Comment on lines
+29
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. think how you can shorten this to just two checks instead of for (use ! isBefore, ! isAfter) |
||
salary += payPerHour * workedHours; | ||
} | ||
} | ||
stringBuilder.append(System.lineSeparator()).append(name).append(" - ").append(salary); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
} | ||
return stringBuilder.toString(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remember to avoid beginning a method implementation with an empty line. In Java, it's a common practice to start the method body immediately after the method signature without an empty line to keep the code compact and readable.