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_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,5 +1,18 @@
package com.epam.izh.rd.online.repository;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

public class SimpleFileRepository implements FileRepository {

/**
Expand All @@ -10,9 +23,26 @@ public class SimpleFileRepository implements FileRepository {
*/
@Override
public long countFilesInDirectory(String path) {
return 0;
long count;
Path filePath = Paths.get(path);
File file;
if (filePath.isAbsolute()){
file = new File(path);
} else {
file = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(path)).getFile());
}

count = new File(file.getPath()).listFiles(File::isFile).length;
List<File> directories = Arrays.asList(new File(file.getPath()).listFiles(File::isDirectory));
for (File directory : directories) {
count += countFilesInDirectory(directory.toPath().toString());

}

return count;
}


/**
* Метод рекурсивно подсчитывает количество папок в директории, считая корень
*
Expand All @@ -21,7 +51,23 @@ public long countFilesInDirectory(String path) {
*/
@Override
public long countDirsInDirectory(String path) {
return 0;
int count = 0;
Path filePath = Paths.get(path);
File file;
if (filePath.isAbsolute()){
file = new File(path);
} else {
count++;
file = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(path)).getFile());
}

List<File> directories = Arrays.asList(Objects.requireNonNull(new File(file.getPath()).listFiles(File::isDirectory)));
count += directories.size();
for(File directory : directories) {
count += countDirsInDirectory(directory.toString());
}

return count;
}

/**
Expand All @@ -43,8 +89,22 @@ public void copyTXTFiles(String from, String to) {
* @return был ли создан файл
*/
@Override
public boolean createFile(String path, String name) {
public boolean createFile(String path, String name){
ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource(path);
File file;
if (resource != null) {
file = new File(resource.getFile(), name);
} else {
file = new File("", name);
} //c
try{
return file.createNewFile();
} catch (IOException e){
System.out.println(e.getMessage());
}
return false;

}

/**
Expand All @@ -55,6 +115,14 @@ public boolean createFile(String path, String name) {
*/
@Override
public String readFileFromResources(String fileName) {
return null;
File file = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(fileName)).getFile());
String value = "";
try (BufferedReader reader = Files.newBufferedReader(file.toPath())) {
value = reader.readLine();
} catch (Exception e) {
System.err.println(e.getMessage());
}
return value;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.math.BigDecimal;
import java.math.BigInteger;

import static java.math.BigDecimal.ROUND_DOWN;

public class SimpleBigNumbersService implements BigNumbersService {

/**
Expand All @@ -13,7 +15,9 @@ public class SimpleBigNumbersService implements BigNumbersService {
*/
@Override
public BigDecimal getPrecisionNumber(int a, int b, int range) {
return null;
BigDecimal devidend = BigDecimal.valueOf(a);
BigDecimal devider = BigDecimal.valueOf(b);
return devidend.divide(devider, range, ROUND_DOWN);
}

/**
Expand All @@ -24,6 +28,10 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) {
*/
@Override
public BigInteger getPrimaryNumber(int range) {
return null;
BigInteger prime = BigInteger.valueOf(2);
for(int i = 0; i < range; i++) {
prime = prime.nextProbablePrime();
}
return prime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;


public class SimpleDateService implements DateService {

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

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

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

/**
Expand All @@ -47,7 +51,9 @@ public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter forma
*/
@Override
public long getNextLeapYear() {
return 0;
int year = Calendar.getInstance().get(Calendar.YEAR);

return year;
}

/**
Expand All @@ -57,7 +63,18 @@ public long getNextLeapYear() {
*/
@Override
public long getSecondsInYear(int year) {
return 0;
if (year %100==0 && year %400 ==0 )
return 31622400;

else if (year % 4 == 0 && year % 100 > 0)
return 31622400;

else if (year % 100 == 0)
return 31536000;

else
return 31536000;

}


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.BufferedReader;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SimpleRegExpService implements RegExpService {

/**
Expand All @@ -11,7 +19,19 @@ public class SimpleRegExpService implements RegExpService {
*/
@Override
public String maskSensitiveData() {
return null;
String fileName = "D:/IdeaProjects/java-data-handling-template1/src/main/resources/sensitive_data.txt";
String value = "";
try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName))) {
value = reader.readLine();
Pattern pattern = Pattern.compile("(\\d{4}) (\\d{4}) (\\d{4}) (\\d{4})");
Matcher matcher = pattern.matcher(value);
if (matcher.find()) {
value = matcher.replaceAll("$1 **** **** $4");
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
return value;
}

/**
Expand All @@ -22,6 +42,33 @@ public String maskSensitiveData() {
*/
@Override
public String replacePlaceholders(double paymentAmount, double balance) {
return null;
String value = readFile();
value = value.replaceAll("\\$\\{payment_amount}", format(paymentAmount));
value = value.replaceAll("\\$\\{balance}", format(balance));
// Pattern pattern = Pattern.compile("\\{\\D+?\\}");
// Matcher matcher = pattern.matcher(value);
// if (matcher.find()){
// value = matcher.replaceAll(String.valueOf(paymentAmount));
// value = matcher.replaceAll(String.valueOf(balance));
// }
return value;
}

private String readFile() {
File file = new File(Objects.requireNonNull(getClass().getClassLoader().getResource("sensitive_data.txt")).getFile());
String value = "";
try (BufferedReader reader = Files.newBufferedReader(file.toPath())) {
value = reader.readLine();
} catch (Exception e) {
System.err.println(e.getMessage());
}
return value;
}

private static String format(double d) {
if (d == (long) d)
return String.format("%d", (long) d);
else
return String.format("%s", d);
}
}
Loading