complete project
This commit is contained in:
commit
a1047da9f3
2
.idea/.gitignore
vendored
Normal file
2
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
# Default ignored files
|
||||
/workspace.xml
|
13
.idea/compiler.xml
Normal file
13
.idea/compiler.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="projekt1" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
14
.idea/misc.xml
Normal file
14
.idea/misc.xml
Normal file
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
1
peopleData.txt
Normal file
1
peopleData.txt
Normal file
@ -0,0 +1 @@
|
||||
asdd, a s 90081900834
|
36
pom.xml
Normal file
36
pom.xml
Normal file
@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>projekt1</groupId>
|
||||
<artifactId>projekt1</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
96
src/main/java/company/DataSaver.java
Normal file
96
src/main/java/company/DataSaver.java
Normal file
@ -0,0 +1,96 @@
|
||||
package company;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class DataSaver {
|
||||
|
||||
public DataSaver(String fileName) {
|
||||
_fileName = fileName;
|
||||
}
|
||||
|
||||
public void deleteFileIfExists() {
|
||||
File fileToDelete = new File(_fileName);
|
||||
|
||||
if (fileToDelete.exists())
|
||||
fileToDelete.delete();
|
||||
}
|
||||
|
||||
public void deleteRowIfPeselExists(String pesel) throws IOException {
|
||||
File file = new File(_fileName);
|
||||
if (!file.exists())
|
||||
return;
|
||||
|
||||
FileReader fileReader = new FileReader(file);
|
||||
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
||||
int lineNumber = 0;
|
||||
int lineToDelete = -1;
|
||||
String line = null;
|
||||
String lines = "";
|
||||
boolean containsPesel = false;
|
||||
|
||||
while (true) {
|
||||
lineNumber++;
|
||||
line = bufferedReader.readLine();
|
||||
if (line == null)
|
||||
break;
|
||||
|
||||
if (line.contains(pesel)) {
|
||||
containsPesel = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
lines += line + "\n";
|
||||
}
|
||||
|
||||
if (!containsPesel)
|
||||
return;
|
||||
|
||||
fileReader.close();
|
||||
bufferedReader.close();
|
||||
|
||||
FileWriter fileWriter = new FileWriter(file, false);
|
||||
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
|
||||
bufferedWriter.write(lines);
|
||||
|
||||
bufferedWriter.close();
|
||||
fileWriter.close();
|
||||
}
|
||||
|
||||
public void savePersonalDataToFile(String city, String personalData) throws IOException {
|
||||
File file = new File(_fileName);
|
||||
FileWriter fileWriter = new FileWriter(file, true);
|
||||
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
|
||||
|
||||
bufferedWriter.write(city + ", " + personalData + "\n");
|
||||
|
||||
bufferedWriter.close();
|
||||
fileWriter.close();
|
||||
}
|
||||
|
||||
private int getLineNumberWithText(File file, String stringToSearch) throws IOException {
|
||||
FileReader fileReader = new FileReader(file);
|
||||
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
||||
String line = null;
|
||||
int lineNumber = 0;
|
||||
int lineToDelete = -1;
|
||||
|
||||
while (true) {
|
||||
lineNumber++;
|
||||
line = bufferedReader.readLine();
|
||||
if (line == null)
|
||||
break;
|
||||
|
||||
if (line.contains(stringToSearch)) {
|
||||
lineToDelete = lineNumber;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fileReader.close();
|
||||
bufferedReader.close();
|
||||
|
||||
return lineToDelete;
|
||||
}
|
||||
|
||||
private String _fileName;
|
||||
}
|
56
src/main/java/company/Main.java
Normal file
56
src/main/java/company/Main.java
Normal file
@ -0,0 +1,56 @@
|
||||
package company;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import static company.PeselValidator.validate;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
||||
DataSaver dataSaver = new DataSaver("peopleData.txt");
|
||||
|
||||
String city;
|
||||
String personalData;
|
||||
|
||||
System.out.println("Aby zakończyć wpisz STOP");
|
||||
dataSaver.deleteFileIfExists();
|
||||
|
||||
while (true) {
|
||||
|
||||
System.out.println("PODAJ MIEJSCOWOWSC:");
|
||||
city = readData(reader);
|
||||
if(checkBreak(city)) return;
|
||||
|
||||
System.out.println("PODAJ IMIE I NAZWISKO ORAZ PESEL ODDZIELONE SPACJĄ:");
|
||||
personalData = readData(reader);
|
||||
if (checkBreak(personalData)) return;
|
||||
|
||||
var splitedData = personalData.split(" ");
|
||||
var pesel = splitedData[splitedData.length - 1];
|
||||
|
||||
if (!validate(pesel)) {
|
||||
System.out.println("Błąd - niepoprawny numer PESEL. Dane nie zostaną zapisane.");
|
||||
continue;
|
||||
}
|
||||
|
||||
dataSaver.deleteRowIfPeselExists(pesel);
|
||||
dataSaver.savePersonalDataToFile(city, personalData);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static boolean checkBreak(String word) {
|
||||
if (word.equalsIgnoreCase("stop")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String readData(BufferedReader reader) throws IOException {
|
||||
return reader.readLine();
|
||||
}
|
||||
}
|
31
src/main/java/company/PeselValidator.java
Normal file
31
src/main/java/company/PeselValidator.java
Normal file
@ -0,0 +1,31 @@
|
||||
package company;
|
||||
|
||||
public class PeselValidator {
|
||||
|
||||
public static boolean validate(String pesel) {
|
||||
if (pesel == null || pesel.length() != 11)
|
||||
return false;
|
||||
|
||||
return validateControlSum(pesel);
|
||||
}
|
||||
|
||||
private static boolean validateControlSum(String pesel) {
|
||||
int controlSum = 0;
|
||||
var weightings = new int[] { 1, 3, 7, 9, 1, 3, 7, 9, 1, 3 };
|
||||
var peselArray = pesel.toCharArray();
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
controlSum += Character.getNumericValue(peselArray[i]) * weightings[i];
|
||||
}
|
||||
|
||||
controlSum = controlSum % 10;
|
||||
controlSum = 10 - controlSum;
|
||||
controlSum = controlSum % 10;
|
||||
|
||||
if (controlSum == Character.getNumericValue(peselArray[10]))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
73
src/test/java/company/DataSaverTests.java
Normal file
73
src/test/java/company/DataSaverTests.java
Normal file
@ -0,0 +1,73 @@
|
||||
package company;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public class DataSaverTests {
|
||||
private DataSaver dataSaver;
|
||||
private String testFileName = "test.txt";
|
||||
|
||||
@BeforeEach
|
||||
public void initTests() throws IOException {
|
||||
dataSaver = new DataSaver(testFileName);
|
||||
|
||||
addToTestFileInitialValues();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void savePersonalDataToFile_ShouldSaveData() throws IOException {
|
||||
var city = "Warszawa";
|
||||
var personalData = "Jan Nowak 11223300456";
|
||||
dataSaver.savePersonalDataToFile(city, personalData);
|
||||
|
||||
var textToFind = city + ", " + personalData;
|
||||
|
||||
assertTrue(checkFileContainsText(textToFind));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteRowIfPeselExists_ShouldDeleteRowWithPesel() throws IOException {
|
||||
assertTrue(checkFileContainsText("Szczecin, Robert Kowalski 11223344567"));
|
||||
|
||||
dataSaver.deleteRowIfPeselExists("11223344567");
|
||||
|
||||
assertFalse(checkFileContainsText("Szczecin, Robert Kowalski 11223344567"));
|
||||
}
|
||||
|
||||
private void addToTestFileInitialValues() throws IOException {
|
||||
File file = new File(testFileName);
|
||||
FileWriter fileWriter = new FileWriter(file, true);
|
||||
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
|
||||
|
||||
bufferedWriter.write("Szczecin, Robert Kowalski 11223344567\n");
|
||||
bufferedWriter.write("Sopot, Witold Barman 99887766543\n");
|
||||
|
||||
bufferedWriter.close();
|
||||
fileWriter.close();
|
||||
}
|
||||
|
||||
private boolean checkFileContainsText(String textToCheck) throws IOException {
|
||||
File file = new File(testFileName);
|
||||
if (!file.exists())
|
||||
return false;
|
||||
|
||||
FileReader fileReader = new FileReader(file);
|
||||
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
||||
|
||||
while (true) {
|
||||
String line = bufferedReader.readLine();
|
||||
if (line == null)
|
||||
return false;
|
||||
|
||||
if (line.contains(textToCheck))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
48
src/test/java/company/PeselValidatorTests.java
Normal file
48
src/test/java/company/PeselValidatorTests.java
Normal file
@ -0,0 +1,48 @@
|
||||
package company;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
|
||||
import static company.PeselValidator.validate;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public class PeselValidatorTests {
|
||||
|
||||
@Test
|
||||
public void validate_ShouldFail_IfLengthIsDifferentThan11() {
|
||||
String invalidPesels[] = { "777", "123456789000" };
|
||||
|
||||
for (var pesel: invalidPesels) {
|
||||
var result = validate(pesel);
|
||||
assertFalse(result);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validate_ShouldFail_IfControlSumIsInvalid() {
|
||||
String invalidPesels[] = { "12345678911", "84475900006", "33333210000" };
|
||||
|
||||
for (var pesel: invalidPesels) {
|
||||
var result = validate(pesel);
|
||||
assertFalse(result);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validate_ShouldFail_IfNull() {
|
||||
var result = validate(null);
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validate_ShouldSuccess() {
|
||||
String invalidPesels[] = { "92021585321", "86041014912", "04292933532" };
|
||||
|
||||
for (var pesel: invalidPesels) {
|
||||
var result = validate(pesel);
|
||||
assertTrue(result);
|
||||
}
|
||||
}
|
||||
}
|
BIN
target/classes/company/DataSaver.class
Normal file
BIN
target/classes/company/DataSaver.class
Normal file
Binary file not shown.
BIN
target/classes/company/Main.class
Normal file
BIN
target/classes/company/Main.class
Normal file
Binary file not shown.
BIN
target/classes/company/PeselValidator.class
Normal file
BIN
target/classes/company/PeselValidator.class
Normal file
Binary file not shown.
@ -0,0 +1,3 @@
|
||||
company\DataSaver.class
|
||||
company\Main.class
|
||||
company\PeselValidator.class
|
@ -0,0 +1,3 @@
|
||||
J:\IdeaProjects\projekt1\src\main\java\company\PeselValidator.java
|
||||
J:\IdeaProjects\projekt1\src\main\java\company\DataSaver.java
|
||||
J:\IdeaProjects\projekt1\src\main\java\company\Main.java
|
@ -0,0 +1,2 @@
|
||||
company\PeselValidatorTests.class
|
||||
company\DataSaverTests.class
|
@ -0,0 +1,2 @@
|
||||
J:\IdeaProjects\projekt1\src\test\java\company\PeselValidatorTests.java
|
||||
J:\IdeaProjects\projekt1\src\test\java\company\DataSaverTests.java
|
BIN
target/test-classes/company/DataSaverTests.class
Normal file
BIN
target/test-classes/company/DataSaverTests.class
Normal file
Binary file not shown.
BIN
target/test-classes/company/PeselValidatorTests.class
Normal file
BIN
target/test-classes/company/PeselValidatorTests.class
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user