From a1047da9f3f354cbe77ad43edf876f6087c25d26 Mon Sep 17 00:00:00 2001 From: Pawel Janecki Date: Sun, 19 Apr 2020 17:09:32 +0200 Subject: [PATCH] complete project --- .idea/.gitignore | 2 + .idea/compiler.xml | 13 +++ .idea/misc.xml | 14 +++ .idea/vcs.xml | 6 ++ README.md | 1 + peopleData.txt | 1 + pom.xml | 36 +++++++ src/main/java/company/DataSaver.java | 96 ++++++++++++++++++ src/main/java/company/Main.java | 56 ++++++++++ src/main/java/company/PeselValidator.java | 31 ++++++ src/test/java/company/DataSaverTests.java | 73 +++++++++++++ .../java/company/PeselValidatorTests.java | 48 +++++++++ target/classes/company/DataSaver.class | Bin 0 -> 2990 bytes target/classes/company/Main.class | Bin 0 -> 2052 bytes target/classes/company/PeselValidator.class | Bin 0 -> 980 bytes .../compile/default-compile/createdFiles.lst | 3 + .../compile/default-compile/inputFiles.lst | 3 + .../default-testCompile/createdFiles.lst | 2 + .../default-testCompile/inputFiles.lst | 2 + .../test-classes/company/DataSaverTests.class | Bin 0 -> 3403 bytes .../company/PeselValidatorTests.class | Bin 0 -> 1887 bytes test.txt | 3 + 22 files changed, 390 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 README.md create mode 100644 peopleData.txt create mode 100644 pom.xml create mode 100644 src/main/java/company/DataSaver.java create mode 100644 src/main/java/company/Main.java create mode 100644 src/main/java/company/PeselValidator.java create mode 100644 src/test/java/company/DataSaverTests.java create mode 100644 src/test/java/company/PeselValidatorTests.java create mode 100644 target/classes/company/DataSaver.class create mode 100644 target/classes/company/Main.class create mode 100644 target/classes/company/PeselValidator.class create mode 100644 target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst create mode 100644 target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst create mode 100644 target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst create mode 100644 target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst create mode 100644 target/test-classes/company/DataSaverTests.class create mode 100644 target/test-classes/company/PeselValidatorTests.class create mode 100644 test.txt diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..81d39d3 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..5b68b3b --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..2c4e090 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..9661ac7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..be0cbf3 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +projekt nr 1 \ No newline at end of file diff --git a/peopleData.txt b/peopleData.txt new file mode 100644 index 0000000..edf0427 --- /dev/null +++ b/peopleData.txt @@ -0,0 +1 @@ +asdd, a s 90081900834 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..784753a --- /dev/null +++ b/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + projekt1 + projekt1 + 1.0-SNAPSHOT + + + 11 + 11 + + + + + com.google.code.gson + gson + 2.8.5 + + + junit + junit + 4.12 + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + + \ No newline at end of file diff --git a/src/main/java/company/DataSaver.java b/src/main/java/company/DataSaver.java new file mode 100644 index 0000000..a92a22a --- /dev/null +++ b/src/main/java/company/DataSaver.java @@ -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; +} diff --git a/src/main/java/company/Main.java b/src/main/java/company/Main.java new file mode 100644 index 0000000..449b7d2 --- /dev/null +++ b/src/main/java/company/Main.java @@ -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(); + } +} diff --git a/src/main/java/company/PeselValidator.java b/src/main/java/company/PeselValidator.java new file mode 100644 index 0000000..4306f1b --- /dev/null +++ b/src/main/java/company/PeselValidator.java @@ -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; + } +} diff --git a/src/test/java/company/DataSaverTests.java b/src/test/java/company/DataSaverTests.java new file mode 100644 index 0000000..444fbd5 --- /dev/null +++ b/src/test/java/company/DataSaverTests.java @@ -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; + } + } +} diff --git a/src/test/java/company/PeselValidatorTests.java b/src/test/java/company/PeselValidatorTests.java new file mode 100644 index 0000000..6a7db18 --- /dev/null +++ b/src/test/java/company/PeselValidatorTests.java @@ -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); + } + } +} diff --git a/target/classes/company/DataSaver.class b/target/classes/company/DataSaver.class new file mode 100644 index 0000000000000000000000000000000000000000..7341603d0a0ac249d46dbe34b721372b76083576 GIT binary patch literal 2990 zcmaJ@OH&kA7(I7hJ<~KGZ`ubMpS+1tQ3gU1L3EIp42pv$rWqO-8M?=r2En{sxO0_k z5>s)_CRtdOK$TQwo2o2R`2|_!M_3`}b`QhgkSw^j@4eqW-{YL`4!{5N`d0v#@l?fO z+=xI&Ou?`UKW@rlL=K}0##AU6S1_R>f?Hus$~ms04^!2=e^(wSg^J{Vs>SI-prbF zyLOEQyQ}+7Xsf0R6%i0I1}-=D0uRWgKu}Iht~_a5j*+r*Zj8J!lMiV+n{rA+@^*_u z+O;bUDQ~M_mXO!hl4iz9*;YnL#U0KWf20m+oJ&h#=x zDAdb>>5LRAFr9K1r_41+;8Y%Wm#_mcS*wbiE5Io`ZW`I-BIAzRE7_zg2%Z*H(9b`=`7lFxylfh!>}~i$Y^+kvu`=_>MxAjk#tW|r-%)g9Y z$U{cbv9s$6zGO*9Yja*Ll^>@X2s^Oi6wR#U{}7|8{tXKU;_SGbG@XeaNpM zzuA2pk8?G|eIIAFbqk`k?HBl7apl7ajurfY1An~&2=nJFLM))(v&KoB;)?fhny7pg zaRz7emUp&~bW-i?NG zoI^9=TWB4Ic!NfT2m-B4#3vIGe-KII>B}OgqwRG1Jp!+`p(NV35FXn`B+;=2H5%}D z2YuZk5mlM%OU*1NR1$lc2H zb~@47sU#5?v)W=1>!I$_IrmuN)bBqw<7`^sTAJ0+E=zCG-lGldN8`P4#9frVu z))n+|r9e>-5yD;d381Q?Jc#$nXMimC^X~(E=rN6-xEJC{PZ853wkpvPkOTx=0{B@Q z4eG(R_SdW`Bnv^w0?Be&@D$#}=)wQgG`eb_o0D|XiL1q?>hsbAy|8aS}eF4(8?BKLz0yW2=l)ITK|4 z7Bh{Lt0{7Go4F^L%?xkdce9@KqAO%Q>}Dvl9>z5{>j>-M>7<4zo=)ni1*wT3{hfCe z6XL4VRgB{De_S$8skqFOc9Jid=ZBE2R~B*Zr`Y9FbRC~@R$20Oe1Ru~zRh(7PwsGU I+8rPK3#jUgyZ`_I literal 0 HcmV?d00001 diff --git a/target/classes/company/Main.class b/target/classes/company/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..6f926b65d28d184a647c3328eef2acf347e5c020 GIT binary patch literal 2052 zcmZuxNmCm~6#g15BgBBQSd1K!jFUJlW=V)K33v&M90UR>2t&XjNh38dAkBPUau*d~9Roy-Bef^g2z1RQ#_s?qp18|~f#T^BA z72K=F_ZW^MfRQMwFsfiIfLr3!WD?LyHg6&EApH%8IZ>e#X;K zEY*2(lI5Ec)^HlA^y~>`v^m|~VNfk$JEp-4epIztm*+O7xaH-l;AaJG4AI1PPFSvP z8jgY``p>P2+{OsSvBA*Pem>TYMRKrZ+IfbEjMg{ab&Nb8aL0KQk$PBVC9$nLF3n%an`4HyiyHsJsct?YG9#yZju-Si zcTHkAvgx0xvzmQZGv-!u#e5T@0U~=SW{gnQCX=k*DI`_f$=uicX*03k6rfY!e1MnF> zr@v}^f$M~#%WM%LdXs@?hp1XOL}1|+g8K;VBb+*fvJi{x)1bdXG*(lF+EqraJlE0r z#sMx(cORhsIf8pqc==d37>oyx#qnS)Rz|~MC{Ew&V7_lK91kC$@lVvr8%=cM#rwUk zc({z_uI_lSj4LnR|3%RRybLA~^J0&omJ*Lq)D39I6?}yj^dpWtL`Z@(lSIZWWwS)L zRw+l5vfLu{F52)r-FS*lkA-o{bcxPik~~C6jdUHv6J<}*Hriv@#tn24Tdg#K-RL2f z`tg7$?x2_^agTg=5dB{hRzvU|+7(nOP~M_ZLEl?6)A|nA6?{Vn+$5ANRX?Kgg`txy z*Erc^wAXgVTFQt&M=1C*@R9_6(si`UnpTsm8ZvT?{`~UQ;#+bK&v^i>hOE-#<=3&I b3Cg(oH=!z<;8ul|ke47Tf@2u+@VEa5h5q5` literal 0 HcmV?d00001 diff --git a/target/classes/company/PeselValidator.class b/target/classes/company/PeselValidator.class new file mode 100644 index 0000000000000000000000000000000000000000..91973836df3982600209502bd578c48460ce727f GIT binary patch literal 980 zcmZuwO-~b16g{sq(`kn(9f3k|K*i6t0#!qFLDgsz6Ouwnsw9;S!!T(F%Sl8@D23vhfLaZ=Jb`S@54@G$&ZZZ=bT&IgMrxt!bk>~6m**2u zjobdNK)&XNJ6$I@D6e|E9+Nu$mJ^53qCjeIaG*l2P}_F)oN~trTIEI@`9W*3_*y{g zN|L;<5aI225}U28oeC>;qzG%Fx+#XvG4hpioOY&Ih2h7rEB&?x#(3Hc{5tKvXN?&W3t5nR;K$iS7%`sfU2xFssR3tJ|mAxFO ziySeHkprb-8Q+YiqSl*gvfk7zz1~b(>3UNqk|JUyC1mLmvr+`Dbh5roL+Xn1P|c~9 z@d0^zE~hRWL7UB~^X-kwTUC5K{$*Q6m+JObrN6W1EJNnJho&B$E8#)kbcW>)^B>Bh zLgO!J`xV!9jAH{6@Q}wgE~5t<`*;p-^H(2I2)8!;rXDVMDnGI*rp zi#lG{(2_HKGi$CZPeWVAa@Pu`vz5BFCmhpY_^Rkn*?Bc9v-jysO)o58k*(;3%$ka}36 zHLnHRS6<32*r^G%?0Ra_v{ndyw#ex3D6i-`W?llHcO}~U*%h~x&q-=T$e=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