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
44 changes: 44 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,48 @@
package com.epam.izh.rd.online;

import com.epam.izh.rd.online.repository.SimpleFileRepository;
import com.epam.izh.rd.online.service.SimpleBigNumbersService;
import com.epam.izh.rd.online.service.SimpleDateService;
import com.epam.izh.rd.online.service.SimpleRegExpService;
import com.epam.izh.rd.online.service.SimpleTextService;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {

public static void main(String[] args) {
// SimpleFileRepository simpleFileRepository = new SimpleFileRepository();
// System.out.println(simpleFileRepository.countFilesInDirectory("c:/temp/java/java3"));
// System.out.println(simpleFileRepository.countDirsInDirectory("c:/temp/java/java3/"));
// simpleFileRepository.copyTXTFiles("c:/temp/java/java3/", "c:/temp/java/java4/");
// System.out.println(simpleFileRepository.createFile("c:/temp/java/java3/", "someNewFile.txt"));
// System.out.println(simpleFileRepository.readFileFromResources("readme.txt"));

// SimpleBigNumbersService simpleBigNumbersService = new SimpleBigNumbersService();
// System.out.println(simpleBigNumbersService.getPrecisionNumber(1, 3, 2));
// System.out.println(simpleBigNumbersService.getPrimaryNumber(100));

// SimpleDateService simpleDateService = new SimpleDateService();
// System.out.println(simpleDateService.parseDate(LocalDate.of(1974, 2, 9)));
// System.out.println(simpleDateService.parseString("1970-01-01 00:00"));
// System.out.println(simpleDateService.convertToCustomFormat(LocalDate.of(1974, 2, 9), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
// System.out.println(simpleDateService.getNextLeapYear());
// System.out.println(simpleDateService.getSecondsInYear(2021));
// System.out.println(simpleDateService.getSecondsInYear(2020));

// SimpleRegExpService simpleRegExpService = new SimpleRegExpService();
// System.out.println(simpleRegExpService.maskSensitiveData());
// System.out.println(simpleRegExpService.replacePlaceholders(1, 2));

// SimpleTextService simpleTextService = new SimpleTextService();
// System.out.println(simpleTextService.removeString("Hello, hello, hello, how low?", ", he"));
// System.out.println(simpleTextService.isQuestionString("test!"));
// System.out.println(simpleTextService.concatenate(new String[]{"a", "b", "c"}));
// System.out.println(simpleTextService.toJumpCase("Load Up On Guns And Bring Your Friends"));
// System.out.println(simpleTextService.isPalindrome(""));
}


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

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;

public class SimpleFileRepository implements FileRepository {

/**
Expand All @@ -10,6 +15,21 @@ public class SimpleFileRepository implements FileRepository {
*/
@Override
public long countFilesInDirectory(String path) {
if (!Paths.get(path).toFile().isDirectory()) return 0;

try {
final long[] count = {0};
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor() {
@Override
public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException {
count[0]++;
return super.visitFile(path, attrs);
}
});
return count[0];
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}

Expand All @@ -21,6 +41,21 @@ public long countFilesInDirectory(String path) {
*/
@Override
public long countDirsInDirectory(String path) {
if (!Paths.get(path).toFile().isDirectory()) return 0;

try {
final long[] count = {0};
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor() {
@Override
public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException {
count[0]++;
return super.preVisitDirectory(dir, attrs);
}
});
return count[0];
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}

Expand All @@ -32,7 +67,22 @@ public long countDirsInDirectory(String path) {
*/
@Override
public void copyTXTFiles(String from, String to) {
return;
if (!Paths.get(from).toFile().isDirectory() || !Paths.get(to).toFile().isDirectory()) return;

final int[] countCopiedFiles = {0};
File[] files = Paths.get(from).toFile().listFiles();
if (files == null) return;
Arrays.stream(files)
.filter(file -> !file.isDirectory() && file.toString().toLowerCase().endsWith(".txt"))
.forEach(file -> {
try {
Files.copy(file.toPath(), Paths.get(to + "/" + file.getName()), StandardCopyOption.REPLACE_EXISTING);
countCopiedFiles[0]++;
} catch (IOException e) {
e.printStackTrace();
}
});
System.out.println(countCopiedFiles[0] + " files copied.");
}

/**
Expand All @@ -44,6 +94,19 @@ public void copyTXTFiles(String from, String to) {
*/
@Override
public boolean createFile(String path, String name) {
try {
// the dir is created additionally if it's missing
if (!Paths.get(path).toFile().exists()) {
boolean result = Paths.get(path).toFile().mkdir();
if (!result) {
return false;
}
}

return Paths.get(path + "/" + name).toFile().createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

Expand All @@ -55,6 +118,15 @@ public boolean createFile(String path, String name) {
*/
@Override
public String readFileFromResources(String fileName) {
return null;
if (!fileName.toLowerCase().endsWith(".txt")) return "";

try ( BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/" + fileName))) {
StringBuilder result = new StringBuilder();
reader.lines().forEach(line -> result.append("\n" + line));
return result.toString().substring(1);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SimpleBigNumbersService implements BigNumbersService {
*/
@Override
public BigDecimal getPrecisionNumber(int a, int b, int range) {
return null;
return new BigDecimal((double) a / (double) b).setScale(range, BigDecimal.ROUND_HALF_UP);
}

/**
Expand All @@ -24,6 +24,33 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) {
*/
@Override
public BigInteger getPrimaryNumber(int range) {
return null;
if (range < 1) return null;
int checkNumber = 2;
int result = checkNumber;
int foundCo = 0;
while (foundCo <= range) {
if (isPrimary(checkNumber)) {
result = checkNumber;
foundCo++;
}
checkNumber++;
}
return new BigInteger(String.valueOf(result));
}

/**
* Метод определяет, является ли заданное число простым.
* Простое число - число, которое делится только на 1 и на само себя.
*
* Например для числа 22 вернется false, а для числа 23 true.
*/
public boolean isPrimary(int number) {
for (int i = 2; i < number; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}

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

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
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,10 @@ public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter forma
*/
@Override
public long getNextLeapYear() {
return 0;
LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
while (!LocalDate.of(year++,1,1).isLeapYear());
return --year;
}

/**
Expand All @@ -57,7 +61,8 @@ public long getNextLeapYear() {
*/
@Override
public long getSecondsInYear(int year) {
return 0;
int daySeconds = 3600 * 24;
return LocalDate.of(year,1,1).isLeapYear() ? 366 * daySeconds : 365 * daySeconds;
}


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.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SimpleRegExpService implements RegExpService {

/**
Expand All @@ -11,6 +19,23 @@ public class SimpleRegExpService implements RegExpService {
*/
@Override
public String maskSensitiveData() {
try ( BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/sensitive_data.txt"))) {
Pattern pattern = Pattern.compile("\\d{4} \\d{4} \\d{4} \\d{4}");
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String maskedReplacement = matcher.group().substring(0, 5) +
"**** ****" + matcher.group().substring(14, 19);
line = line.replace(matcher.group(), maskedReplacement);
}
stringBuilder.append("\n" + line);
}
return stringBuilder.toString().substring(1);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

Expand All @@ -22,6 +47,18 @@ public String maskSensitiveData() {
*/
@Override
public String replacePlaceholders(double paymentAmount, double balance) {
try ( BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/sensitive_data.txt"))) {
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
line = line.replaceAll("\\$\\{payment_amount}",String.valueOf((int) paymentAmount));
line = line.replaceAll("\\$\\{balance}",String.valueOf((int) balance));
stringBuilder.append("\n" + line);
}
return stringBuilder.toString().substring(1);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.epam.izh.rd.online.service;

import java.util.Arrays;

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, "");
}

/**
Expand All @@ -24,7 +26,7 @@ public String removeString(String base, String remove) {
*/
@Override
public boolean isQuestionString(String text) {
return false; //TODO
return text.endsWith("?");
}

/**
Expand All @@ -35,7 +37,11 @@ public boolean isQuestionString(String text) {
*/
@Override
public String concatenate(String... elements) {
return null; //TODO
String result = "";
for (String string : elements) {
result += string;
}
return result.trim();
}

/**
Expand All @@ -47,7 +53,18 @@ public String concatenate(String... elements) {
*/
@Override
public String toJumpCase(String text) {
return null; //TODO
String result = "";
boolean isLowerCase = true;
for (int i = 0; i < text.length(); i++) {
if (isLowerCase) {
result += text.substring(i, i + 1).toLowerCase();
isLowerCase = false;
} else {
result += text.substring(i, i + 1).toUpperCase();
isLowerCase = true;
}
}
return result;
}

/**
Expand All @@ -59,6 +76,9 @@ public String toJumpCase(String text) {
*/
@Override
public boolean isPalindrome(String string) {
return false; //TODO
if (string == null) return false;
string = string.replace(" ", "");
if (string.length() == 0) return false;
return string.toLowerCase().equals(new StringBuilder(string.toLowerCase()).reverse().toString());
}
}
Loading