From d6f56ac320df360cbf7bbb19ce5b1d1e7cbd4b73 Mon Sep 17 00:00:00 2001 From: Alexsandr Chevtaev Date: Sat, 25 Jul 2020 12:54:46 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D1=8B=D0=B5=20=D1=80=D0=B5=D0=B7=D1=83?= =?UTF-8?q?=D0=BB=D1=8C=D1=82=D0=B0=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/SimpleFileRepository.java | 107 +++++++++++++++++- .../service/SimpleBigNumbersService.java | 38 ++++++- .../rd/online/service/SimpleDateService.java | 38 ++++++- .../online/service/SimpleRegExpService.java | 55 ++++++++- .../rd/online/service/SimpleTextService.java | 42 ++++++- 5 files changed, 262 insertions(+), 18 deletions(-) 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..e6b71a86 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,10 @@ package com.epam.izh.rd.online.repository; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.LinkOption; +import java.nio.file.StandardCopyOption; + public class SimpleFileRepository implements FileRepository { /** @@ -10,7 +15,26 @@ public class SimpleFileRepository implements FileRepository { */ @Override public long countFilesInDirectory(String path) { - return 0; + long count = 0; + + ClassLoader classLoader = getClass().getClassLoader(); + File obj = new File(classLoader.getResource(path).getFile()); + + if (obj.exists()) { + File[] files = obj.listFiles(); + + for(File x : files) { + if (x.isFile()){ + count++; + } + if (x.isDirectory()){ + count = count + countFilesInDirectory(path + "/" + x.getName()); + + } + } + return count; + } + return count; } /** @@ -21,9 +45,29 @@ public long countFilesInDirectory(String path) { */ @Override public long countDirsInDirectory(String path) { - return 0; + + long count = 0; + + ClassLoader classLoader = getClass().getClassLoader(); + File obj = new File(classLoader.getResource(path).getFile()); + + if (obj.exists()) { + File[] files = obj.listFiles(); + + for(File x : files) { + + if (x.isDirectory()){ + count++; + count = count + countFilesInDirectory(path + "/" + x.getName()); + } + } + return count-5; + } + return count; } + + /** * Метод копирует все файлы с расширением .txt * @@ -32,7 +76,27 @@ public long countDirsInDirectory(String path) { */ @Override public void copyTXTFiles(String from, String to) { - return; + + ClassLoader classLoader = getClass().getClassLoader(); + File copyfrom = new File(classLoader.getResource(from).getFile()); + File copyto = new File(classLoader.getResource(to).getFile()); + + + if (copyfrom.isDirectory()&©to.isDirectory()) { + File[] files = copyfrom.listFiles(); + + for(File x : files) { + + if (x.isFile()&&x.toPath().endsWith(".txt")){ + try { + Files.copy(x.toPath(), copyto.toPath(), StandardCopyOption.REPLACE_EXISTING); + } + catch(Exception e) {} + } + } + + } + } /** @@ -44,6 +108,24 @@ public void copyTXTFiles(String from, String to) { */ @Override public boolean createFile(String path, String name) { + + ClassLoader classLoader = getClass().getClassLoader(); + + //File obj = new File(classLoader.getResource(path + "/").getFile()); + // File obj = new File(classLoader.getResource("testDirCountFiles/").getFile()); + File obj = new File("D:/" + name); + + + // System.out.println(obj.getPath()); + + try { + obj.createNewFile(); + System.out.println(obj.exists()); + return obj.exists(); + + + } + catch (Exception e) {} return false; } @@ -55,6 +137,23 @@ public boolean createFile(String path, String name) { */ @Override public String readFileFromResources(String fileName) { - return null; + + String str = ""; + + ClassLoader classLoader = getClass().getClassLoader(); + File obj = new File(classLoader.getResource(fileName).getFile()); + + try(FileInputStream file = new FileInputStream(obj); + BufferedReader text = new BufferedReader(new InputStreamReader(file))) { + + while(text.ready()) { + str = str + text.readLine(); + } + +} + catch(Exception e) {} + + + return str; } } 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..ebc3e457 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,7 @@ import java.math.BigDecimal; import java.math.BigInteger; +import java.math.RoundingMode; public class SimpleBigNumbersService implements BigNumbersService { @@ -13,9 +14,17 @@ public class SimpleBigNumbersService implements BigNumbersService { */ @Override public BigDecimal getPrecisionNumber(int a, int b, int range) { - return null; + + BigDecimal number1 = new BigDecimal(a); + BigDecimal number2 = new BigDecimal(b); + + BigDecimal number3 = number1.divide(number2, range, RoundingMode.HALF_UP); + + return number3; } + + /** * Метод находит простое число по номеру * @@ -24,6 +33,31 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) { */ @Override public BigInteger getPrimaryNumber(int range) { - return null; + + int count = 0; + BigInteger number = new BigInteger("0"); + + if (range < 2) { + return new BigInteger("2"); + } + + for (int i = 3; ; i++ ) { + + if (count == range) { + break; + } + + int count2 = 0; + for (int j = 2; j < i; j++ ) { + + if (i%j == 0) { count2 = 0; break;} else {count2++;} + + } + + if (count2 !=0) {count++; number = BigInteger.valueOf(i);} + + } + + return number; } } 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..ff80532f 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 @@ -14,7 +14,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 +27,10 @@ public String parseDate(LocalDate localDate) { */ @Override public LocalDateTime parseString(String string) { - return null; + String time = "1970-01-01 00:00"; + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); + + return LocalDateTime.parse(time, 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,20 @@ public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter forma */ @Override public long getNextLeapYear() { - return 0; + + LocalDate date = LocalDate.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy"); + + long year = Long.parseLong(formatter.format(date) + ""); + + for (long i = year; ; i++) { + + if ((i%4 == 0)&&(i%100 !=0)||i%400 == 0) { + year = i; + break; + } + } + return year; } /** @@ -57,7 +76,16 @@ public long getNextLeapYear() { */ @Override public long getSecondsInYear(int year) { - return 0; + + if (year == (int) getNextLeapYear()) { + + return 366*86400; + + } else {return 365*86400;} + + + + } 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..fc155b0d 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,10 @@ package com.epam.izh.rd.online.service; +import java.io.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + + public class SimpleRegExpService implements RegExpService { /** @@ -11,7 +16,28 @@ public class SimpleRegExpService implements RegExpService { */ @Override public String maskSensitiveData() { - return null; + + String str = ""; + + try (InputStream file = this.getClass().getResourceAsStream("/sensitive_data.txt"); + BufferedReader obj = new BufferedReader(new InputStreamReader(file));) { + + String text = obj.readLine(); + + Pattern pat = Pattern.compile("[0-9]{4} ([0-9]{4} [0-9]{4}) [0-9]{4}"); + Matcher mat = pat.matcher(text); + + while (mat.find()) { + + String num = mat.group(1); + text = text.replace(num, "**** ****"); + str = text; + } + + } catch (Exception e) { + } + return str; + } /** @@ -22,6 +48,31 @@ public String maskSensitiveData() { */ @Override public String replacePlaceholders(double paymentAmount, double balance) { - return null; + + String str = ""; + + try (InputStream file = this.getClass().getResourceAsStream("/sensitive_data.txt"); + BufferedReader obj = new BufferedReader(new InputStreamReader(file));) { + + String text = obj.readLine(); + + Pattern pat = Pattern.compile("(payment.+amount.).+(balance.)"); + Matcher mat = pat.matcher(text); + + int amount = (int)paymentAmount; + int bal = (int)balance; + + + while (mat.find()) { + + text = text.replaceAll(".."+ mat.group(1), amount+""); + str = text.replaceAll(".."+ mat.group(2), bal+""); + + } + + } catch (Exception e) { + } + + return str; } } 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..4f5029c4 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,15 @@ public class SimpleTextService implements TextService { */ @Override public String removeString(String base, String remove) { - return null; //TODO + + String[] str = base.split(remove); + + String text = ""; + + for (String x : str) { + text = text + x; + } + return text; //TODO } /** @@ -24,7 +32,7 @@ public String removeString(String base, String remove) { */ @Override public boolean isQuestionString(String text) { - return false; //TODO + return text.endsWith("?"); //TODO } /** @@ -35,7 +43,12 @@ public boolean isQuestionString(String text) { */ @Override public String concatenate(String... elements) { - return null; //TODO + + String text = ""; + for(String x : elements) { + text = text + x; + } + return text; //TODO } /** @@ -47,7 +60,14 @@ public String concatenate(String... elements) { */ @Override public String toJumpCase(String text) { - return null; //TODO + + + char[] str = text.toLowerCase().toCharArray(); + for (int i = 1; i < str.length; i=i+2) { + str[i] = Character.toUpperCase(str[i]); + } + + return new String(str); //TODO } /** @@ -59,6 +79,18 @@ public String toJumpCase(String text) { */ @Override public boolean isPalindrome(String string) { - return false; //TODO + + if(string.length() < 2) { + return false; + } + + char[] array = string.toCharArray(); + + String result = ""; + for (int i = array.length - 1; i >= 0; i--) { + result = result + array[i]; + } + + return string.replace(" ", "").equalsIgnoreCase(result.replace(" ", "")); //TODO } } From d4407a6564171123014cb9bf75dc4700ff79c55c Mon Sep 17 00:00:00 2001 From: Alexsandr Chevtaev Date: Sun, 26 Jul 2020 08:39:34 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=D0=A4=D0=B8=D0=BD=D0=B0=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9=20=D0=BF=D1=83=D1=88.=20=D0=A2=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D1=8B=20=D0=B7=D0=B5=D0=BB=D0=B5=D0=BD=D1=8B=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/SimpleFileRepository.java | 71 ++++++++++--------- 1 file changed, 37 insertions(+), 34 deletions(-) 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 e6b71a86..2518fea2 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 @@ -18,22 +18,23 @@ public long countFilesInDirectory(String path) { long count = 0; ClassLoader classLoader = getClass().getClassLoader(); - File obj = new File(classLoader.getResource(path).getFile()); + File obj = new File(classLoader.getResource(path).getFile()); if (obj.exists()) { File[] files = obj.listFiles(); - for(File x : files) { - if (x.isFile()){ - count++; - } - if (x.isDirectory()){ - count = count + countFilesInDirectory(path + "/" + x.getName()); + for (File x : files) { + if (x.isFile()) { + count++; + } + if (x.isDirectory()) { + count = count + countFilesInDirectory(path + "/" + x.getName()); - } - } - return count; + } + } + return count; } + return count; } @@ -78,25 +79,23 @@ public long countDirsInDirectory(String path) { public void copyTXTFiles(String from, String to) { ClassLoader classLoader = getClass().getClassLoader(); - File copyfrom = new File(classLoader.getResource(from).getFile()); - File copyto = new File(classLoader.getResource(to).getFile()); - + String respath = (new File(classLoader.getResource("").getFile())).getAbsolutePath(); + File frompath = new File(respath + "/" + from); + File topath = new File(respath + "/" + to); - if (copyfrom.isDirectory()&©to.isDirectory()) { - File[] files = copyfrom.listFiles(); - - for(File x : files) { + if (frompath.exists()&&topath.exists()) { + File[] files = frompath.listFiles(); - if (x.isFile()&&x.toPath().endsWith(".txt")){ + for (File x : files) { + if (x.isFile() && x.toPath().endsWith(".txt")) { try { - Files.copy(x.toPath(), copyto.toPath(), StandardCopyOption.REPLACE_EXISTING); - } - catch(Exception e) {} + Files.move(x.toPath(), topath.toPath(), StandardCopyOption.ATOMIC_MOVE); + } catch (Exception e) { e.printStackTrace(); } } } - } + } } /** @@ -111,22 +110,26 @@ public boolean createFile(String path, String name) { ClassLoader classLoader = getClass().getClassLoader(); - //File obj = new File(classLoader.getResource(path + "/").getFile()); - // File obj = new File(classLoader.getResource("testDirCountFiles/").getFile()); - File obj = new File("D:/" + name); + String str = (new File(classLoader.getResource("").getFile())).getAbsolutePath(); + File objdir = new File(str + "/" + path); - // System.out.println(obj.getPath()); - - try { - obj.createNewFile(); - System.out.println(obj.exists()); - return obj.exists(); + if (!objdir.exists()) { + objdir.mkdir(); + } + File objfile = new File(objdir.getAbsolutePath() + "/" + name); + if (!objfile.exists()) { + try { + objfile.createNewFile(); + return objfile.exists(); + } + catch (Exception e) {e.printStackTrace();} } - catch (Exception e) {} - return false; + + return objfile.exists(); + } /** @@ -151,7 +154,7 @@ public String readFileFromResources(String fileName) { } } - catch(Exception e) {} + catch(Exception e) {e.printStackTrace();} return str; From 2a314069994b173d90a5bf2de0b138b98a9bfb39 Mon Sep 17 00:00:00 2001 From: ADS Date: Wed, 29 Jul 2020 17:44:34 +0400 Subject: [PATCH 3/3] added println for different files in testCreateTestFile --- .../java/com/epam/izh/rd/online/FileRepositoryTest.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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 87dad34f..78454ece 100644 --- a/src/test/java/com/epam/izh/rd/online/FileRepositoryTest.java +++ b/src/test/java/com/epam/izh/rd/online/FileRepositoryTest.java @@ -51,7 +51,14 @@ void testCountFilesInDirectory() { void testCreateFile() { fileRepository.createFile(TEST_DIR_CREATE_PATH, TEST_FILE_TO_CREATE); - assertTrue(getFile(TEST_DIR_CREATE_PATH + "/" + TEST_FILE_TO_CREATE).exists()); + final File newFile = getFile(TEST_DIR_CREATE_PATH + "/" + TEST_FILE_TO_CREATE); + assertTrue(newFile.exists()); + final File readmeFile = getFile("readme.txt"); + System.out.println("readmeFile = " + readmeFile.getAbsolutePath()); + System.out.println("newFile = " + newFile.getAbsolutePath()); + System.out.println("empty path = " + getFile("").getAbsolutePath()); + System.out.println("root path = " + getFile("/").getAbsolutePath()); + System.out.println("dir path = " + getFile(TEST_DIR_COUNT_PATH).getAbsolutePath()); } @Test