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,13 @@
package com.epam.izh.rd.online.repository;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;

public class SimpleFileRepository implements FileRepository {

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

/**
Expand All @@ -21,9 +38,17 @@ public long countFilesInDirectory(String path) {
*/
@Override
public long countDirsInDirectory(String path) {
return 0;
long count = 1;
File dir = new File("./src/main/resources/" + path);
for (File items : dir.listFiles()) {
if (items.isDirectory()) {
count += countDirsInDirectory(path + "/" + items.getName());
}
}
return count;
}


/**
* Метод копирует все файлы с расширением .txt
*
Expand All @@ -32,7 +57,20 @@ public long countDirsInDirectory(String path) {
*/
@Override
public void copyTXTFiles(String from, String to) {
return;
File dirFrom = new File(from).getParentFile();
File dirTo = new File(to).getParentFile();
if (!dirTo.exists()) dirTo.mkdirs();
File[] files = dirFrom.listFiles();
if (files == null) return;
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".txt")) {
try{
Files.copy(file.toPath(), new File(dirTo + "/" + file.getName()).toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
Expand All @@ -44,6 +82,17 @@ public void copyTXTFiles(String from, String to) {
*/
@Override
public boolean createFile(String path, String name) {
URL aUrl = getClass().getResource("/");
File dir = new File(aUrl.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 @@ -55,6 +104,16 @@ public boolean createFile(String path, String name) {
*/
@Override
public String readFileFromResources(String fileName) {
File aFile = new File("src/main/resources/" + fileName);
try {
BufferedReader bfReader = new BufferedReader(new FileReader(aFile));
String str = bfReader.readLine();
return str;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
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,11 @@ public class SimpleBigNumbersService implements BigNumbersService {
*/
@Override
public BigDecimal getPrecisionNumber(int a, int b, int range) {
return null;
int resDivision = a / b;
if (b == 0) {
return BigDecimal.valueOf(0);
}
return BigDecimal.valueOf(a).divide(BigDecimal.valueOf(b), range, RoundingMode.HALF_UP);
}

/**
Expand All @@ -24,6 +29,10 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) {
*/
@Override
public BigInteger getPrimaryNumber(int range) {
return null;
BigInteger res = new BigInteger("2");
for (int i = 0; i < range; i++) {
res = res.nextProbablePrime();
}
return res;
}
}
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,8 @@ public class SimpleDateService implements DateService {
*/
@Override
public String parseDate(LocalDate localDate) {
return null;
DateTimeFormatter formatDate = DateTimeFormatter.ofPattern("dd-MM-yyyy");
return formatDate.format(localDate);
}

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

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

/**
Expand All @@ -47,18 +50,23 @@ public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter forma
*/
@Override
public long getNextLeapYear() {
for (int i = 2022; i < 2025; i++) {
if(Year.of(i).isLeap()) {
return i;
}
}
return 0;
}



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


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

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

public class SimpleRegExpService implements RegExpService {

/**
Expand All @@ -11,7 +19,13 @@ public class SimpleRegExpService implements RegExpService {
*/
@Override
public String maskSensitiveData() {
return null;
String msg = readFile();
Pattern aPattern = Pattern.compile("(\\d{4}\\s)(\\d{4}\\s\\d{4})(\\s\\d{4})");
Matcher aMatcher = aPattern.matcher(msg);
while (aMatcher.find()) {
msg = msg.replaceAll(aMatcher.group(2), "**** ****");
}
return msg;
}

/**
Expand All @@ -22,6 +36,21 @@ public String maskSensitiveData() {
*/
@Override
public String replacePlaceholders(double paymentAmount, double balance) {
return null;
String readFile = readFile();
BigDecimal pay = new BigDecimal(paymentAmount).setScale(0, RoundingMode.HALF_UP);
BigDecimal bal = new BigDecimal(balance).setScale(0, RoundingMode.HALF_UP);
readFile = readFile.replaceAll("(\\$\\{(payment_amount)\\})", String.valueOf(pay));
readFile = readFile.replaceAll("(\\$\\{(balance)\\})", String.valueOf(bal));
return readFile;
}

public String readFile() {
String line = null;
try (BufferedReader aBuffer = new BufferedReader(new FileReader("src/main/resources/sensitive_data.txt"))) {
line = aBuffer.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
}
36 changes: 24 additions & 12 deletions src/main/java/com/epam/izh/rd/online/service/SimpleTextService.java
Original file line number Diff line number Diff line change
@@ -1,64 +1,76 @@
package com.epam.izh.rd.online.service;

import java.util.Locale;

public class SimpleTextService implements TextService {

/**
* Реализовать функционал удаления строки из другой строки.
*
* <p>
* Например для базовой строки "Hello, hello, hello, how low?" и строки для удаления ", he"
* метод вернет "Hellollollo, how low?"
*
* @param base - базовая строка с текстом
* @param base - базовая строка с текстом
* @param remove - строка которую необходимо удалить
*/
@Override
public String removeString(String base, String remove) {
return null; //TODO
return base.replaceAll(remove, "");
}

/**
* Реализовать функционал проверки на то, что строка заканчивается знаком вопроса.
*
* <p>
* Например для строки "Hello, hello, hello, how low?" метод вернет true
* Например для строки "Hello, hello, hello!" метод вернет false
*/
@Override
public boolean isQuestionString(String text) {
return false; //TODO
return text.endsWith("?");
}

/**
* Реализовать функционал соединения переданных строк.
*
* <p>
* Например для параметров {"Smells", " ", "Like", " ", "Teen", " ", "Spirit"}
* метод вернет "Smells Like Teen Spirit"
*/
@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();
}

/**
* Реализовать функционал изменения регистра в вид лесенки.
* Возвращаемый текст должен начинаться с прописного регистра.
*
* <p>
* Например для строки "Load Up On Guns And Bring Your Friends"
* метод вернет "lOaD Up oN GuNs aNd bRiNg yOuR FrIeNdS".
*/
@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);
}

/**
* Метод определяет, является ли строка палиндромом.
*
* <p>
* Палиндром - строка, которая одинаково читается слева направо и справа налево.
*
* <p>
* Например для строки "а роза упала на лапу Азора" вернется true, а для "я не палиндром" false
*/
@Override
public boolean isPalindrome(String string) {
return false; //TODO
StringBuilder aBuilder = new StringBuilder(string.replaceAll(" ", ""));
aBuilder.reverse();
return string.replaceAll(" ", "").equalsIgnoreCase(String.valueOf(aBuilder))&&!string.equals("");
}
}