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
4 changes: 4 additions & 0 deletions fileRepository/copy/TestFileToCopy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test
1
2
текст
23 changes: 23 additions & 0 deletions java-data-handling-template.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_9">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.5.2" level="project" />
</component>
</module>
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.epam.izh.rd.online.repository;

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

public interface FileRepository {


long countFilesInDirectory(String path);

long countDirsInDirectory(String path);

void copyTXTFiles(String from, String to);
void copyTXTFiles(String from, String to) throws IOException;

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

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;



public class SimpleFileRepository implements FileRepository {

/**
Expand All @@ -10,9 +17,23 @@ public class SimpleFileRepository implements FileRepository {
*/
@Override
public long countFilesInDirectory(String path) {
return 0;

long filesThisDir=0;
File f = new File(path);
File[] files = f.listFiles();
for (File item : files) {
if (item.isFile()) {
filesThisDir++;
}
if (item.isDirectory()) {
filesThisDir += countFilesInDirectory(item.getAbsolutePath());
}
}
return filesThisDir;

}


/**
* Метод рекурсивно подсчитывает количество папок в директории, считая корень
*
Expand All @@ -21,7 +42,17 @@ public long countFilesInDirectory(String path) {
*/
@Override
public long countDirsInDirectory(String path) {
return 0;
File file = new File (path);
File[] files = file.listFiles();
long DirsThisDir = 1;

for(File item: files)
{
if (item.isDirectory()) {
DirsThisDir += countDirsInDirectory(item.getAbsolutePath());
}
}
return DirsThisDir;
}

/**
Expand All @@ -31,8 +62,20 @@ public long countDirsInDirectory(String path) {
* @param to путь куда
*/
@Override
public void copyTXTFiles(String from, String to) {
return;
public void copyTXTFiles(String from, String to) throws IOException {
File fileFrom = new File(from);
File fileTo = new File(to);
Path pathFrom = Paths.get(fileFrom.getParent());
Path pathTo = Paths.get(fileTo.getParent());

if (!Files.exists(pathTo)) {
Files.createDirectories(pathTo);
}
for(File item: pathFrom.toFile().listFiles()){
if(item.getAbsolutePath().endsWith(".txt")) {
Files.copy(fileFrom.toPath(), fileTo.toPath());
}
}
}

/**
Expand All @@ -43,8 +86,17 @@ public void copyTXTFiles(String from, String to) {
* @return был ли создан файл
*/
@Override
public boolean createFile(String path, String name) {
return false;
public boolean createFile(String path, String name) throws IOException {
File file = new File(path+"./"+name);
Path dir = Paths.get(file.getParent());
if (!Files.exists(dir)) {
Files.createDirectory(dir);
}
if (!file.exists()) {
file.createNewFile();
return true;
}
return file.exists();
}

/**
Expand All @@ -54,7 +106,17 @@ public boolean createFile(String path, String name) {
* @return контент
*/
@Override
public String readFileFromResources(String fileName) {
return null;
public String readFileFromResources(String fileName) throws IOException {
String absoluteFilePath = "C:/java-data-handling-template/src/main/resources/" + fileName;
String content = new String();
FileReader fileReader = new FileReader(absoluteFilePath);
BufferedReader buffReader = new BufferedReader(fileReader);
try {
while (buffReader.ready()) {
content = buffReader.readLine();
}
}catch (IOException e){System.out.println(e.getMessage());}
buffReader.close();
return content;
}
}
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,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 firstNumber = new BigDecimal(a);
BigDecimal secondNumber = new BigDecimal(b);
return firstNumber.divide(secondNumber, range, RoundingMode.HALF_UP);
}

/**
Expand All @@ -24,6 +27,24 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) {
*/
@Override
public BigInteger getPrimaryNumber(int range) {
return null;
Integer[] arr = new Integer[range+1];
int numberArr = 1;
int i = 3;
boolean isPrime = true;
while (arr[range] == null){
for (int j = 2; j <= i/j; j++){
if (i % j == 0 ) {
isPrime = false;
}

}
if (isPrime){
arr[numberArr] = i ;
numberArr++;
}
i++;
isPrime = true;
}
return BigInteger.valueOf(arr[range]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


public class SimpleDateService implements DateService {

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

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

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

/**
Expand All @@ -47,7 +52,14 @@ public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter forma
*/
@Override
public long getNextLeapYear() {
return 0;
LocalDate temp = LocalDate.now();
long year = temp.getYear() + 1;
while(true) {
if ((year % 400 == 0 && year % 100 == 0) || (year % 4 == 0 && year % 100 > 0)) {
return year;
}
year++;
}
}

/**
Expand All @@ -57,7 +69,11 @@ public long getNextLeapYear() {
*/
@Override
public long getSecondsInYear(int year) {
return 0;
if ((year % 400 == 0 && year % 100 == 0) || (year % 4 == 0 && year % 100 > 0)) {
return 366*24*60*60;
} else {
return 365*24*60*60;
}
}


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

import java.io.*;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SimpleRegExpService implements RegExpService {

/**
Expand All @@ -10,8 +15,22 @@ public class SimpleRegExpService implements RegExpService {
* @return обработанный текст
*/
@Override
public String maskSensitiveData() {
return null;
public String maskSensitiveData() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("C:/java-data-handling-template/src/main/resources/sensitive_data.txt"));
String content = br.readLine();
br.close();

Pattern pattern = Pattern.compile("\\d{4} \\d{4} \\d{4} \\d{4}");
Matcher matcher = pattern.matcher(content);

StringBuffer sbNewContent = new StringBuffer(content.length());
String sNewContent = "";
while (matcher.find()){
sNewContent = matcher.group().substring(0,4) + " **** " + "**** " + matcher.group().substring(15,19);
matcher.appendReplacement(sbNewContent, sNewContent);
}
matcher.appendTail(sbNewContent);
return sbNewContent.toString();
}

/**
Expand All @@ -21,7 +40,20 @@ public String maskSensitiveData() {
* @return обработанный текст
*/
@Override
public String replacePlaceholders(double paymentAmount, double balance) {
return null;
public String replacePlaceholders(double paymentAmount, double balance) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("C:/java-data-handling-template/src/main/resources/sensitive_data.txt"));
String content = bufferedReader.readLine();
bufferedReader.close();
String sPaymentAmount = DecimalFormat.getNumberInstance().format(paymentAmount);
String sBalance = DecimalFormat.getInstance().format(balance);

Pattern pPayment = Pattern.compile("\\$\\{payment_amount\\}");
Matcher mPayment = pPayment.matcher(content);
content = mPayment.replaceAll(sPaymentAmount);

Pattern pBalance = Pattern.compile("\\$\\{balance\\}");
Matcher mBalance = pBalance.matcher(content);
content = mBalance.replaceAll(sBalance);
return content;
}
}
Loading