Implementacja i testy

This commit is contained in:
Bartosz Chyzy 2020-03-28 11:33:42 +01:00
commit 2fcfefe44b
18 changed files with 235 additions and 0 deletions

2
Project1/.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# Default ignored files
/workspace.xml

View File

@ -0,0 +1 @@
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

9
Project1/.idea/misc.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Project1.iml" filepath="$PROJECT_DIR$/Project1.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,3 @@
<template>
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
</template>

6
Project1/.idea/vcs.xml Normal file
View 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>

37
Project1/Project1.iml Normal file
View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit5.4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.4.2/junit-jupiter-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.4.2/junit-jupiter-api-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.1.1/opentest4j-1.1.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.4.2/junit-platform-commons-1.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.4.2/junit-jupiter-params-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.4.2/junit-jupiter-engine-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.4.2/junit-platform-engine-1.4.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

View File

@ -0,0 +1,70 @@
package com.s434666;
import com.s434666.models.Person;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
private static PeselValidator peselValidator;
public static void main(String[] args) {
peselValidator = new PeselValidator();
var people = readPeople();
if(saveToFile(people))
System.out.println("Pomyślnie zapisano liste do pliku");
else
System.out.println("Nie udało się zapisać listy do pliku");
}
private static Iterable<Person> readPeople(){
var people = new TreeMap<String, Person>();
var scanner = new Scanner(System.in);
while (true){
System.out.println("Miasto: ");
var city = scanner.nextLine();
System.out.println("Imię: ");
var name = scanner.nextLine();
System.out.println("Nazwisko: ");
var surname = scanner.nextLine();
System.out.println("PESEL: ");
var pesel = scanner.nextLine();
if(peselValidator.validatePesel(pesel))
people.put(pesel, new Person(city, name,surname, pesel));
else
System.out.println("Niepoprawny PESEL! Ta osoba nie zostanie dodana do bazy!");
System.out.println("Czy chcesz dodać koleją osobę? y - tak");
if(scanner.nextLine().trim().toUpperCase().charAt(0) != 'Y')
break;
}
return people.values();
}
private static Boolean saveToFile(Iterable<Person> people){
var file = new File("lista.txt");
try {
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
for (Person person : people) {
bufferedWriter.write(person.print());
bufferedWriter.newLine();
}
bufferedWriter.close();
} catch (Exception ex){
return false;
}
return true;
}
}

View File

@ -0,0 +1,20 @@
package com.s434666;
import static org.junit.jupiter.api.Assertions.*;
import com.s434666.models.Person;
import org.junit.*;
public class PersonTest {
@Test
public void personPrintCorrentString(){
var city = "Warszawa";
var name = "Andrzej";
var surname = "Nowak";
var pesel = "763648363842";
var person = new Person(city,name,surname,pesel);
var personPrint = String.format("City: %s, Name: %s, Surname: %s, PESEL: %s", city,name,surname, pesel);
assertEquals(person.print(),personPrint);
}
}

View File

@ -0,0 +1,23 @@
package com.s434666;
import java.util.stream.IntStream;
public class PeselValidator {
public Boolean validatePesel(String pesel){
if(pesel.length()!=11)
return false;
try{
var splitedNumbers = pesel.chars().map(c-> Character.getNumericValue(c)).toArray();
var factors = new int[]{9,7,3,1,9,7,3,1,9,7};
//DLACZEGO W JAVIE NIE MA ZIP WITH?! :((((
var controlSum = IntStream.range(0,10).map(i->factors[i]*splitedNumbers[i]).sum();
return controlSum%10 == splitedNumbers[10];
}
catch(Exception ex){
return false;
}
}
}

View File

@ -0,0 +1,31 @@
package com.s434666;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.*;
public class PeselValidatorTest {
PeselValidator peselValidator;
@Before
public void setUp(){
peselValidator = new PeselValidator();
}
@Test
public void validatePeselInvalidPeselReturnsFalse(){
assertFalse(peselValidator.validatePesel(""));
assertFalse(peselValidator.validatePesel("TO NIE JEST PESEL"));
assertFalse(peselValidator.validatePesel("50121876785"));
assertFalse(peselValidator.validatePesel("5012183435436781"));
assertFalse(peselValidator.validatePesel("50034781"));
}
@Test
public void validatePeselValidPeselStringReturnsTrue(){
assertTrue(peselValidator.validatePesel("93020998613"));
assertTrue(peselValidator.validatePesel("55112035952"));
assertTrue(peselValidator.validatePesel("62092707537"));
assertTrue(peselValidator.validatePesel("50121876781"));
}
}

View File

@ -0,0 +1,19 @@
package com.s434666.models;
public class Person {
private String city;
private String firstName;
private String surname;
private String pesel;
public Person(String city, String firstName, String surname, String pesel) {
this.city = city;
this.firstName = firstName;
this.surname = surname;
this.pesel = pesel;
}
public String print(){
return String.format("City: %s, Name: %s, Surname: %s, PESEL: %s", city,firstName,surname, pesel);
}
}