Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.epam.izh.rd.online.repository;

import java.io.FileNotFoundException;
import java.io.IOException;

public interface FileRepository {


Expand All @@ -9,8 +12,8 @@ public interface FileRepository {

void copyTXTFiles(String from, String to);

boolean createFile(String path, String name);
boolean createFile(String path, String name) throws IOException;

String readFileFromResources(String fileName);
String readFileFromResources(String fileName) throws IOException;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.epam.izh.rd.online.repository;

import java.io.*;
import java.net.URL;
import java.nio.file.Files;

public class SimpleFileRepository implements FileRepository {

/**
Expand All @@ -10,7 +14,16 @@ public class SimpleFileRepository implements FileRepository {
*/
@Override
public long countFilesInDirectory(String path) {
return 0;
long result = 0;
File dir = new File("src/main/resources/" + path);
File[] arr = dir.listFiles();
if (dir.isDirectory()) {
for (File file : arr) {
result += countFilesInDirectory(path + "/" + file.getName());
}
} else
result++;
return result;
}

/**
Expand All @@ -21,7 +34,15 @@ public long countFilesInDirectory(String path) {
*/
@Override
public long countDirsInDirectory(String path) {
return 0;
long result = 0;
File dir = new File("src/main/resources/" + path);
if (dir.isDirectory()) {
result++;
for (File file : dir.listFiles()) {
result += countDirsInDirectory(path + "/" + file.getName());
}
}
return result;
}

/**
Expand All @@ -32,7 +53,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();
}
}

/**
Expand All @@ -44,6 +74,17 @@ public void copyTXTFiles(String from, String to) {
*/
@Override
public boolean createFile(String path, String name) {
URL url = getClass().getResource("/");
File dir = new File(url.getPath() + "/" + path);
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(dir.getPath() + "/" + name);
try {
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

Expand All @@ -54,7 +95,9 @@ public boolean createFile(String path, String name) {
* @return контент
*/
@Override
public String readFileFromResources(String fileName) {
return null;
public String readFileFromResources(String fileName) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/" + fileName));

return reader.readLine();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.epam.izh.rd.online.service;

import java.io.FileNotFoundException;
import java.io.IOException;

public interface RegExpService {

String maskSensitiveData();
String maskSensitiveData() throws IOException;

String replacePlaceholders(double paymentAmount, double balance);
String replacePlaceholders(double paymentAmount, double balance) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;

import static java.math.RoundingMode.HALF_UP;

public class SimpleBigNumbersService implements BigNumbersService {

Expand All @@ -13,7 +16,12 @@ public class SimpleBigNumbersService implements BigNumbersService {
*/
@Override
public BigDecimal getPrecisionNumber(int a, int b, int range) {
return null;
BigDecimal result;
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);

result = ba.divide(bb, range, HALF_UP);
return result;
}

/**
Expand All @@ -24,6 +32,11 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) {
*/
@Override
public BigInteger getPrimaryNumber(int range) {
return null;
BigInteger result = new BigInteger("2");
for (int i = 0; i < range; i++) {
result = result.nextProbablePrime();
}
return result;

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.epam.izh.rd.online.service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class SimpleDateService implements DateService {

Expand All @@ -14,7 +17,8 @@ public class SimpleDateService implements DateService {
*/
@Override
public String parseDate(LocalDate localDate) {
return null;
String result = localDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
return result;
}

/**
Expand All @@ -25,7 +29,9 @@ public String parseDate(LocalDate localDate) {
*/
@Override
public LocalDateTime parseString(String string) {
return null;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime result = LocalDateTime.parse(string, formatter);
return result;
}

/**
Expand All @@ -37,7 +43,8 @@ public LocalDateTime parseString(String string) {
*/
@Override
public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter formatter) {
return null;
String result = localDate.format(formatter);
return result;
}

/**
Expand All @@ -47,7 +54,15 @@ public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter forma
*/
@Override
public long getNextLeapYear() {
return 0;
Year ldNow = Year.now();
String year = ldNow.format(DateTimeFormatter.ofPattern("yyyy"));
Long result = Long.parseLong(year);
while (true) {
if (Year.isLeap(result)) {
return result;
} else
result++;
}
}

/**
Expand All @@ -57,7 +72,11 @@ public long getNextLeapYear() {
*/
@Override
public long getSecondsInYear(int year) {
return 0;
Year yea = Year.of(year);
if (yea.isLeap()) {
return (long) 366 * 24 * 3600;
} else
return (long) 365 * 24 * 3600;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.epam.izh.rd.online.service;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SimpleRegExpService implements RegExpService {

/**
Expand All @@ -10,8 +16,17 @@ public class SimpleRegExpService implements RegExpService {
* @return обработанный текст
*/
@Override
public String maskSensitiveData() {
return null;
public String maskSensitiveData() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/sensitive_data.txt"));
String content;
String result = "";
while ((content = reader.readLine()) != null){
result += content;
}
Pattern pat = Pattern.compile("(\\s\\d{4}\\s)(\\d{4}\\s)(\\d{4}\\s)(\\d{4}\\s)");
Matcher match = pat.matcher(result);
result = match.replaceAll("$1**** **** $4");
return result;
}

/**
Expand All @@ -21,7 +36,21 @@ public String maskSensitiveData() {
* @return обработанный текст
*/
@Override
public String replacePlaceholders(double paymentAmount, double balance) {
return null;
public String replacePlaceholders(double paymentAmount, double balance) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/sensitive_data.txt"));
String content;
String result = "";
while ((content = reader.readLine()) != null){
result += content;
}
Pattern patPay = Pattern.compile("\\W\\{.{14}}");
Matcher match = patPay.matcher(result);
result = match.replaceAll(String.valueOf((int) paymentAmount));

Pattern patBalance = Pattern.compile("\\W\\{.{7}}");
Matcher matchBalance = patBalance.matcher(result);
result = matchBalance.replaceAll(String.valueOf((int) balance));

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SimpleTextService implements TextService {
*/
@Override
public String removeString(String base, String remove) {
return null; //TODO
return base.replace(remove, ""); //TODO
}

/**
Expand All @@ -24,7 +24,8 @@ public String removeString(String base, String remove) {
*/
@Override
public boolean isQuestionString(String text) {
return false; //TODO

return text.endsWith("?"); //TODO
}

/**
Expand All @@ -35,7 +36,12 @@ public boolean isQuestionString(String text) {
*/
@Override
public String concatenate(String... elements) {
return null; //TODO
String res = "";
String[] result = elements;
for (String str : result) {
res = res + str;
}
return res; //TODO
}

/**
Expand All @@ -47,7 +53,15 @@ public String concatenate(String... elements) {
*/
@Override
public String toJumpCase(String text) {
return null; //TODO
char[] arr = text.toCharArray();
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0) {
arr[i] = Character.toLowerCase(arr[i]);
} else
arr[i] = Character.toUpperCase(arr[i]);
}

return String.valueOf(arr); //TODO
}

/**
Expand All @@ -59,6 +73,12 @@ public String toJumpCase(String text) {
*/
@Override
public boolean isPalindrome(String string) {
return false; //TODO

if (string.equals("")) {
return false;
}
return string.replaceAll("\\s", "")
.equalsIgnoreCase(new StringBuilder(string.replaceAll("\\s", ""))
.reverse().toString()); //TODO
}
}
4 changes: 2 additions & 2 deletions src/test/java/com/epam/izh/rd/online/FileRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ void testCopyTXTFiles() {

@Test
@DisplayName("Тест метода FileRepository.createFile(String path)")
void testCreateFile() {
void testCreateFile() throws IOException {
fileRepository.createFile(TEST_DIR_CREATE_PATH, TEST_FILE_TO_CREATE);

assertTrue(getFile(TEST_DIR_CREATE_PATH + "/" + TEST_FILE_TO_CREATE).exists());
}

@Test
@DisplayName("Тест метода FileRepository.readFileFromResources(String fileName)")
void testReadFileFromResources() {
void testReadFileFromResources() throws IOException {
assertEquals("Ya-hoo!", fileRepository.readFileFromResources("readme.txt"));
}

Expand Down
Loading