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..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 @@ -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,27 @@ 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 +46,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 +77,25 @@ public long countDirsInDirectory(String path) { */ @Override public void copyTXTFiles(String from, String to) { - return; + + ClassLoader classLoader = getClass().getClassLoader(); + String respath = (new File(classLoader.getResource("").getFile())).getAbsolutePath(); + File frompath = new File(respath + "/" + from); + File topath = new File(respath + "/" + to); + + if (frompath.exists()&&topath.exists()) { + File[] files = frompath.listFiles(); + + for (File x : files) { + if (x.isFile() && x.toPath().endsWith(".txt")) { + try { + Files.move(x.toPath(), topath.toPath(), StandardCopyOption.ATOMIC_MOVE); + } catch (Exception e) { e.printStackTrace(); } + } + } + + + } } /** @@ -44,7 +107,29 @@ public void copyTXTFiles(String from, String to) { */ @Override public boolean createFile(String path, String name) { - return false; + + ClassLoader classLoader = getClass().getClassLoader(); + + String str = (new File(classLoader.getResource("").getFile())).getAbsolutePath(); + + File objdir = new File(str + "/" + path); + + 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();} + } + + return objfile.exists(); + } /** @@ -55,6 +140,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) {e.printStackTrace();} + + + 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 } } 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