Skip to content
Open

Ok! #29

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,13 @@
package com.epam.izh.rd.online.repository;

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;

public class SimpleFileRepository implements FileRepository {

/**
Expand All @@ -10,7 +18,15 @@ public class SimpleFileRepository implements FileRepository {
*/
@Override
public long countFilesInDirectory(String path) {
return 0;
File[] files = new File("src/main/resources/" + path).listFiles();
long count = 0;
for (File file : files) {
if (file.isFile()) count++;
else {
count += countFilesInDirectory(path + "/" + file.getName());
}
}
return count;
}

/**
Expand All @@ -21,7 +37,14 @@ public long countFilesInDirectory(String path) {
*/
@Override
public long countDirsInDirectory(String path) {
return 0;
File[] files = new File("src/main/resources/" + path).listFiles();
long count = 1;
for (File file : files) {
if (file.isDirectory()) {
count += countDirsInDirectory(path + "/" + file.getName());
}
}
return count;
}

/**
Expand All @@ -32,7 +55,17 @@ public long countDirsInDirectory(String path) {
*/
@Override
public void copyTXTFiles(String from, String to) {
return;
File fileTo = new File(new File(to).getParent());
if (!fileTo.exists())fileTo.mkdir();
File[] files = new File(new File(from).getParent()).listFiles();
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".txt")) {
try {
Files.copy(file.toPath(), Paths.get(to));
} catch (IOException e) {
}
}
}
}

/**
Expand All @@ -44,6 +77,14 @@ public void copyTXTFiles(String from, String to) {
*/
@Override
public boolean createFile(String path, String name) {
File folder = new File(getClass().getResource("/").getPath() + "/" + path);
if (!folder.exists()) folder.mkdir();
File file = new File(folder.getPath() + "/" + name);
try {
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

Expand All @@ -55,6 +96,16 @@ public boolean createFile(String path, String name) {
*/
@Override
public String readFileFromResources(String fileName) {
return null;
String str = "";
try (FileReader fileReader = new FileReader(new File("src/main/resources/" + fileName))) {
int c = 0;
while ((c=fileReader.read())!=-1){
str += String.valueOf((char) c);
}
} catch (IOException e) {
e.printStackTrace();
}

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

/**
Expand All @@ -24,6 +25,10 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) {
*/
@Override
public BigInteger getPrimaryNumber(int range) {
return null;
BigInteger integer = new BigInteger("2");
for (int i = 0; i < range; i++) {
integer = integer.nextProbablePrime();
}
return integer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
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 format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
return LocalDateTime.parse(string,format);
}

/**
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,11 @@ public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter forma
*/
@Override
public long getNextLeapYear() {
return 0;
int year = Calendar.getInstance().get(Calendar.YEAR) + 1;
while (!Year.isLeap(year)) {
year++;
}
return year;
}

/**
Expand All @@ -57,7 +65,9 @@ public long getNextLeapYear() {
*/
@Override
public long getSecondsInYear(int year) {
return 0;
int day = 365;
if (Year.of(year).isLeap()) day = 366;
return day*24*60*60;
}


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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class SimpleRegExpService implements RegExpService {

/**
Expand All @@ -11,7 +16,17 @@ public class SimpleRegExpService implements RegExpService {
*/
@Override
public String maskSensitiveData() {
return null;
String regex = "(\\d+)(\\s\\d+)(\\s\\d+)\\s(\\d+)";
String replacement = "$1 **** **** $4";
String str = "";
try {
BufferedReader reader = new BufferedReader( new FileReader(
new File("src/main/resources/sensitive_data.txt")));
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str.replaceAll(regex,replacement);
}

/**
Expand All @@ -22,6 +37,18 @@ public String maskSensitiveData() {
*/
@Override
public String replacePlaceholders(double paymentAmount, double balance) {
return null;
String regex1 = "${payment_amount}";
String regex2 = "${balance}";
String str = "";
try {
BufferedReader reader = new BufferedReader( new FileReader(
new File("src/main/resources/sensitive_data.txt")));
str = reader.readLine();
str = str.replace(regex1,String.valueOf( (int) paymentAmount));
str = str.replace(regex2,String.valueOf( (int) balance));
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SimpleTextService implements TextService {
*/
@Override
public String removeString(String base, String remove) {
return null; //TODO
return base.replaceAll(remove,""); //TODO
}

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

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

/**
Expand All @@ -47,7 +52,11 @@ public String concatenate(String... elements) {
*/
@Override
public String toJumpCase(String text) {
return null; //TODO
char[] chars = text.toLowerCase().toCharArray();
for (int i = 1; i < chars.length; i += 2) {
chars[i] = Character.toUpperCase(chars[i]);
}
return new String(chars); //TODO
}

/**
Expand All @@ -59,6 +68,15 @@ public String toJumpCase(String text) {
*/
@Override
public boolean isPalindrome(String string) {
return false; //TODO
string = string.toLowerCase().replaceAll(" ","");
char[] chars = string.toCharArray();
int count = 0;
for (int i = 0, j = chars.length - 1; i < chars.length;j--, i++) {
if (chars[i] == chars[j]) {
count++;
}
}
if (count == 0) return false;
return count==chars.length; //TODO
}
}