-
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
Pull_reques_1 #1336
base: master
Are you sure you want to change the base?
Pull_reques_1 #1336
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,49 @@ | ||
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; | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); | ||
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. Since the formatter is used multiple times in your code, it should be declared as a constant field. This will make your code cleaner and easier to maintain. Consider moving it to a static final field at the class level. |
||
LocalDate startDate = LocalDate.parse(dateFrom, formatter); | ||
LocalDate endDate = LocalDate.parse(dateTo, formatter); | ||
|
||
int[] employeesSalaries = new int[names.length]; | ||
|
||
for (String record : data) { | ||
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. It's possible to optimize the algorithm by avoiding the nested loop. You can check if the date is within range first and then iterate only once through the names to update the salaries. |
||
String[] partsOfData = record.split(" "); | ||
String dataDate = partsOfData[0]; | ||
LocalDate currentDate = LocalDate.parse(dataDate, formatter); | ||
String dataName = partsOfData[1]; | ||
int dataHoursWorked = Integer.parseInt(partsOfData[2]); | ||
int dataRatePerHour = Integer.parseInt(partsOfData[3]); | ||
|
||
if (currentDate.isEqual(startDate) || currentDate.isAfter(startDate)) { | ||
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. You can simplify the if-statement by using |
||
if (currentDate.isEqual(endDate) || currentDate.isBefore(endDate)) { | ||
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. Similarly, you can simplify the if-statement by using |
||
for (int i = 0; i < names.length; i++) { | ||
if (names[i].equals(dataName)) { | ||
employeesSalaries[i] += dataHoursWorked * dataRatePerHour; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
StringBuilder salaryInfoReport = new StringBuilder(); | ||
salaryInfoReport.append("Report for period ") | ||
.append(dateFrom).append(" - ") | ||
.append(dateTo) | ||
.append(System.lineSeparator()); | ||
|
||
for (int i = 0; i < names.length; i++) { | ||
salaryInfoReport.append(names[i]) | ||
.append(" - ") | ||
.append(employeesSalaries[i]); | ||
if (i < names.length - 1) { | ||
salaryInfoReport.append(System.lineSeparator()); | ||
} | ||
} | ||
return salaryInfoReport.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.
Don't begin method implementation with an empty line. Please remove the empty line at the start of the method.