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,13 @@
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 +18,32 @@ 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 +54,30 @@ 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 +88,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 +109,33 @@ 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 +146,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,8 @@

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

public class SimpleBigNumbersService implements BigNumbersService {

Expand All @@ -13,7 +15,8 @@ public class SimpleBigNumbersService implements BigNumbersService {
*/
@Override
public BigDecimal getPrecisionNumber(int a, int b, int range) {
return null;

return new BigDecimal(Integer.toString(a)).divide(new BigDecimal(Integer.toString(b)), range, RoundingMode.HALF_UP);
}

/**
Expand All @@ -24,6 +27,23 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) {
*/
@Override
public BigInteger getPrimaryNumber(int range) {
return null;
BigInteger[] primaryNumbers = new BigInteger[]{new BigInteger("2")};
BigInteger value = new BigInteger("3");
while (primaryNumbers.length <= range) {
Boolean flagPrimary = true;
int count = 0;
while (flagPrimary && count < primaryNumbers.length) {
if (value.remainder(primaryNumbers[count]).compareTo(new BigInteger("0")) == 0) {
flagPrimary = false;
}
count++;
}
if (flagPrimary) {
primaryNumbers = Arrays.copyOf(primaryNumbers, primaryNumbers.length + 1);
primaryNumbers[primaryNumbers.length - 1] = value;
}
value = value.add(new BigInteger("1"));
}
return primaryNumbers[primaryNumbers.length-1];
}
}
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,9 @@ public class SimpleDateService implements DateService {
*/
@Override
public String parseDate(LocalDate localDate) {
return null;

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
return formatter.format(localDate);
}

/**
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:HH");
return LocalDateTime.parse(string, formatter);
}

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

return formatter.format(localDate);
}

/**
Expand All @@ -47,7 +53,13 @@ public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter forma
*/
@Override
public long getNextLeapYear() {
return 0;

Year year = Year.now();
do {
year = year.plusYears(1);
} while (!year.isLeap());

return year.getValue();
}

/**
Expand All @@ -57,7 +69,13 @@ public long getNextLeapYear() {
*/
@Override
public long getSecondsInYear(int year) {
return 0;

Year givenYear = Year.of(year);
if (givenYear.isLeap()) {
return 366 * 24 * 3600;
} else {
return 365 * 24 * 3600;
}
}


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.FileReader;
import java.io.IOException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SimpleRegExpService implements RegExpService {

/**
Expand All @@ -9,9 +16,28 @@ public class SimpleRegExpService implements RegExpService {
*
* @return обработанный текст
*/
private static final String nameFile = "D:\\Epam_Java_Course\\java-data-handling-template\\src\\main\\" +
"resources\\sensitive_data.txt";

@Override
public String maskSensitiveData() {
return null;

StringBuilder outputString = null;

try (BufferedReader reader = new BufferedReader(new FileReader(nameFile))) {

outputString = new StringBuilder(reader.readLine());
Pattern pattern = Pattern.compile("(\\d{4} ){4}");
Matcher matcher = pattern.matcher(outputString);

while (matcher.find()) {
outputString.replace(matcher.start() + 5, matcher.end() - 5, "**** **** ");
}
} catch (IOException e) {
e.printStackTrace();
}

return Objects.requireNonNull(outputString).toString();
}

/**
Expand All @@ -22,6 +48,25 @@ public String maskSensitiveData() {
*/
@Override
public String replacePlaceholders(double paymentAmount, double balance) {
return null;

StringBuilder outputString = null;


try (BufferedReader reader = new BufferedReader(new FileReader(nameFile))) {
outputString = new StringBuilder(reader.readLine());
Pattern pattern = Pattern.compile("(\\$\\{)payment_amount}");
Matcher matcher = pattern.matcher(outputString);
if (!matcher.find()) throw new IOException();
outputString.replace(matcher.start(), matcher.end(), Integer.toString((int) paymentAmount));
pattern = Pattern.compile("(\\$\\{)balance}");
matcher = pattern.matcher(outputString);
if (!matcher.find()) throw new IOException();
outputString.replace(matcher.start(), matcher.end(), Integer.toString((int) balance));
} catch (IOException e) {
e.printStackTrace();
}

return Objects.requireNonNull(outputString).toString();
}

}
Loading