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
371 changes: 371 additions & 0 deletions src/main/java/com/epam/izh/rd/online/Main.java

Large diffs are not rendered by default.

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.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

public class SimpleFileRepository implements FileRepository {

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

// The base working directory is 'src\main\resources\'
String basePath = "src"+ File.separator + "main" + File.separator + "resources" + File.separator;

// Path from the root folder of the project is base 'basePath' + given as a parameter relative 'path'
String dirPath = basePath + path;

long counter = 0;

File f = new File(dirPath);

if (!f.exists() || f.isFile()) { return 0;} // If there is no such directory or path points to a file, return 0
else { // If path points to existing directory -->

File[] listOfElements = f.listFiles(); // Getting elements from the directory

for ( File element : listOfElements ) { // Scanning elements from directory
if (element.isDirectory()) { // Element is a directory itself -->
String relativePath = element.getPath().replace(basePath,""); // Getting path of subdirectory relative to basePath
counter += countFilesInDirectory(relativePath); // Add number of files from subdirectory to the counter
} else { // Element is a file --> increment counter by 1
counter++;
}
}
}

return counter;
}

/**
Expand All @@ -21,7 +54,31 @@ public long countFilesInDirectory(String path) {
*/
@Override
public long countDirsInDirectory(String path) {
return 0;

// The base working directory is 'src\main\resources\'
String basePath = "src"+ File.separator + "main" + File.separator + "resources" + File.separator;

// Path from the root folder of the project is base 'basePath' + given as a parameter relative 'path'
String dirPath = basePath + path;

long counter = 1; // Counting the given as a parameter root folder

File f = new File(dirPath);

if (!f.exists() || f.isFile()) { return 0;} // If there is no such directory or path points to a file, return 0
else { // If path points to existing directory -->

File[] listOfElements = f.listFiles(); // Getting elements from the directory

for ( File element : listOfElements ) { // Scanning elements from directory
if (element.isDirectory()) { // Element is a directory itself -->
String relativePath = element.getPath().replace(basePath,""); // Getting path of subdirectory relative to basePath
counter += countDirsInDirectory(relativePath); // Adding number of directories from subdirectory to the counter
}
}
}

return counter;
}

/**
Expand All @@ -32,6 +89,31 @@ public long countDirsInDirectory(String path) {
*/
@Override
public void copyTXTFiles(String from, String to) {

File fromDir = new File(from);

if (!(fromDir.exists() && fromDir.isDirectory())) { return; }

File toDir = new File(to);

if (!(toDir.exists() && toDir.isDirectory())) {
if (!toDir.mkdir()) {
System.out.println(":( Failed to create destination folder '" + toDir.getPath() + "'!");
return;
}
}

for (File f : fromDir.listFiles()) {
if (f.isFile() && f.getName().endsWith(".txt")) {
File copy = new File(to + "\\" + f.getName());
try {
copy.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return;
}

Expand All @@ -44,7 +126,44 @@ public void copyTXTFiles(String from, String to) {
*/
@Override
public boolean createFile(String path, String name) {
return false;

// Path 'dirPath' from the root of the project is base 'basePath' + given as a parameter relative 'path'
String basePath = "src/main/resources/";
String dirPath = basePath + path;

// Getting resources
URL resource = getClass().getResource("/");
URI resURI = null;

try {
resURI = resource.toURI(); // URI for resources
} catch (URISyntaxException e) {
e.printStackTrace();
return false;
}

// Getting path to compiled resources folder (target\classes\...)
Path targetPath = Paths.get(resURI);
String targetDir = targetPath.toString() + "\\" + path;

File dirFile = new File(dirPath);

if (!(dirFile.exists() && dirFile.isDirectory())) {
if(!dirFile.mkdir()) { return false; } // Trying to create folder if there is none
}

File f = new File(dirPath + "/" + name);

if (f.exists() && f.isFile()) { f.delete(); } // Cleaning old version of the file

try {
f.createNewFile();
copyTXTFiles(dirPath, targetDir); // Copying file into compiled resources folder ( target\classes\... )
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

/**
Expand All @@ -55,6 +174,29 @@ public boolean createFile(String path, String name) {
*/
@Override
public String readFileFromResources(String fileName) {
return null;

// Path to resources from the root of the project
String basePath = "src/main/resources/";

try (
FileReader reader = new FileReader(basePath + fileName);
BufferedReader bufferedReader = new BufferedReader(reader))
{
String content = "";
String line = "";

while ((line = bufferedReader.readLine()) != null) {
content += line;
}

return content;

} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ public class SimpleBigNumbersService implements BigNumbersService {
*/
@Override
public BigDecimal getPrecisionNumber(int a, int b, int range) {
return null;

if (b == 0) {
System.out.println("Недопустимая операция: деление на ноль ( " + a + " / 0 )!");
return null;
} else {
return new BigDecimal(a).divide(new BigDecimal(b), range, BigDecimal.ROUND_HALF_UP);
}
}

/**
Expand All @@ -24,6 +30,16 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) {
*/
@Override
public BigInteger getPrimaryNumber(int range) {

for (int i = 2, n = 0; n <= range; i++) {

BigInteger temp = new BigInteger(String.valueOf(i));

if (temp.isProbablePrime(10)) {
if (n++ == range) { return temp; }
}
}

return null;
}
}
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,7 @@ public class SimpleDateService implements DateService {
*/
@Override
public String parseDate(LocalDate localDate) {
return null;
return localDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
}

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

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

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

int thisYear = Year.now().getValue();

while (!Year.of(thisYear).isLeap()) {
thisYear++;
}

return thisYear;
}

/**
Expand All @@ -57,7 +65,7 @@ public long getNextLeapYear() {
*/
@Override
public long getSecondsInYear(int year) {
return 0;
return Year.of(year).length()*24*60*60;
}


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

import com.epam.izh.rd.online.repository.SimpleFileRepository;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SimpleRegExpService implements RegExpService {

/**
Expand All @@ -11,7 +15,26 @@ public class SimpleRegExpService implements RegExpService {
*/
@Override
public String maskSensitiveData() {
return null;

// Using methods of another class object to work with files
SimpleFileRepository rep4File = new SimpleFileRepository();

String content = ""; // Variable to put in the data from the file

// Reading data from file
content = rep4File.readFileFromResources("sensitive_data.txt");

// Regular expression to identify account numbers
String regex = "\\d{4}\\s(\\d{4}\\s\\d{4})\\s\\d{4}"; // Group 1 is to be masked by asterisks

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);

while (matcher.find()) {
content = content.replaceAll(matcher.group(1), "**** ****");
}

return content;
}

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

// Using methods of another class object to work with files
SimpleFileRepository rep4File = new SimpleFileRepository();

String content = ""; // Variable to put in the data from the file

// Reading data from file
content = rep4File.readFileFromResources("sensitive_data.txt");

// Regular expression to identify account numbers
String regex = "\\$\\{([a-z_0-9]+)\\}"; // Group 0 is to be replaced, group 1 is for identification

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);

while (matcher.find()) {
String group = Pattern.quote(matcher.group()); // Quoting found substring with metacharacters
if(matcher.group(1).intern() == "payment_amount") {
content = content.replaceAll(group, String.valueOf((int)paymentAmount));
}
if(matcher.group(1).intern() == "balance") {
content = content.replaceAll(group, String.valueOf((int)balance));
}
}

return content;
}
}
Loading