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

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class SimpleFileRepository implements FileRepository {

/**
Expand All @@ -10,7 +17,31 @@ public class SimpleFileRepository implements FileRepository {
*/
@Override
public long countFilesInDirectory(String path) {
return 0;
Path pathResult = Paths.get(path);
if (!pathResult.isAbsolute()) {

URL url = getClass().getClassLoader().getResource(path);
try {
assert url != null;
Path pathAbsolut = Paths.get(url.toURI());
path = pathAbsolut.toString();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}

File dir = new File(path);
long countFiles = 0;
if (dir.exists() && dir.isDirectory()) {
for (File innerDir: dir.listFiles()) {
if (innerDir.isFile()) {
countFiles++;
} else {
countFiles += new SimpleFileRepository().countFilesInDirectory(innerDir.getPath());
}
}
}
return countFiles;
}

/**
Expand All @@ -21,7 +52,29 @@ public long countFilesInDirectory(String path) {
*/
@Override
public long countDirsInDirectory(String path) {
return 0;
Path pathResult = Paths.get(path);
if (!pathResult.isAbsolute()) {

URL url = getClass().getClassLoader().getResource(path);
try {
assert url != null;
Path pathAbsolut = Paths.get(url.toURI());
path = pathAbsolut.toString();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}

File dir = new File(path);
long countDirs = 1;
if (dir.exists() && dir.isDirectory()) {
for (File innerDir: dir.listFiles()) {
if (innerDir.isDirectory()) {
countDirs += new SimpleFileRepository().countDirsInDirectory(innerDir.getPath());
}
}
}
return countDirs;
}

/**
Expand All @@ -32,7 +85,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,7 +106,32 @@ public void copyTXTFiles(String from, String to) {
*/
@Override
public boolean createFile(String path, String name) {
return false;
Path pathResultPath = Paths.get(path);

if (!pathResultPath.isAbsolute()) {

try {
Path pathAbsolut = Paths.get(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
path = pathAbsolut.toString() + "\\" + path;
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
File file = new File(path, name);

if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (!file.exists()) {
try {
file.createNewFile();
return file.exists();

} catch (IOException e) {
e.printStackTrace();
}
}
return file.exists();
}

/**
Expand All @@ -55,6 +142,19 @@ 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.util.ArrayList;

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 = BigDecimal.valueOf(a);
BigDecimal secondNumber = BigDecimal.valueOf(b);
return firstNumber.divide(secondNumber, range, BigDecimal.ROUND_HALF_UP);
}

/**
Expand All @@ -24,6 +27,14 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) {
*/
@Override
public BigInteger getPrimaryNumber(int range) {
return null;
int[] numbers = new int[range * range];
ArrayList<BigInteger> arrayNumbers = new ArrayList<>();
for (int i = 2; i < numbers.length; i++) {
numbers[i] = i;
if (BigInteger.valueOf(numbers[i]).isProbablePrime(i)) {
arrayNumbers.add(BigInteger.valueOf(numbers[i]));
}
}
return arrayNumbers.get(range);
}
}
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,13 @@ public class SimpleDateService implements DateService {
*/
@Override
public String parseDate(LocalDate localDate) {
return null;
String month = "0";
if (localDate.getMonthValue() < 10) {
month = month + localDate.getMonthValue();
} else {
month = "" + localDate.getMonthValue();
}
return String.format("%d-%2s-%d", localDate.getDayOfMonth(), month, localDate.getYear());
}

/**
Expand All @@ -25,7 +32,12 @@ public String parseDate(LocalDate localDate) {
*/
@Override
public LocalDateTime parseString(String string) {
return null;
int year = Integer.parseInt(string.substring(0, 4));
int mount = Integer.parseInt(string.substring(5, 7));
int days = Integer.parseInt(string.substring(8, 10));
int hours = Integer.parseInt(string.substring(11, 13));
int minutes = Integer.parseInt(string.substring(14, 16));
return LocalDateTime.of(year, mount, days, hours, minutes);
}

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

/**
Expand All @@ -47,7 +59,12 @@ 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;
}

/**
Expand All @@ -57,7 +74,15 @@ public long getNextLeapYear() {
*/
@Override
public long getSecondsInYear(int year) {
return 0;
int daysInYear = 365;
int hoursInDay = 24;
int minutesInHours = 60;
int secondsInMinutes = 60;
if (Year.isLeap(year)) {
daysInYear = 366;
}
return daysInYear * hoursInDay * minutesInHours * secondsInMinutes;

}


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

import java.io.BufferedReader;
import java.io.FileNotFoundException;
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 @@ -11,7 +18,23 @@ public class SimpleRegExpService implements RegExpService {
*/
@Override
public String maskSensitiveData() {
return null;
String allMaskedText = "";
try (FileReader fileReader = new FileReader("src/main/resources/sensitive_data.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
) {
String allText = bufferedReader.readLine();
Pattern pattern = Pattern.compile("\\b([0-9]{4})\\s[0-9]{0,9}\\s[0-9]{0,9}\\s([0-9]{4})\\b");
Matcher matcher = pattern.matcher(allText);
String maskForCard = "$1 **** **** $2";
if (matcher.find()) {
allMaskedText = matcher.replaceAll(maskForCard);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return allMaskedText;
}

/**
Expand All @@ -22,6 +45,28 @@ public String maskSensitiveData() {
*/
@Override
public String replacePlaceholders(double paymentAmount, double balance) {
return null;
String replacePlaceholderAllText = "";
try (FileReader fileReader = new FileReader("src/main/resources/sensitive_data.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
) {
String allText = bufferedReader.readLine();
Pattern patternForBalance = Pattern.compile("\\$\\{balance\\}");
Matcher matcherBalance = patternForBalance.matcher(allText);
String balanceReplace = String.format("%.0f", balance);
String paymentReplace = String.format("%.0f", paymentAmount);
if (matcherBalance.find()) {
String replacePlaceholderBalance = matcherBalance.replaceAll(balanceReplace);
Pattern patternForPayment = Pattern.compile("\\$\\{payment_amount\\}");
Matcher matcherPayment = patternForPayment.matcher(replacePlaceholderBalance);
if (matcherPayment.find()) {
replacePlaceholderAllText = matcherPayment.replaceAll(paymentReplace);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return replacePlaceholderAllText;
}
}
Loading