Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojtek-A-JAVA committed Dec 13, 2024
1 parent 467bc6a commit f09741e
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
package core.basesyntax;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
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;
private static final String DATE_FORMAT = "dd.MM.yyyy";

public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
Calendar dateFromDateFormat = changeDateFormat(dateFrom);
Calendar dateToDateFormat = changeDateFormat(dateTo);
dateFromDateFormat.add(Calendar.DAY_OF_MONTH, -1);
dateToDateFormat.add(Calendar.DAY_OF_MONTH, 1);
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[0]).after(dateFromDateFormat)
&& changeDateFormat(dataInfo[0]).before(dateToDateFormat)) {
if (changeDateFormat(dataInfo[DATE]).isAfter(dateFromDateFormat.minusDays(DAY))
&& changeDateFormat(dataInfo[DATE]).isBefore(dateToDateFormat.plusDays(DAY))) {
for (int i = 0; i < names.length; i++) {
if (Objects.equals(names[i], dataInfo[1])) {
salary[i] += Integer.parseInt(dataInfo[2]) * Integer.parseInt(dataInfo[3]);
if (Objects.equals(names[i], dataInfo[NAME])) {
salary[i] += Integer.parseInt(dataInfo[HOURS])
* Integer.parseInt(dataInfo[INCOME]);
}
}
}
Expand All @@ -39,17 +44,8 @@ && changeDateFormat(dataInfo[0]).before(dateToDateFormat)) {
return stringBuilder.toString();
}

public Calendar changeDateFormat(String stringDate) {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");

Calendar calendar = Calendar.getInstance();

try {
calendar.setTime(sdf.parse(stringDate));
} catch (ParseException e) {
e.printStackTrace();
}

return calendar;
public LocalDate changeDateFormat(String stringDate) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
return LocalDate.parse(stringDate, formatter);
}
}

0 comments on commit f09741e

Please sign in to comment.