From 3215c0058850c00a2171f4d2286bb231571f9469 Mon Sep 17 00:00:00 2001 From: Valeriy M Date: Sun, 29 Sep 2024 00:40:56 +0300 Subject: [PATCH] fixed Bugs --- src/main/java/core/basesyntax/SalaryInfo.java | 98 ++++++++++--------- 1 file changed, 52 insertions(+), 46 deletions(-) diff --git a/src/main/java/core/basesyntax/SalaryInfo.java b/src/main/java/core/basesyntax/SalaryInfo.java index 9ee35dfa5..ad4b49a85 100644 --- a/src/main/java/core/basesyntax/SalaryInfo.java +++ b/src/main/java/core/basesyntax/SalaryInfo.java @@ -1,46 +1,33 @@ 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) @@ -48,23 +35,42 @@ public String getSalaryInfo(String[] names, String[] data, String dateFrom, Stri && 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]; } }