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 efce797 commit 467bc6a
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
package core.basesyntax;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Objects;

public class SalaryInfo {
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
return null;
Calendar dateFromDateFormat = changeDateFormat(dateFrom);
Calendar dateToDateFormat = changeDateFormat(dateTo);
dateFromDateFormat.add(Calendar.DAY_OF_MONTH, -1);
dateToDateFormat.add(Calendar.DAY_OF_MONTH, 1);
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)) {
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]);
}
}
}
}

StringBuilder stringBuilder = new StringBuilder("Report for period " + dateFrom
+ " - " + dateTo + System.lineSeparator());
for (int i = 0; i < names.length; i++) {
if (i != names.length - 1) {
stringBuilder.append(names[i]).append(" - ").append(salary[i])
.append(System.lineSeparator());
} else {
stringBuilder.append(names[i]).append(" - ").append(salary[i]);
}
}

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;
}
}

0 comments on commit 467bc6a

Please sign in to comment.