Skip to content
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

A task for calculating salaries has been implemented. #1078

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

public class Main {
druhov marked this conversation as resolved.
Show resolved Hide resolved
public static void main(String[] args) {
SalaryInfo salaryInfo = new SalaryInfo();

String[] names = {"John", "Andrew", "Kate"};
String[] data = {
"26.04.2019 John 4 50",
"05.04.2019 Andrew 3 200",
"10.04.2019 John 7 100",
"22.04.2019 Kate 9 100",
"25.06.2019 John 11 50",
"26.04.2019 Andrew 3 150",
"13.02.2019 John 7 100",
"26.04.2019 Kate 9 100"
};
String dateFrom = "01.04.2019";
String dateTo = "30.04.2019";

String result = salaryInfo.getSalaryInfo(names, data, dateFrom, dateTo);
System.out.println(result);
}
}
51 changes: 50 additions & 1 deletion src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
package core.basesyntax;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SalaryInfo {
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
return null;
StringBuilder result = new StringBuilder();

try {
druhov marked this conversation as resolved.
Show resolved Hide resolved
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
Date fromDate = dateFormat.parse(dateFrom);
Date toDate = dateFormat.parse(dateTo);
druhov marked this conversation as resolved.
Show resolved Hide resolved

result.append("Report for period ")
.append(dateFrom)
.append(" - ")
.append(dateTo)
.append(System.lineSeparator());

for (String name : names) {
int totalSalary = 0;

for (String entry : data) {
String[] entryParts = entry.split(" ");
String entryDateStr = entryParts[0];
druhov marked this conversation as resolved.
Show resolved Hide resolved
String entryName = entryParts[1];

try {
int hours = Integer.parseInt(entryParts[2]);
int hourlyRate = Integer.parseInt(entryParts[3]);
Date entryDate = dateFormat.parse(entryDateStr);

if (entryName.equals(name) && entryDate.compareTo(fromDate) >= 0
&& entryDate.compareTo(toDate) <= 0) {
totalSalary += hours * hourlyRate;
}
} catch (NumberFormatException e) {
throw new NumberFormatException("Invalid number format");
}
}

result.append(name)
.append(" - ")
.append(totalSalary);
if (!name.equals(names[names.length - 1])) {
result.append(System.lineSeparator());
}
}
} catch (Exception e) {
throw new RuntimeException("Error occurred while calculating salaries");
}

return result.toString();
}
}