Skip to content

Commit

Permalink
fixed Bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeriyM250 committed Sep 28, 2024
1 parent c2cee89 commit 3215c00
Showing 1 changed file with 52 additions and 46 deletions.
98 changes: 52 additions & 46 deletions src/main/java/core/basesyntax/SalaryInfo.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,76 @@
package core.basesyntax;

public class SalaryInfo {
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
int salaryFirst = 0;
int salarySecond = 0;
int salaryThird = 0;
String firstName = "";
String secondName = "";
String thirdName = "";
public static final int ZERO = 0;
public static final int FIRST = 1;
public static final int SECOND = 2;

String [] splitDataFrom = dateFrom.split("[.]");
String [] splitDataTo = dateTo.split("[.]");
String [] splitCurrentData = new String[3];
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
final String DivideR = "[.]";
final int ThirD = 3;
final int FiftH = 5;
String [] peopleNames = new String[3];
int [] salaries = new int[3];
String [] splitInputString = new String[4];

int currentDay = 0;
int currentMonth = 0;
int toDay = Integer.parseInt(splitDataTo[0]);
int fromDay = Integer.parseInt(splitDataFrom[0]);
int toMonth = Integer.parseInt(splitDataTo[1]);
int fromMonth = Integer.parseInt(splitDataFrom[1]);
firstName = names[0];
int toDay = Integer.parseInt(dateTo.substring(ZERO, SECOND));
int fromDay = Integer.parseInt(dateFrom.substring(ZERO, SECOND));
int toMonth = Integer.parseInt(dateTo.substring(ThirD, FiftH));
int fromMonth = Integer.parseInt(dateFrom.substring(ThirD, FiftH));

for (int i = 1; i < names.length; i++) {
if (!firstName.equals(names[i])) {
secondName = names[i];
}
break;
}
for (int i = 1; i < names.length; i++) {
if (!firstName.equals(names[i]) && !secondName.equals(names[i])) {
thirdName = names[i];
break;
}
}
peopleNames[ZERO] = names[0];
peopleNames[FIRST] = defineName(names, peopleNames[ZERO], peopleNames[ZERO]);
peopleNames[SECOND] = defineName(names, peopleNames[ZERO], peopleNames[FIRST]);

//sort by data
for (int i = 0; i < data.length; i++) {
splitInputString = data[i].split(" ");
splitCurrentData = splitInputString[0].split("[.]");
currentDay = Integer.parseInt(splitCurrentData[0]);
currentMonth = Integer.parseInt(splitCurrentData[1]);
currentDay = Integer.parseInt(data[i].substring(ZERO, SECOND));
currentMonth = Integer.parseInt(data[i].substring(ThirD, FiftH));

if ((fromDay <= currentDay && currentDay <= toDay
&& currentMonth == toMonth && currentMonth == fromMonth)
|| (fromDay <= currentDay && fromMonth == currentMonth
&& currentMonth < toMonth)
|| (fromMonth < currentMonth && currentDay <= toDay
&& currentMonth == toMonth)) {
if (splitInputString[1].equals(firstName)) {
salaryFirst += Integer.parseInt(splitInputString[2])
* Integer.parseInt(splitInputString[3]);
}
if (splitInputString[1].equals(secondName)) {
salarySecond += Integer.parseInt(splitInputString[2])
* Integer.parseInt(splitInputString[3]);
}
if (splitInputString[1].equals(thirdName)) {
salaryThird += Integer.parseInt(splitInputString[2])
* Integer.parseInt(splitInputString[3]);
}

salaries[ZERO] += defineIncome(peopleNames[ZERO], splitInputString[FIRST],
splitInputString[SECOND], splitInputString[ThirD]);

salaries[FIRST] += defineIncome(peopleNames[FIRST], splitInputString[FIRST],
splitInputString[SECOND], splitInputString[ThirD]);

salaries[SECOND] += defineIncome(peopleNames[SECOND], splitInputString[FIRST],
splitInputString[SECOND], splitInputString[ThirD]);
}
}
return createReport(peopleNames, salaries, dateFrom, dateTo);
}

private String defineName(String [] names, String firstName, String secondName) {
for (int i = 1; i < names.length; i++) {
if (!firstName.equals(names[i]) && !secondName.equals(names[i])) {
return names[i];
}
}
return "";
}

private int defineIncome(String personName, String comparisonName,
String hours, String income) {
if (comparisonName.equals(personName)) {
return Integer.parseInt(hours)
* Integer.parseInt(income);
}
return 0;
}

private String createReport(String [] names, int [] salaries, String dateFrom, String dateTo) {
return "Report for period " + dateFrom + " - " + dateTo
+ System.lineSeparator() + firstName + " - " + salaryFirst
+ System.lineSeparator() + secondName + " - " + salarySecond
+ System.lineSeparator() + thirdName + " - " + salaryThird;
+ System.lineSeparator() + names[ZERO] + " - " + salaries[ZERO]
+ System.lineSeparator() + names[FIRST] + " - " + salaries[FIRST]
+ System.lineSeparator() + names[SECOND] + " - " + salaries[SECOND];
}
}

0 comments on commit 3215c00

Please sign in to comment.