commit 2fcfefe44b39303259329d5a67b60f22bd07b331 Author: Bartosz Chyzy Date: Sat Mar 28 11:33:42 2020 +0100 Implementacja i testy diff --git a/Project1/.idea/.gitignore b/Project1/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/Project1/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/Project1/.idea/description.html b/Project1/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/Project1/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/Project1/.idea/encodings.xml b/Project1/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/Project1/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Project1/.idea/misc.xml b/Project1/.idea/misc.xml new file mode 100644 index 0000000..bf392ff --- /dev/null +++ b/Project1/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/Project1/.idea/modules.xml b/Project1/.idea/modules.xml new file mode 100644 index 0000000..8c66cbc --- /dev/null +++ b/Project1/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Project1/.idea/project-template.xml b/Project1/.idea/project-template.xml new file mode 100644 index 0000000..1f08b88 --- /dev/null +++ b/Project1/.idea/project-template.xml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/Project1/.idea/vcs.xml b/Project1/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Project1/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Project1/Project1.iml b/Project1/Project1.iml new file mode 100644 index 0000000..4df31a6 --- /dev/null +++ b/Project1/Project1.iml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Project1/out/production/Project1/com/s434666/Main.class b/Project1/out/production/Project1/com/s434666/Main.class new file mode 100644 index 0000000..d691362 Binary files /dev/null and b/Project1/out/production/Project1/com/s434666/Main.class differ diff --git a/Project1/out/production/Project1/com/s434666/PersonTest.class b/Project1/out/production/Project1/com/s434666/PersonTest.class new file mode 100644 index 0000000..060836e Binary files /dev/null and b/Project1/out/production/Project1/com/s434666/PersonTest.class differ diff --git a/Project1/out/production/Project1/com/s434666/PeselValidator.class b/Project1/out/production/Project1/com/s434666/PeselValidator.class new file mode 100644 index 0000000..f7dfc42 Binary files /dev/null and b/Project1/out/production/Project1/com/s434666/PeselValidator.class differ diff --git a/Project1/out/production/Project1/com/s434666/PeselValidatorTest.class b/Project1/out/production/Project1/com/s434666/PeselValidatorTest.class new file mode 100644 index 0000000..6e97b2d Binary files /dev/null and b/Project1/out/production/Project1/com/s434666/PeselValidatorTest.class differ diff --git a/Project1/out/production/Project1/com/s434666/models/Person.class b/Project1/out/production/Project1/com/s434666/models/Person.class new file mode 100644 index 0000000..d97ed50 Binary files /dev/null and b/Project1/out/production/Project1/com/s434666/models/Person.class differ diff --git a/Project1/src/com/s434666/Main.java b/Project1/src/com/s434666/Main.java new file mode 100644 index 0000000..5b2702c --- /dev/null +++ b/Project1/src/com/s434666/Main.java @@ -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 readPeople(){ + var people = new TreeMap(); + 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 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; + } +} diff --git a/Project1/src/com/s434666/PersonTest.java b/Project1/src/com/s434666/PersonTest.java new file mode 100644 index 0000000..23f5d1e --- /dev/null +++ b/Project1/src/com/s434666/PersonTest.java @@ -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); + } +} \ No newline at end of file diff --git a/Project1/src/com/s434666/PeselValidator.java b/Project1/src/com/s434666/PeselValidator.java new file mode 100644 index 0000000..eb95a2f --- /dev/null +++ b/Project1/src/com/s434666/PeselValidator.java @@ -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; + } + } +} diff --git a/Project1/src/com/s434666/PeselValidatorTest.java b/Project1/src/com/s434666/PeselValidatorTest.java new file mode 100644 index 0000000..661a8de --- /dev/null +++ b/Project1/src/com/s434666/PeselValidatorTest.java @@ -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")); + } +} \ No newline at end of file diff --git a/Project1/src/com/s434666/models/Person.java b/Project1/src/com/s434666/models/Person.java new file mode 100644 index 0000000..41a26bc --- /dev/null +++ b/Project1/src/com/s434666/models/Person.java @@ -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); + } +}