-
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
created SalaryInfo() methods, solved task #1351
base: master
Are you sure you want to change the base?
Changes from 3 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,33 @@ | ||
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; | ||
public static final 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. The constant |
||
|
||
public String getSalaryInfo(String[] names, String[] 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. The method name |
||
String dateFrom, String dateTo) { | ||
StringBuilder report = new StringBuilder().append("Report for period ") | ||
.append(dateFrom).append(" - ").append(dateTo); | ||
LocalDate localDateFrom = LocalDate.parse(dateFrom, formatter); | ||
LocalDate localDateTo = LocalDate.parse(dateTo, formatter); | ||
for (String name : names) { | ||
int salary = 0; | ||
for (String line: data) { | ||
String[] dataParts = line.split(" "); | ||
LocalDate localDate = LocalDate.parse(dataParts[0], formatter); | ||
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. The variable |
||
if ((localDate.isAfter(localDateFrom) || localDate.equals(localDateFrom)) | ||
&& (localDate.isBefore(localDateTo) | ||
|| localDate.equals(localDateTo)) && dataParts[1].equals(name)) { | ||
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. This condition is a bit complex and hard to read. Consider breaking it down into smaller conditions and using descriptive variable names for them. This will make your code easier to understand. |
||
salary += Integer.parseInt(dataParts[2]) | ||
* Integer.parseInt(dataParts[3]); | ||
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. These magic numbers |
||
|
||
} | ||
} | ||
report.append(System.lineSeparator()).append(name).append(" - ") | ||
.append(salary); | ||
} | ||
return report.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.
Good job on making the
DateTimeFormatter
a constant. This is a good practice as it enhances readability and maintainability of your code.