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>
5 changes: 5 additions & 0 deletions src/main/java/com/epam/izh/rd/online/Main.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package com.epam.izh.rd.online;



public class Main {
public static void main(String[] args) {

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


import java.io.*;
import java.util.Objects;

public class SimpleFileRepository implements FileRepository {

/**
Expand All @@ -8,9 +12,18 @@ public class SimpleFileRepository implements FileRepository {
* @param path путь до директори
* @return файлов, в том числе скрытых
*/

@Override
public long countFilesInDirectory(String path) {
return 0;
File file = new File(Objects.requireNonNull(getClass()
.getClassLoader().getResource(path)).getFile());
int count = 0;
for (File f : Objects.requireNonNull(file.listFiles())) {
if (f.isDirectory()) {
count += countFilesInDirectory(path + "/" + f.getName());
} else count++;
}
return count;
}

/**
Expand All @@ -21,7 +34,16 @@ public long countFilesInDirectory(String path) {
*/
@Override
public long countDirsInDirectory(String path) {
return 0;
File file = new File(Objects.requireNonNull(getClass().getClassLoader()
.getResource(path)).getFile());
int count = 0;
if (file.isDirectory()) {
for (File f : Objects.requireNonNull(file.listFiles())) {
count += countDirsInDirectory(path + "/" + f.getName());
}
count++;
}
return count;
}

/**
Expand All @@ -32,7 +54,7 @@ public long countDirsInDirectory(String path) {
*/
@Override
public void copyTXTFiles(String from, String to) {
return;

}

/**
Expand All @@ -44,6 +66,13 @@ public void copyTXTFiles(String from, String to) {
*/
@Override
public boolean createFile(String path, String name) {
File file = new File(Objects.requireNonNull(getClass()
.getClassLoader().getResource(path)).getFile() + File.separator + name);
try {
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

Expand All @@ -55,6 +84,14 @@ public boolean createFile(String path, String name) {
*/
@Override
public String readFileFromResources(String fileName) {
return null;
String path = Objects.requireNonNull(getClass().getClassLoader()
.getResource(fileName)).getPath();
String readStr = null;
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
readStr = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return readStr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

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

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;
return new BigDecimal(a).divide(new BigDecimal(b), range, RoundingMode.FLOOR);
}

/**
Expand All @@ -24,6 +26,14 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) {
*/
@Override
public BigInteger getPrimaryNumber(int range) {
return null;
BigInteger one = new BigInteger("1");
BigInteger two = new BigInteger("2");
while (range > 0) {
one = one.add(two);
if (one.isProbablePrime(3)) {
range--;
}
}
return one;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.stream.IntStream;

import static java.time.LocalDate.now;

public class SimpleDateService implements DateService {

Expand All @@ -14,7 +18,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 +29,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 +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,10 @@ public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter forma
*/
@Override
public long getNextLeapYear() {
return 0;
return IntStream.range(now().getYear(), now().getYear() + 8)
.filter(Year::isLeap)
.findFirst()
.getAsInt();
}

/**
Expand All @@ -57,8 +64,12 @@ public long getNextLeapYear() {
*/
@Override
public long getSecondsInYear(int year) {
return 0;
long seconds;
if (Year.isLeap(year)) {
seconds = 366 * 24 * 60 * 60;
} else {
seconds = 365 * 24 * 60 * 60;
}
return seconds;
}


}
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.FileNotFoundException;
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 @@ -11,7 +19,22 @@ public class SimpleRegExpService implements RegExpService {
*/
@Override
public String maskSensitiveData() {
return null;
String path = Objects.requireNonNull(getClass().getClassLoader()
.getResource("sensitive_data.txt")).getPath();
Pattern pattern = Pattern.compile("\\d{4}\\s(\\d{4}\\s\\d{4})\\s\\d{4}");
StringBuffer buf = new StringBuffer();
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
String readStr = reader.readLine();
Matcher matcher = pattern.matcher(readStr);
while (matcher.find()) {
matcher.appendReplacement(buf, matcher.group().
replaceAll(matcher.group(1), "**** ****"));
}
matcher.appendTail(buf);
} catch (IOException e) {
e.printStackTrace();
}
return buf.toString();
}

/**
Expand All @@ -22,6 +45,16 @@ public String maskSensitiveData() {
*/
@Override
public String replacePlaceholders(double paymentAmount, double balance) {
String path = Objects.requireNonNull(getClass().getClassLoader()
.getResource("sensitive_data.txt")).getPath();
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path))) {
String line = bufferedReader.readLine();
line = line.replaceAll("(\\$\\{(payment_amount)})", String.valueOf((int) paymentAmount));
line = line.replaceAll("(\\$\\{(balance)})", String.valueOf((int) balance));
return line;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
43 changes: 31 additions & 12 deletions src/main/java/com/epam/izh/rd/online/service/SimpleTextService.java
Original file line number Diff line number Diff line change
@@ -1,64 +1,83 @@
package com.epam.izh.rd.online.service;


public class SimpleTextService implements TextService {

/**
* Реализовать функционал удаления строки из другой строки.
*
* <p>
* Например для базовой строки "Hello, hello, hello, how low?" и строки для удаления ", he"
* метод вернет "Hellollollo, how low?"
*
* @param base - базовая строка с текстом
* @param base - базовая строка с текстом
* @param remove - строка которую необходимо удалить
*/
@Override
public String removeString(String base, String remove) {
return null; //TODO
return base.replaceAll(remove, "");
}

/**
* Реализовать функционал проверки на то, что строка заканчивается знаком вопроса.
*
* <p>
* Например для строки "Hello, hello, hello, how low?" метод вернет true
* Например для строки "Hello, hello, hello!" метод вернет false
*/
@Override
public boolean isQuestionString(String text) {
return false; //TODO
return text.endsWith("?");
}

/**
* Реализовать функционал соединения переданных строк.
*
* <p>
* Например для параметров {"Smells", " ", "Like", " ", "Teen", " ", "Spirit"}
* метод вернет "Smells Like Teen Spirit"
*/
@Override
public String concatenate(String... elements) {
return null; //TODO
StringBuilder strB = new StringBuilder();
for (String s : elements) {
strB.append(s);
}
return strB.toString();
}

/**
* Реализовать функционал изменения регистра в вид лесенки.
* Возвращаемый текст должен начинаться с прописного регистра.
*
* <p>
* Например для строки "Load Up On Guns And Bring Your Friends"
* метод вернет "lOaD Up oN GuNs aNd bRiNg yOuR FrIeNdS".
*/
@Override
public String toJumpCase(String text) {
return null; //TODO
String[] str = text.toLowerCase().split("");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < str.length; i++) {
if (i % 2 != 0) {
builder.append(str[i].toUpperCase());
} else {
builder.append(str[i]);
}
}
return builder.toString();
}

/**
* Метод определяет, является ли строка палиндромом.
*
* <p>
* Палиндром - строка, которая одинаково читается слева направо и справа налево.
*
* <p>
* Например для строки "а роза упала на лапу Азора" вернется true, а для "я не палиндром" false
*/
@Override
public boolean isPalindrome(String string) {
return false; //TODO
if (string.isEmpty()) {
return false;
}
return string.replaceAll(" ", "")
.equalsIgnoreCase(new StringBuilder(string.replaceAll(" ", ""))
.reverse().toString());
}
}