diff --git a/java-data-handling-template.iml b/java-data-handling-template.iml new file mode 100644 index 00000000..5e9ffdc5 --- /dev/null +++ b/java-data-handling-template.iml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/epam/izh/rd/online/repository/SimpleFileRepository.java b/src/main/java/com/epam/izh/rd/online/repository/SimpleFileRepository.java index 1783b845..1796a8a3 100644 --- a/src/main/java/com/epam/izh/rd/online/repository/SimpleFileRepository.java +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleFileRepository.java @@ -1,5 +1,13 @@ package com.epam.izh.rd.online.repository; +import java.io.*; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + + public class SimpleFileRepository implements FileRepository { /** @@ -10,7 +18,32 @@ public class SimpleFileRepository implements FileRepository { */ @Override public long countFilesInDirectory(String path) { - return 0; + + Path pathResult = Paths.get(path); + if (!pathResult.isAbsolute()) { + + URL url = getClass().getClassLoader().getResource(path); + try { + assert url != null; + Path pathAbsolut = Paths.get(url.toURI()); + path = pathAbsolut.toString(); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + } + + File dir = new File(path); + long countFiles = 0; + if (dir.exists() && dir.isDirectory()) { + for (File innerDir: dir.listFiles()) { + if (innerDir.isFile()) { + countFiles++; + } else { + countFiles += new SimpleFileRepository().countFilesInDirectory(innerDir.getPath()); + } + } + } + return countFiles; } /** @@ -21,7 +54,30 @@ public long countFilesInDirectory(String path) { */ @Override public long countDirsInDirectory(String path) { - return 0; + + Path pathResult = Paths.get(path); + if (!pathResult.isAbsolute()) { + + URL url = getClass().getClassLoader().getResource(path); + try { + assert url != null; + Path pathAbsolut = Paths.get(url.toURI()); + path = pathAbsolut.toString(); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + } + + File dir = new File(path); + long countDirs = 1; + if (dir.exists() && dir.isDirectory()) { + for (File innerDir: dir.listFiles()) { + if (innerDir.isDirectory()) { + countDirs += new SimpleFileRepository().countDirsInDirectory(innerDir.getPath()); + } + } + } + return countDirs; } /** @@ -32,7 +88,16 @@ public long countDirsInDirectory(String path) { */ @Override public void copyTXTFiles(String from, String to) { - return; + File dirFrom = new File(from); + File dirTo = new File(to); + if (!dirTo.getParentFile().exists()) { + dirTo.getParentFile().mkdirs(); + } + try { + Files.copy(dirFrom.getAbsoluteFile().toPath(), dirTo.getAbsoluteFile().toPath()); + } catch (IOException e) { + e.printStackTrace(); + } } /** @@ -44,7 +109,33 @@ public void copyTXTFiles(String from, String to) { */ @Override public boolean createFile(String path, String name) { - return false; + + Path pathResultPath = Paths.get(path); + + if (!pathResultPath.isAbsolute()) { + + try { + Path pathAbsolut = Paths.get(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI()); + path = pathAbsolut.toString() + "\\" + path; + } catch (URISyntaxException e) { + e.printStackTrace(); + } + } + File file = new File(path, name); + + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + if (!file.exists()) { + try { + file.createNewFile(); + return file.exists(); + + } catch (IOException e) { + e.printStackTrace(); + } + } + return file.exists(); } /** @@ -55,6 +146,19 @@ public boolean createFile(String path, String name) { */ @Override public String readFileFromResources(String fileName) { - return null; + + String textFromFile = ""; + try (BufferedReader reader = new BufferedReader( + new FileReader("src/main/resources"+File.separator+fileName))){ + while (reader.ready()) { + textFromFile += reader.readLine(); + } + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return textFromFile; } } diff --git a/src/main/java/com/epam/izh/rd/online/service/SimpleBigNumbersService.java b/src/main/java/com/epam/izh/rd/online/service/SimpleBigNumbersService.java index a94c7bba..d6c2efde 100644 --- a/src/main/java/com/epam/izh/rd/online/service/SimpleBigNumbersService.java +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleBigNumbersService.java @@ -2,6 +2,8 @@ import java.math.BigDecimal; import java.math.BigInteger; +import java.math.RoundingMode; +import java.util.Arrays; public class SimpleBigNumbersService implements BigNumbersService { @@ -13,7 +15,8 @@ public class SimpleBigNumbersService implements BigNumbersService { */ @Override public BigDecimal getPrecisionNumber(int a, int b, int range) { - return null; + + return new BigDecimal(Integer.toString(a)).divide(new BigDecimal(Integer.toString(b)), range, RoundingMode.HALF_UP); } /** @@ -24,6 +27,23 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) { */ @Override public BigInteger getPrimaryNumber(int range) { - return null; + BigInteger[] primaryNumbers = new BigInteger[]{new BigInteger("2")}; + BigInteger value = new BigInteger("3"); + while (primaryNumbers.length <= range) { + Boolean flagPrimary = true; + int count = 0; + while (flagPrimary && count < primaryNumbers.length) { + if (value.remainder(primaryNumbers[count]).compareTo(new BigInteger("0")) == 0) { + flagPrimary = false; + } + count++; + } + if (flagPrimary) { + primaryNumbers = Arrays.copyOf(primaryNumbers, primaryNumbers.length + 1); + primaryNumbers[primaryNumbers.length - 1] = value; + } + value = value.add(new BigInteger("1")); + } + return primaryNumbers[primaryNumbers.length-1]; } } diff --git a/src/main/java/com/epam/izh/rd/online/service/SimpleDateService.java b/src/main/java/com/epam/izh/rd/online/service/SimpleDateService.java index 70d64dfd..efd8485a 100644 --- a/src/main/java/com/epam/izh/rd/online/service/SimpleDateService.java +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleDateService.java @@ -2,6 +2,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.Year; import java.time.format.DateTimeFormatter; public class SimpleDateService implements DateService { @@ -14,7 +15,9 @@ public class SimpleDateService implements DateService { */ @Override public String parseDate(LocalDate localDate) { - return null; + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); + return formatter.format(localDate); } /** @@ -25,7 +28,9 @@ public String parseDate(LocalDate localDate) { */ @Override public LocalDateTime parseString(String string) { - return null; + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:HH"); + return LocalDateTime.parse(string, formatter); } /** @@ -37,7 +42,8 @@ public LocalDateTime parseString(String string) { */ @Override public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter formatter) { - return null; + + return formatter.format(localDate); } /** @@ -47,7 +53,13 @@ public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter forma */ @Override public long getNextLeapYear() { - return 0; + + Year year = Year.now(); + do { + year = year.plusYears(1); + } while (!year.isLeap()); + + return year.getValue(); } /** @@ -57,7 +69,13 @@ public long getNextLeapYear() { */ @Override public long getSecondsInYear(int year) { - return 0; + + Year givenYear = Year.of(year); + if (givenYear.isLeap()) { + return 366 * 24 * 3600; + } else { + return 365 * 24 * 3600; + } } diff --git a/src/main/java/com/epam/izh/rd/online/service/SimpleRegExpService.java b/src/main/java/com/epam/izh/rd/online/service/SimpleRegExpService.java index b6eff56b..3697b2d0 100644 --- a/src/main/java/com/epam/izh/rd/online/service/SimpleRegExpService.java +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleRegExpService.java @@ -1,5 +1,12 @@ package com.epam.izh.rd.online.service; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class SimpleRegExpService implements RegExpService { /** @@ -9,9 +16,28 @@ public class SimpleRegExpService implements RegExpService { * * @return обработанный текст */ + private static final String nameFile = "D:\\Epam_Java_Course\\java-data-handling-template\\src\\main\\" + + "resources\\sensitive_data.txt"; + @Override public String maskSensitiveData() { - return null; + + StringBuilder outputString = null; + + try (BufferedReader reader = new BufferedReader(new FileReader(nameFile))) { + + outputString = new StringBuilder(reader.readLine()); + Pattern pattern = Pattern.compile("(\\d{4} ){4}"); + Matcher matcher = pattern.matcher(outputString); + + while (matcher.find()) { + outputString.replace(matcher.start() + 5, matcher.end() - 5, "**** **** "); + } + } catch (IOException e) { + e.printStackTrace(); + } + + return Objects.requireNonNull(outputString).toString(); } /** @@ -22,6 +48,25 @@ public String maskSensitiveData() { */ @Override public String replacePlaceholders(double paymentAmount, double balance) { - return null; + + StringBuilder outputString = null; + + + try (BufferedReader reader = new BufferedReader(new FileReader(nameFile))) { + outputString = new StringBuilder(reader.readLine()); + Pattern pattern = Pattern.compile("(\\$\\{)payment_amount}"); + Matcher matcher = pattern.matcher(outputString); + if (!matcher.find()) throw new IOException(); + outputString.replace(matcher.start(), matcher.end(), Integer.toString((int) paymentAmount)); + pattern = Pattern.compile("(\\$\\{)balance}"); + matcher = pattern.matcher(outputString); + if (!matcher.find()) throw new IOException(); + outputString.replace(matcher.start(), matcher.end(), Integer.toString((int) balance)); + } catch (IOException e) { + e.printStackTrace(); + } + + return Objects.requireNonNull(outputString).toString(); } + } diff --git a/src/main/java/com/epam/izh/rd/online/service/SimpleTextService.java b/src/main/java/com/epam/izh/rd/online/service/SimpleTextService.java index 68951fbe..22a9e4a5 100644 --- a/src/main/java/com/epam/izh/rd/online/service/SimpleTextService.java +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleTextService.java @@ -13,7 +13,8 @@ public class SimpleTextService implements TextService { */ @Override public String removeString(String base, String remove) { - return null; //TODO + + return base.replaceAll(remove, ""); //TODO } /** @@ -24,7 +25,8 @@ public String removeString(String base, String remove) { */ @Override public boolean isQuestionString(String text) { - return false; //TODO + + return (text.endsWith("?")); //TODO } /** @@ -35,7 +37,12 @@ public boolean isQuestionString(String text) { */ @Override public String concatenate(String... elements) { - return null; //TODO + + String resultString = ""; + for (String item : elements) { + resultString += item; + } + return resultString; //TODO } /** @@ -47,7 +54,18 @@ public String concatenate(String... elements) { */ @Override public String toJumpCase(String text) { - return null; //TODO + + String textToLowerCase = text.toLowerCase(); + String textToUpperCase = text.toUpperCase(); + String textToJumpCase = ""; + for (int i = 0; i < text.length(); i++) { + if (i % 2 == 0) { + textToJumpCase += textToLowerCase.substring(i, i + 1); + } else { + textToJumpCase += textToUpperCase.substring(i, i + 1); + } + } + return textToJumpCase; //TODO } /** @@ -59,6 +77,12 @@ public String toJumpCase(String text) { */ @Override public boolean isPalindrome(String string) { - return false; //TODO + + if (string.length() > 1) { + string = new SimpleTextService().removeString(string, " "); + return string.equalsIgnoreCase(new StringBuilder(string).reverse().toString()); //TODO + } else { + return false; + } //TODO } } diff --git a/src/test/java/com/epam/izh/rd/online/FileRepositoryTest.java b/src/test/java/com/epam/izh/rd/online/FileRepositoryTest.java index d581bac2..6e55a4e6 100644 --- a/src/test/java/com/epam/izh/rd/online/FileRepositoryTest.java +++ b/src/test/java/com/epam/izh/rd/online/FileRepositoryTest.java @@ -75,7 +75,6 @@ void testCopyTXTFiles() { @DisplayName("Тест метода FileRepository.createFile(String path)") void testCreateFile() { fileRepository.createFile(TEST_DIR_CREATE_PATH, TEST_FILE_TO_CREATE); - assertTrue(getFile(TEST_DIR_CREATE_PATH + "/" + TEST_FILE_TO_CREATE).exists()); }