From a239268746c91494302e8e2f913f2edef753d5b4 Mon Sep 17 00:00:00 2001 From: Vladyslav Druhov Date: Thu, 28 Sep 2023 10:01:39 +0300 Subject: [PATCH 01/10] A task for calculating salaries has been implemented. Used a string builder to build input variables --- src/main/java/core/basesyntax/Main.java | 26 ++++++++++ .../core/basesyntax/SalaryCalculator.java | 51 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/main/java/core/basesyntax/Main.java create mode 100644 src/main/java/core/basesyntax/SalaryCalculator.java diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 000000000..0c24a64c5 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public class Main { + public static void main(String[] args) { + 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 = SalaryCalculator.getSalaryInfo(names, data, dateFrom, dateTo); + System.out.println(result); + } +} + + + + diff --git a/src/main/java/core/basesyntax/SalaryCalculator.java b/src/main/java/core/basesyntax/SalaryCalculator.java new file mode 100644 index 000000000..4afd4c251 --- /dev/null +++ b/src/main/java/core/basesyntax/SalaryCalculator.java @@ -0,0 +1,51 @@ +package core.basesyntax; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class SalaryCalculator { + public static String getSalaryInfo(String[] names, String[] data, String dateFrom, + String dateTo) { + StringBuilder result = new StringBuilder(); + try { + SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); + Date fromDate = dateFormat.parse(dateFrom); + Date toDate = dateFormat.parse(dateTo); + + result.append("Report for period ") + .append(dateFrom) + .append(" - ") + .append(dateTo) + .append(System.lineSeparator()); + + for (int i = 0; i < names.length; i++) { + String name = names[i]; + int totalSalary = 0; + + for (String entry : data) { + String[] entryParts = entry.split(" "); + String entryDateStr = entryParts[0]; + String entryName = entryParts[1]; + 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; + } + } + + result.append(name) + .append(" - ") + .append(totalSalary) + .append(System.lineSeparator()); + } + } catch (Exception e) { + throw new RuntimeException("Error occurred while calculating salaries"); + } + + return result.toString(); + } +} + From c058e95b651facf1763f466078b93514332df99a Mon Sep 17 00:00:00 2001 From: Vladyslav Druhov Date: Thu, 28 Sep 2023 11:01:07 +0300 Subject: [PATCH 02/10] Fixed errors related to incorrect payroll withdrawal. --- src/main/java/core/basesyntax/Main.java | 8 ++- src/main/java/core/basesyntax/SalaryInfo.java | 51 ++++++++++++++++++- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index 0c24a64c5..503039633 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -2,6 +2,8 @@ public class Main { public static void main(String[] args) { + SalaryInfo salaryInfo = new SalaryInfo(); + String[] names = {"John", "Andrew", "Kate"}; String[] data = { "26.04.2019 John 4 50", @@ -16,11 +18,7 @@ public static void main(String[] args) { String dateFrom = "01.04.2019"; String dateTo = "30.04.2019"; - String result = SalaryCalculator.getSalaryInfo(names, data, dateFrom, dateTo); + String result = salaryInfo.getSalaryInfo(names, data, dateFrom, dateTo); System.out.println(result); } } - - - - diff --git a/src/main/java/core/basesyntax/SalaryInfo.java b/src/main/java/core/basesyntax/SalaryInfo.java index 4a34339b5..b267ebb17 100644 --- a/src/main/java/core/basesyntax/SalaryInfo.java +++ b/src/main/java/core/basesyntax/SalaryInfo.java @@ -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 { + SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); + Date fromDate = dateFormat.parse(dateFrom); + Date toDate = dateFormat.parse(dateTo); + + 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]; + 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(); } } From 98ebdfd8d7ff67c10fdeb4b4207c82c8ccf363bd Mon Sep 17 00:00:00 2001 From: Vladyslav Druhov Date: Thu, 28 Sep 2023 11:01:30 +0300 Subject: [PATCH 03/10] I misunderstood the task and created a new, unnecessary class. Deleted . --- .../core/basesyntax/SalaryCalculator.java | 51 ------------------- 1 file changed, 51 deletions(-) delete mode 100644 src/main/java/core/basesyntax/SalaryCalculator.java diff --git a/src/main/java/core/basesyntax/SalaryCalculator.java b/src/main/java/core/basesyntax/SalaryCalculator.java deleted file mode 100644 index 4afd4c251..000000000 --- a/src/main/java/core/basesyntax/SalaryCalculator.java +++ /dev/null @@ -1,51 +0,0 @@ -package core.basesyntax; - -import java.text.SimpleDateFormat; -import java.util.Date; - -public class SalaryCalculator { - public static String getSalaryInfo(String[] names, String[] data, String dateFrom, - String dateTo) { - StringBuilder result = new StringBuilder(); - try { - SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); - Date fromDate = dateFormat.parse(dateFrom); - Date toDate = dateFormat.parse(dateTo); - - result.append("Report for period ") - .append(dateFrom) - .append(" - ") - .append(dateTo) - .append(System.lineSeparator()); - - for (int i = 0; i < names.length; i++) { - String name = names[i]; - int totalSalary = 0; - - for (String entry : data) { - String[] entryParts = entry.split(" "); - String entryDateStr = entryParts[0]; - String entryName = entryParts[1]; - 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; - } - } - - result.append(name) - .append(" - ") - .append(totalSalary) - .append(System.lineSeparator()); - } - } catch (Exception e) { - throw new RuntimeException("Error occurred while calculating salaries"); - } - - return result.toString(); - } -} - From aa4f1c8499120f778352b5d1c204a9d5bd48b2f3 Mon Sep 17 00:00:00 2001 From: Vladyslav Druhov Date: Fri, 29 Sep 2023 10:19:52 +0300 Subject: [PATCH 04/10] Made changes to the code according to the guide, replaced date with localDate --- src/main/java/core/basesyntax/SalaryInfo.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/core/basesyntax/SalaryInfo.java b/src/main/java/core/basesyntax/SalaryInfo.java index b267ebb17..86418b630 100644 --- a/src/main/java/core/basesyntax/SalaryInfo.java +++ b/src/main/java/core/basesyntax/SalaryInfo.java @@ -1,16 +1,18 @@ package core.basesyntax; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.Date; public class SalaryInfo { + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy"); public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) { StringBuilder result = new StringBuilder(); try { - SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); - Date fromDate = dateFormat.parse(dateFrom); - Date toDate = dateFormat.parse(dateTo); + LocalDate fromDate = LocalDate.parse(dateFrom, DATE_FORMATTER); + LocalDate toDate = LocalDate.parse(dateTo, DATE_FORMATTER); result.append("Report for period ") .append(dateFrom) @@ -23,16 +25,15 @@ public String getSalaryInfo(String[] names, String[] data, String dateFrom, Stri for (String entry : data) { String[] entryParts = entry.split(" "); - String entryDateStr = entryParts[0]; + LocalDate entryDate = LocalDate.parse(entryParts[0], DATE_FORMATTER); 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) { + if (entryName.equals(name) && !entryDate.isBefore(fromDate) + && !entryDate.isAfter(toDate)) { totalSalary += hours * hourlyRate; } } catch (NumberFormatException e) { From 061a5cc4e634e06b4653a6e0a52555d82e3db75d Mon Sep 17 00:00:00 2001 From: Vladyslav Druhov Date: Fri, 29 Sep 2023 10:19:52 +0300 Subject: [PATCH 05/10] Fix checkstyle error --- src/main/java/core/basesyntax/SalaryInfo.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/core/basesyntax/SalaryInfo.java b/src/main/java/core/basesyntax/SalaryInfo.java index b267ebb17..86418b630 100644 --- a/src/main/java/core/basesyntax/SalaryInfo.java +++ b/src/main/java/core/basesyntax/SalaryInfo.java @@ -1,16 +1,18 @@ package core.basesyntax; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.Date; public class SalaryInfo { + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy"); public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) { StringBuilder result = new StringBuilder(); try { - SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); - Date fromDate = dateFormat.parse(dateFrom); - Date toDate = dateFormat.parse(dateTo); + LocalDate fromDate = LocalDate.parse(dateFrom, DATE_FORMATTER); + LocalDate toDate = LocalDate.parse(dateTo, DATE_FORMATTER); result.append("Report for period ") .append(dateFrom) @@ -23,16 +25,15 @@ public String getSalaryInfo(String[] names, String[] data, String dateFrom, Stri for (String entry : data) { String[] entryParts = entry.split(" "); - String entryDateStr = entryParts[0]; + LocalDate entryDate = LocalDate.parse(entryParts[0], DATE_FORMATTER); 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) { + if (entryName.equals(name) && !entryDate.isBefore(fromDate) + && !entryDate.isAfter(toDate)) { totalSalary += hours * hourlyRate; } } catch (NumberFormatException e) { From d32f2fa1cb1503e8bbeceb451968d95d6ef6f504 Mon Sep 17 00:00:00 2001 From: Vladyslav Druhov Date: Fri, 29 Sep 2023 10:23:30 +0300 Subject: [PATCH 06/10] Fix checkstyle error --- src/main/java/core/basesyntax/SalaryInfo.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/core/basesyntax/SalaryInfo.java b/src/main/java/core/basesyntax/SalaryInfo.java index 86418b630..f5cd1f222 100644 --- a/src/main/java/core/basesyntax/SalaryInfo.java +++ b/src/main/java/core/basesyntax/SalaryInfo.java @@ -1,12 +1,12 @@ package core.basesyntax; -import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; -import java.util.Date; public class SalaryInfo { - private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + private static final DateTimeFormatter DATE_FORMATTER = + DateTimeFormatter.ofPattern("dd.MM.yyyy"); + public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) { StringBuilder result = new StringBuilder(); From 512ed73f1c6f30a1b4f0b09d1cbadc612491969d Mon Sep 17 00:00:00 2001 From: Vladyslav Druhov Date: Fri, 29 Sep 2023 12:46:54 +0300 Subject: [PATCH 07/10] Improved code readability --- src/main/java/core/basesyntax/SalaryInfo.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/core/basesyntax/SalaryInfo.java b/src/main/java/core/basesyntax/SalaryInfo.java index b267ebb17..fb19da025 100644 --- a/src/main/java/core/basesyntax/SalaryInfo.java +++ b/src/main/java/core/basesyntax/SalaryInfo.java @@ -1,16 +1,19 @@ package core.basesyntax; import java.text.SimpleDateFormat; -import java.util.Date; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Locale; public class SalaryInfo { + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.ENGLISH); + public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) { StringBuilder result = new StringBuilder(); try { - SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); - Date fromDate = dateFormat.parse(dateFrom); - Date toDate = dateFormat.parse(dateTo); + LocalDate fromDate = LocalDate.parse(dateFrom, DATE_FORMATTER); + LocalDate toDate = LocalDate.parse(dateTo, DATE_FORMATTER); result.append("Report for period ") .append(dateFrom) @@ -29,10 +32,9 @@ public String getSalaryInfo(String[] names, String[] data, String dateFrom, Stri try { int hours = Integer.parseInt(entryParts[2]); int hourlyRate = Integer.parseInt(entryParts[3]); - Date entryDate = dateFormat.parse(entryDateStr); + LocalDate entryDate = LocalDate.parse(entryDateStr, DATE_FORMATTER); - if (entryName.equals(name) && entryDate.compareTo(fromDate) >= 0 - && entryDate.compareTo(toDate) <= 0) { + if (entryName.equals(name) && !entryDate.isBefore(fromDate) && !entryDate.isAfter(toDate)) { totalSalary += hours * hourlyRate; } } catch (NumberFormatException e) { From 66b4a7e3b2e1f9f60068156920ad68d6a8f8bd58 Mon Sep 17 00:00:00 2001 From: Vladyslav Druhov Date: Fri, 29 Sep 2023 12:48:48 +0300 Subject: [PATCH 08/10] Fix cheakstyle --- src/main/java/core/basesyntax/SalaryInfo.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/core/basesyntax/SalaryInfo.java b/src/main/java/core/basesyntax/SalaryInfo.java index fb19da025..6125c95c4 100644 --- a/src/main/java/core/basesyntax/SalaryInfo.java +++ b/src/main/java/core/basesyntax/SalaryInfo.java @@ -1,12 +1,12 @@ package core.basesyntax; -import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Locale; public class SalaryInfo { - private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.ENGLISH); + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter + .ofPattern("dd.MM.yyyy", Locale.ENGLISH); public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) { StringBuilder result = new StringBuilder(); @@ -34,7 +34,8 @@ public String getSalaryInfo(String[] names, String[] data, String dateFrom, Stri int hourlyRate = Integer.parseInt(entryParts[3]); LocalDate entryDate = LocalDate.parse(entryDateStr, DATE_FORMATTER); - if (entryName.equals(name) && !entryDate.isBefore(fromDate) && !entryDate.isAfter(toDate)) { + if (entryName.equals(name) && !entryDate.isBefore(fromDate) + && !entryDate.isAfter(toDate)) { totalSalary += hours * hourlyRate; } } catch (NumberFormatException e) { From 4c143da076d26190e5f11c598ed976c0d7540709 Mon Sep 17 00:00:00 2001 From: Vladyslav Druhov Date: Fri, 29 Sep 2023 13:14:51 +0300 Subject: [PATCH 09/10] Delete Main class --- src/main/java/core/basesyntax/Main.java | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 src/main/java/core/basesyntax/Main.java diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java deleted file mode 100644 index 503039633..000000000 --- a/src/main/java/core/basesyntax/Main.java +++ /dev/null @@ -1,24 +0,0 @@ -package core.basesyntax; - -public class Main { - 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); - } -} From 1093ffc0d0383996825f691a8494873ed2175a24 Mon Sep 17 00:00:00 2001 From: Vladyslav Druhov Date: Fri, 29 Sep 2023 14:44:18 +0300 Subject: [PATCH 10/10] Remove try-catch blocks and make indexes to constants --- src/main/java/core/basesyntax/SalaryInfo.java | 71 +++++++++---------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/src/main/java/core/basesyntax/SalaryInfo.java b/src/main/java/core/basesyntax/SalaryInfo.java index 6125c95c4..e52b85d90 100644 --- a/src/main/java/core/basesyntax/SalaryInfo.java +++ b/src/main/java/core/basesyntax/SalaryInfo.java @@ -8,50 +8,47 @@ public class SalaryInfo { private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter .ofPattern("dd.MM.yyyy", Locale.ENGLISH); + private static final int ENTRY_DATE_INDEX = 0; + private static final int ENTRY_NAME_INDEX = 1; + private static final int HOURS_INDEX = 2; + private static final int HOURLY_RATE_INDEX = 3; + public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) { StringBuilder result = new StringBuilder(); - try { - LocalDate fromDate = LocalDate.parse(dateFrom, DATE_FORMATTER); - LocalDate toDate = LocalDate.parse(dateTo, DATE_FORMATTER); + LocalDate fromDate = LocalDate.parse(dateFrom, DATE_FORMATTER); + LocalDate toDate = LocalDate.parse(dateTo, DATE_FORMATTER); - 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]; - String entryName = entryParts[1]; - - try { - int hours = Integer.parseInt(entryParts[2]); - int hourlyRate = Integer.parseInt(entryParts[3]); - LocalDate entryDate = LocalDate.parse(entryDateStr, DATE_FORMATTER); - - if (entryName.equals(name) && !entryDate.isBefore(fromDate) - && !entryDate.isAfter(toDate)) { - totalSalary += hours * hourlyRate; - } - } catch (NumberFormatException e) { - throw new NumberFormatException("Invalid number format"); - } - } + 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[ENTRY_DATE_INDEX]; + String entryName = entryParts[ENTRY_NAME_INDEX]; - result.append(name) - .append(" - ") - .append(totalSalary); - if (!name.equals(names[names.length - 1])) { - result.append(System.lineSeparator()); + int hours = Integer.parseInt(entryParts[HOURS_INDEX]); + int hourlyRate = Integer.parseInt(entryParts[HOURLY_RATE_INDEX]); + LocalDate entryDate = LocalDate.parse(entryDateStr, DATE_FORMATTER); + + if (entryName.equals(name) && !entryDate.isBefore(fromDate) + && !entryDate.isAfter(toDate)) { + totalSalary += hours * hourlyRate; } } - } catch (Exception e) { - throw new RuntimeException("Error occurred while calculating salaries"); + + result.append(name) + .append(" - ") + .append(totalSalary); + if (!name.equals(names[names.length - 1])) { + result.append(System.lineSeparator()); + } } return result.toString();