Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader fin = new BufferedReader(new InputStreamReader(
- new FileInputStream("pesel.txt")
- ));
- String[] dane = new String[150];
- for(int i = 0; i < dane.length;i++) {
- dane[i] = fin.readLine();
- }
- // Podpunkt a)
- int count = 0;
- for(String x : dane) {
- if(x.substring(2, 4).equals("12"))
- count++;
- }
- System.out.println(count);
- System.out.println();
- // Podpunkt b)
- int womanCount = 0;
- for(String x : dane) {
- if(isEven(Integer.parseInt(x.charAt(9) + "")))
- womanCount++;
- }
- System.out.println(womanCount);
- System.out.println();
- // Podpunkt c)
- HashMap<String, Integer> hm = new HashMap<>(); // rok-liczbaOsob
- String rok;
- int rok89 = 0;
- int rok90 = 0;
- for(String x : dane) {
- rok = x.substring(0, 2);
- if(hm.getOrDefault(rok, -1) == -1) {
- hm.put(rok, 1);
- } else {
- hm.put(rok, hm.get(rok) + 1);
- }
- }
- int max = hm.values().iterator().next();
- for(Map.Entry<String, Integer> me : hm.entrySet()) {
- if(me.getValue() > max)
- max = me.getValue();
- }
- int finalMax = max;
- hm.entrySet().forEach(me -> {
- if(me.getValue() == finalMax)
- System.out.println(me.getKey());
- });
- System.out.println();
- ArrayList<String> wrongPesels = new ArrayList<>();
- for(String x : dane) {
- if(Integer.parseInt(x.substring(10, 11)) != calculateChecksum(x)) {
- wrongPesels.add(x);
- }
- }
- // sortowanie rosnaco wrongPesels
- wrongPesels.sort(String::compareTo);
- wrongPesels.forEach(pesel -> System.out.println(pesel));
- // Przygotowujemy dane do podpunktu e
- HashMap<String, Integer> map = new HashMap<>(); // rok-liczba
- String rocznik;
- for(String x : dane) {
- rocznik = x.substring(0,1) + "0"; // pelny rocznik
- if(map.getOrDefault(rocznik, -1) == -1) {
- map.put(rocznik, 1);
- } else {
- map.put(rocznik, map.get(rocznik) + 1);
- }
- }
- try(FileWriter fout = new FileWriter("daneEksport.txt")) {
- fout.write("Rocznik;Ilosc osob" + "\n");
- for (Map.Entry<String, Integer> me : map.entrySet()) {
- fout.write(me.getKey() + ";" + me.getValue() + "\n");
- }
- }
- }
- public static boolean isEven(int x) {
- return (x%2 == 0);
- }
- public static int calculateChecksum(String pesel) {
- int tablicaWag[] = {
- 1, 3, 7, 9, 1, 3, 7, 9, 1, 3
- };
- int daneZPeselu[] = new int[10];
- for(int i = 0; i < pesel.length()-1/*bez cyfry kontrolnej*/;i++) {
- daneZPeselu[i] = Integer.parseInt(pesel.charAt(i) + "");
- }
- int sumaWynikow = 0;
- for(int i = 0; i < 10;i++) {
- sumaWynikow += (tablicaWag[i] * daneZPeselu[i]);
- }
- if((sumaWynikow % 10) == 0) {
- return 0;
- } else {
- return (10-(sumaWynikow%10));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement