commit edf823884b58058df64c06a15be738b74c1ea91e Author: Piotr Baranowski Date: Sat Apr 25 09:33:28 2020 +0200 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b2c9882 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ + +target/ \ No newline at end of file diff --git a/ZJJXProjekt1.iml b/ZJJXProjekt1.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/ZJJXProjekt1.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b6d94ee --- /dev/null +++ b/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + ZJJX-Projekt1 + ZJJX-Projekt1 + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 13 + 13 + + + + + \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..994e237 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,20 @@ +import java.io.File; +import java.io.IOException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class Main { + public static void main(String[] args) { + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy HH-mm-ss"); + LocalDateTime now = LocalDateTime.now(); + String fileName = now.format(dtf) + " lista.txt"; + File file = new File(fileName); + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + + Read.read(file); + } +} diff --git a/src/main/java/Pesel.java b/src/main/java/Pesel.java new file mode 100644 index 0000000..f12924a --- /dev/null +++ b/src/main/java/Pesel.java @@ -0,0 +1,44 @@ +public class Pesel { + protected static String pesel; + + public static boolean checkPesel(String pesel) { + Pesel.pesel = pesel; + return (len() && checksum() && dayOfBirth()); + } + + public static boolean checksum() { + int[] multipliers = {1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1}; + int sum = 0; + + for(int i = 0; i < 11; i++) { + sum += multipliers[i] * Integer.valueOf(pesel.charAt(i)); + } + + if(sum % 10 == 0) { + return true; + } + else { + return false; + } + } + + public static boolean dayOfBirth() { + String dayOfBirthStr = pesel.substring(4,6); + int dayOfBirth = Integer.valueOf(dayOfBirthStr); + if(dayOfBirth > 0 && dayOfBirth < 32) { + return true; + } + else { + return false; + } + } + + public static boolean len() { + if(pesel.length() == 11) { + return true; + } + else { + return false; + } + } +} diff --git a/src/main/java/Read.java b/src/main/java/Read.java new file mode 100644 index 0000000..1e2fb83 --- /dev/null +++ b/src/main/java/Read.java @@ -0,0 +1,42 @@ +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Read { + public static void read(File file) { + String continuteInput; + Scanner read = new Scanner(System.in); + + while(true) { + List personalInfo = new ArrayList(); + System.out.print("Podaj miasto: "); + String city = read.nextLine(); + personalInfo.add(city); + + System.out.print("Podaj imię, nazwisko, PESEL (oddzielone spacjami): "); + String data = read.nextLine(); + for (String d : data.split(" ")) { + personalInfo.add(d); + } + + if(!Pesel.checkPesel(personalInfo.get(3))) { + System.out.println("Wprowadzony PESEL jest niepoprawny."); + } + else { + boolean saved = false; + // sprawdzenie czy już siedzi w pliku + if(!saved) { + Write.write(personalInfo, file); + System.out.println("Osoba dodana do listy."); + } + } + System.out.print("Chcesz kontynuować? [t/n] "); + continuteInput = read.nextLine(); + if(continuteInput.equals("n")) { + break; + } + } + read.close(); + } +} diff --git a/src/main/java/Write.java b/src/main/java/Write.java new file mode 100644 index 0000000..6f55f06 --- /dev/null +++ b/src/main/java/Write.java @@ -0,0 +1,35 @@ +import java.io.*; +import java.util.List; + +public class Write { + public static void checkIfPeselExists(File file, List data) { + if(file.exists()) { + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { + String line; + while((line = bufferedReader.readLine()) != null) { + + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + System.out.println("Problem z odczytaniem danych z pliku."); + } + } + + public static void write(List data, File file) { + if(file.exists()) { + try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file, true))){ + fileWriter.write(data.get(0) + " " + data.get(1) + " " + data.get(2) + " " + data.get(3) + "\n"); + } catch (IOException e) { + e.printStackTrace(); + return; + } + System.out.println("Zapis danych do pliku zakończył się powodzeniem."); + } else { + System.out.println("Problem z zapisem do pliku."); + } + } +}