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.*;
import java.nio.file.Files;

public class SimpleFileRepository implements FileRepository {

/**
Expand All @@ -10,9 +13,23 @@ public class SimpleFileRepository implements FileRepository {
*/
@Override
public long countFilesInDirectory(String path) {
return 0;
long c = 0;
File dir = new File("src/main/resources/" + path);
File[] dirs = dir.listFiles();
if (dirs != null) {
for (File file : dirs) {
if (file.isFile()) {
c++;
}
if (file.isDirectory()) {
c += countFilesInDirectory(path + "/" + file.getName());
}
}
}
return c;
}


/**
* Метод рекурсивно подсчитывает количество папок в директории, считая корень
*
Expand All @@ -21,7 +38,21 @@ public long countFilesInDirectory(String path) {
*/
@Override
public long countDirsInDirectory(String path) {
return 0;

long c = 1;
File dir = new File("src/main/resources/" + path);
File[] dirs = dir.listFiles();
if (dirs != null) {
for (File file : dirs) {
if (file.isDirectory()) {
c++;
}
if (file.isDirectory()) {
c += countDirsInDirectory(path + "/" + file.getName()) - 1;
}
}
}
return c;
}

/**
Expand All @@ -32,7 +63,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,9 +84,19 @@ public void copyTXTFiles(String from, String to) {
*/
@Override
public boolean createFile(String path, String name) {
String targetFolder = "target/classes/";
File dir = new File(targetFolder + path);
dir.mkdir();
File file = new File(dir.getPath() + File.separator + name);
try {
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}


/**
* Метод считывает тело файла .txt из папки src/main/resources
*
Expand All @@ -55,6 +105,17 @@ 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

public class SimpleBigNumbersService implements BigNumbersService {

Expand All @@ -13,7 +14,9 @@ public class SimpleBigNumbersService implements BigNumbersService {
*/
@Override
public BigDecimal getPrecisionNumber(int a, int b, int range) {
return null;
BigDecimal aBig = new BigDecimal(a);
BigDecimal bBig = new BigDecimal(b);
return aBig.divide(bBig, range, RoundingMode.HALF_UP);
}

/**
Expand All @@ -24,6 +27,10 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) {
*/
@Override
public BigInteger getPrimaryNumber(int range) {
return null;
BigInteger simpleNum = new BigInteger("2");
for(int i = 0; i < range; i++){
simpleNum = simpleNum.nextProbablePrime();
}
return simpleNum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -14,7 +15,7 @@ public class SimpleDateService implements DateService {
*/
@Override
public String parseDate(LocalDate localDate) {
return null;
return localDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
}

/**
Expand All @@ -25,7 +26,8 @@ public String parseDate(LocalDate localDate) {
*/
@Override
public LocalDateTime parseString(String string) {
return null;

return LocalDateTime.parse(string, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
}

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

/**
Expand All @@ -47,18 +49,28 @@ public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter forma
*/
@Override
public long getNextLeapYear() {
return 0;
long year = Year.now().getValue();
do {
year++;
} while (!Year.isLeap(year));

return year;
}


/**
* Метод считает число секунд в заданном году
*
* @return число секунд
*/
@Override
public long getSecondsInYear(int year) {
return 0;
}

int daysInYear = 365;
if (Year.isLeap(year)) {
daysInYear = 366;
}
return daysInYear * 24 * 3600;

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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SimpleRegExpService implements RegExpService {

/**
Expand All @@ -11,6 +20,24 @@ public class SimpleRegExpService implements RegExpService {
*/
@Override
public String maskSensitiveData() {
File file = new File("src/main/resources", "sensitive_data.txt");
Pattern pattern = Pattern.compile("\\d{4}\\s(\\d{4}?)\\s(\\d{4}?)\\s\\d{4}");
Matcher matcher;
int counter = 0;
try {
List<String> result = Files.readAllLines(file.toPath());
while (counter < result.size()){
matcher = pattern.matcher(result.get(counter));
while (matcher.find()) {
String[] parts = matcher.group().split(" ");
result.set(counter, result.get(counter).replaceFirst(String.valueOf(pattern), parts[0] + " **** **** " + parts[parts.length - 1]));
}
counter++;
}
return result.get(0);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

Expand All @@ -22,6 +49,21 @@ public String maskSensitiveData() {
*/
@Override
public String replacePlaceholders(double paymentAmount, double balance) {
return null;
String text = "";
try (BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/sensitive_data.txt"))) {
text = reader.readLine();
Pattern pattern = Pattern.compile("\\$\\{.*?}");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
if (matcher.group().equals("${payment_amount}")) {
text = text.replaceAll("\\$\\{payment_amount}", String.valueOf((int)paymentAmount));
} else if (matcher.group().equals("${balance}")) {
text = text.replaceAll("\\$\\{balance}", String.valueOf((int)balance));
}
}
} catch (IOException e) {
e.printStackTrace();
}
return text;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.epam.izh.rd.online.service;

import java.util.Locale;

import static java.awt.SystemColor.text;

public class SimpleTextService implements TextService {

/**
Expand All @@ -12,8 +16,8 @@ public class SimpleTextService implements TextService {
* @param remove - строка которую необходимо удалить
*/
@Override
public String removeString(String base, String remove) {
return null; //TODO
public String removeString(String base, String remove)
{ return base.replaceAll(remove, "");
}

/**
Expand All @@ -23,9 +27,9 @@ public String removeString(String base, String remove) {
* Например для строки "Hello, hello, hello!" метод вернет false
*/
@Override
public boolean isQuestionString(String text) {
return false; //TODO
}
public boolean isQuestionString(String text){
return text.endsWith("?");
}

/**
* Реализовать функционал соединения переданных строк.
Expand All @@ -35,7 +39,11 @@ public boolean isQuestionString(String text) {
*/
@Override
public String concatenate(String... elements) {
return null; //TODO
StringBuilder aBuilder = new StringBuilder();
for (int i = 0; i < elements.length; i++) {
aBuilder.append(elements[i]);
}
return aBuilder.toString();
}

/**
Expand All @@ -47,7 +55,11 @@ public String concatenate(String... elements) {
*/
@Override
public String toJumpCase(String text) {
return null; //TODO
char[] chars = text.toLowerCase().toCharArray();
for (int i = 1; i < chars.length; i += 2) {
chars[i] = Character.toUpperCase(chars[i]);
}
return new String(chars);
}

/**
Expand All @@ -59,6 +71,11 @@ public String toJumpCase(String text) {
*/
@Override
public boolean isPalindrome(String string) {
return false; //TODO
String raw = string.toLowerCase(Locale.ROOT).replace(" ", "").replace(",", "");
String palindrome = new StringBuilder(raw).reverse().toString();
if (raw.isEmpty()) {
return false;
}
return palindrome.equals(raw);
}
}