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
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package com.epam.izh.rd.online.repository;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
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.Objects;

public class SimpleFileRepository implements FileRepository {

/**
Expand All @@ -10,7 +21,18 @@ public class SimpleFileRepository implements FileRepository {
*/
@Override
public long countFilesInDirectory(String path) {
return 0;
URL resource = getClass().getClassLoader().getResource(path);
File dir = new File(resource.getFile());
if (!dir.exists()) return 0L;
if (dir.isFile()) return 1L;
if (dir.isDirectory()) {
File[] dirs = dir.listFiles();
if (dirs == null || dirs.length == 0) return 0L;
return Arrays.stream(dirs)
.map(d -> countFilesInDirectory(path + "/" + d.getName()))
.reduce(Long::sum).orElse(0L);
}
return 0L;
}

/**
Expand All @@ -21,7 +43,18 @@ public long countFilesInDirectory(String path) {
*/
@Override
public long countDirsInDirectory(String path) {
return 0;
URL resource = getClass().getClassLoader().getResource(path);
File dir = new File(resource.getFile());
if (!dir.exists()) return 0L;
if (dir.isFile()) return 0L;
if (dir.isDirectory()) {
File[] dirs = dir.listFiles();
if (dirs == null || dirs.length == 0) return 1L;
return Arrays.stream(dirs)
.map(d -> countDirsInDirectory(path + "/" + d.getName()))
.reduce(Long::sum).orElse(0L) + 1L;
}
return 0L;
}

/**
Expand All @@ -32,7 +65,23 @@ public long countDirsInDirectory(String path) {
*/
@Override
public void copyTXTFiles(String from, String to) {
return;
File fileFrom = new File(from);
File fileTo = new File(to);
Path pathFrom = Paths.get(fileFrom.getParent());
Path pathTo = Paths.get(fileTo.getParent());
try {
if (!Files.exists(pathTo)) {
Files.createDirectories(pathTo);
}
for (File item : Objects.requireNonNull(pathFrom.toFile().listFiles())) {
if (item.getAbsolutePath().endsWith(".txt")) {
Files.copy(fileFrom.toPath(), fileTo.toPath());
}
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}

}

/**
Expand All @@ -44,7 +93,17 @@ public void copyTXTFiles(String from, String to) {
*/
@Override
public boolean createFile(String path, String name) {
return false;
URL resource = getClass().getClassLoader().getResource("");
File dir = new File(resource.getFile() + "/" + path);
File file = new File(resource.getPath() + "/" + path, name);
try {
if (dir.mkdirs() || dir.exists()) {
file.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
return file.exists();
}

/**
Expand All @@ -55,6 +114,15 @@ public boolean createFile(String path, String name) {
*/
@Override
public String readFileFromResources(String fileName) {
return null;
String pathFile = "./src/main/resources/" + fileName;
StringBuilder content = new StringBuilder();
try (BufferedReader bw = new BufferedReader(new FileReader(pathFile))) {
String buff = null;
while ((buff = bw.readLine()) != null) content.append(buff);
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
return content.toString();
}
}
}
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.math.RoundingMode;

public class SimpleBigNumbersService implements BigNumbersService {

Expand All @@ -13,7 +14,7 @@ public class SimpleBigNumbersService implements BigNumbersService {
*/
@Override
public BigDecimal getPrecisionNumber(int a, int b, int range) {
return null;
return new BigDecimal((double) a / b).setScale(range, RoundingMode.HALF_UP);
}

/**
Expand All @@ -22,8 +23,23 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) {
* @param range номер числа, считая с числа 2
* @return простое число
*/
public static boolean isPrime(int number){
for(int i=2; i<number; i++){
if(number%i == 0){
return false;
}
}
return true;
}

@Override
public BigInteger getPrimaryNumber(int range) {
return null;
int i = 0, num = 2;
while (i <= range) {
if (isPrime(num++)) {
i++;
}
}
return BigInteger.valueOf(--num);
}
}
}
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,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 +27,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 +40,7 @@ public LocalDateTime parseString(String string) {
*/
@Override
public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter formatter) {
return null;
return localDate.format(formatter);
}

/**
Expand All @@ -47,7 +50,15 @@ public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter forma
*/
@Override
public long getNextLeapYear() {
return 0;
long year = LocalDate.now().getYear();
if (Year.isLeap(year)) return year;
else {
while(!Year.isLeap(year)) {
year = (year % 100 == 0 ? ++year : year + (year % 4));
}
}

return year;
}

/**
Expand All @@ -57,8 +68,9 @@ public long getNextLeapYear() {
*/
@Override
public long getSecondsInYear(int year) {
return 0;
return (long) LocalDate.of(year, 1, 1).lengthOfYear() * 24 * 3600;

}


}
}
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.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,24 @@ public class SimpleRegExpService implements RegExpService {
*/
@Override
public String maskSensitiveData() {
return null;
StringBuilder result = null;
try {
File file = new File("C:/Users/Roma/IdeaProjects/java-data-handling-template/src/main/resources/sensitive_data.txt");
FileReader fr = new FileReader(file);
BufferedReader reader = new BufferedReader(fr);

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

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

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

/**
Expand All @@ -22,6 +47,25 @@ public String maskSensitiveData() {
*/
@Override
public String replacePlaceholders(double paymentAmount, double balance) {
return null;
StringBuilder result = null;
try {
File file = new File("C:/Users/Roma/IdeaProjects/java-data-handling-template/src/main/resources/sensitive_data.txt");
FileReader fr = new FileReader(file);
BufferedReader reader = new BufferedReader(fr);

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

return Objects.requireNonNull(result).toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.epam.izh.rd.online.service;

import java.util.Locale;

public class SimpleTextService implements TextService {

/**
Expand All @@ -13,7 +15,7 @@ public class SimpleTextService implements TextService {
*/
@Override
public String removeString(String base, String remove) {
return null; //TODO
return base.replace(remove, ""); //TODO
}

/**
Expand All @@ -24,7 +26,7 @@ public String removeString(String base, String remove) {
*/
@Override
public boolean isQuestionString(String text) {
return false; //TODO
return text.lastIndexOf("?") == text.length() - 1 && text.length() != 0; //TODO
}

/**
Expand All @@ -35,7 +37,7 @@ public boolean isQuestionString(String text) {
*/
@Override
public String concatenate(String... elements) {
return null; //TODO
return String.join("", elements); //TODO
}

/**
Expand All @@ -47,8 +49,15 @@ public String concatenate(String... elements) {
*/
@Override
public String toJumpCase(String text) {
return null; //TODO
}
String result = "";
for (int i = 0; i < text.length() - 1;) {
result += text.substring(i, i + 1).toLowerCase();
result += text.substring(i + 1, i + 2).toUpperCase();
i += 2;
}
return result; //TODO

} //TODO

/**
* Метод определяет, является ли строка палиндромом.
Expand All @@ -59,6 +68,14 @@ public String toJumpCase(String text) {
*/
@Override
public boolean isPalindrome(String string) {
return false; //TODO
if (string.length() > 0) {
string = string.toLowerCase().replaceAll("\\s+", "");
String invertedString = "";
for (int i = 0; i < string.length(); i++) {
invertedString = string.charAt(i) + invertedString;
}
return string.equals(invertedString);
}
return false;
}
}
}