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
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_8">
<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>
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.epam.izh.rd.online.repository;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;

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 count = 0;
try {
Files.walk(Paths.get(path))
.filter(Files::isRegularFile)
.count();
} catch (IOException e) {
e.printStackTrace();
return 0;
}
return count;
}

/**
Expand All @@ -21,7 +34,16 @@ public long countFilesInDirectory(String path) {
*/
@Override
public long countDirsInDirectory(String path) {
return 0;
long count = 0;
try {
Files.walk(Paths.get(path))
.filter(p -> p.toFile().isDirectory())
.count();
} catch (Exception e) {
e.printStackTrace();
return 0;
}
return count;
}

/**
Expand All @@ -32,7 +54,22 @@ public long countDirsInDirectory(String path) {
*/
@Override
public void copyTXTFiles(String from, String to) {
return;
Path fromPath = Paths.get(from).normalize();
Path toPath = Paths.get(to).normalize();
if (toPath.getParent() != null) {
if (fromPath.endsWith(".txt") && (Files.notExists(toPath.getParent()))) {
try {
Files.createDirectories(toPath.getParent());
} catch (IOException e) {
e.printStackTrace();
}
try {
Files.copy(fromPath, toPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
Expand All @@ -44,9 +81,47 @@ public void copyTXTFiles(String from, String to) {
*/
@Override
public boolean createFile(String path, String name) {
return false;
boolean isCreateDir = false;
boolean isCreateFile = false;
Path dirPath = Paths.get(System.getProperty("user.dir") + File.separator + path).normalize();
Path filePath = Paths.get(dirPath + File.separator + name).normalize();

if (Files.notExists(dirPath)) {
try {
Files.createDirectories(dirPath);
isCreateDir = Files.exists(dirPath);
} catch (IOException e) {
isCreateDir = Files.exists(dirPath);
e.printStackTrace();
}
if (Files.notExists(filePath)) {
try {
Files.createFile(filePath);
isCreateFile = Files.exists(filePath);
} catch (IOException e) {
isCreateFile = Files.exists(filePath);
e.printStackTrace();
}
}

} else if (Files.exists(dirPath) && Files.notExists(filePath)) {
isCreateDir = Files.exists(dirPath);
try {
Files.createFile(filePath);
isCreateFile = Files.exists(filePath);
} catch (IOException e) {
isCreateFile = Files.exists(filePath);
e.printStackTrace();
}

} else {
isCreateDir = Files.exists(dirPath);
isCreateFile = Files.exists(filePath);
}
return (isCreateDir && isCreateFile);
}


/**
* Метод считывает тело файла .txt из папки src/main/resources
*
Expand All @@ -55,6 +130,13 @@ public boolean createFile(String path, String name) {
*/
@Override
public String readFileFromResources(String fileName) {
return null;
Path path = Paths.get("src/main/resources" + File.separator + fileName);
String read = "";
try {
read = Files.readAllLines(path).get(0);
} catch (IOException e) {
e.printStackTrace();
}
return read;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ public class SimpleBigNumbersService implements BigNumbersService {
/**
* Метод делит первое число на второе с заданной точностью
* Например 1/3 с точностью 2 = 0.33
*
* @param range точность
* @return результат
*/
@Override
public BigDecimal getPrecisionNumber(int a, int b, int range) {
return null;
BigDecimal aa = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
return aa.divide(bb, range, BigDecimal.ROUND_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 temp = new BigInteger("2");
for (int i = 0; i < range; i++) {
temp = temp.nextProbablePrime();
}
return temp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

public class SimpleDateService implements DateService {

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

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

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

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

return yearForCheck;
}

/**
Expand All @@ -57,7 +68,8 @@ public long getNextLeapYear() {
*/
@Override
public long getSecondsInYear(int year) {
return 0;
long secondsInYear = TimeUnit.DAYS.toSeconds(Year.of(year).length());
return secondsInYear;
}


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

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SimpleRegExpService implements RegExpService {

/**
Expand All @@ -11,7 +19,20 @@ public class SimpleRegExpService implements RegExpService {
*/
@Override
public String maskSensitiveData() {
return null;
Pattern paymentAmountAndBalance = Pattern.compile("(?<=\\d{4}\\s)(\\d{4}\\s\\d{4})(?=\\s\\d{4})");
Path sensitivePath = Paths.get("src" + File.separator + "main" + File.separator + "resources" + File.separator + "sensitive_data.txt");
String read = "";
try {
read = Files.readAllLines(sensitivePath).get(0);
} catch (IOException e) {
e.printStackTrace();
}
Matcher source = paymentAmountAndBalance.matcher(read);
boolean isMatch = source.find();
if (isMatch) {
read = read.replaceAll(paymentAmountAndBalance.pattern().toString(), "\\*\\*\\*\\* \\*\\*\\*\\*");
}
return read;
}

/**
Expand All @@ -22,6 +43,24 @@ public String maskSensitiveData() {
*/
@Override
public String replacePlaceholders(double paymentAmount, double balance) {
return null;
int roundPaymentAmount = (int) Math.round(paymentAmount);
int roundBalance = (int) Math.round(balance);
Pattern paymentAmountAndBalance = Pattern.compile("\\$\\{payment_amount}|\\$\\{balance}");
String stringPaymentAmount = String.valueOf(roundPaymentAmount);
String stringBalance = String.valueOf(roundBalance);
Path sensitivePath = Paths.get("src" + File.separator + "main" + File.separator + "resources" + File.separator + "sensitive_data.txt");
String read = "";
try {
read = Files.readAllLines(sensitivePath).get(0);
} catch (IOException e) {
e.printStackTrace();
}
Matcher source = paymentAmountAndBalance.matcher(read);
boolean isMatch = source.find();
if (isMatch) {
read = read.replaceAll("\\$\\{payment_amount}", stringPaymentAmount)
.replaceAll("\\$\\{balance}", stringBalance);
}
return read;
}
}
Loading