Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class Main {
  7. public static void main(String[] args) throws IOException {
  8. BufferedReader fin = new BufferedReader(new InputStreamReader(
  9. new FileInputStream("pesel.txt")
  10. ));
  11.  
  12. String[] dane = new String[150];
  13. for(int i = 0; i < dane.length;i++) {
  14. dane[i] = fin.readLine();
  15. }
  16.  
  17. // Podpunkt a)
  18. int count = 0;
  19. for(String x : dane) {
  20. if(x.substring(2, 4).equals("12"))
  21. count++;
  22. }
  23. System.out.println(count);
  24. System.out.println();
  25.  
  26. // Podpunkt b)
  27. int womanCount = 0;
  28. for(String x : dane) {
  29. if(isEven(Integer.parseInt(x.charAt(9) + "")))
  30. womanCount++;
  31. }
  32. System.out.println(womanCount);
  33. System.out.println();
  34.  
  35. // Podpunkt c)
  36. HashMap<String, Integer> hm = new HashMap<>(); // rok-liczbaOsob
  37. String rok;
  38. int rok89 = 0;
  39. int rok90 = 0;
  40. for(String x : dane) {
  41. rok = x.substring(0, 2);
  42.  
  43. if(hm.getOrDefault(rok, -1) == -1) {
  44. hm.put(rok, 1);
  45. } else {
  46. hm.put(rok, hm.get(rok) + 1);
  47. }
  48. }
  49.  
  50. int max = hm.values().iterator().next();
  51. for(Map.Entry<String, Integer> me : hm.entrySet()) {
  52. if(me.getValue() > max)
  53. max = me.getValue();
  54. }
  55.  
  56. int finalMax = max;
  57. hm.entrySet().forEach(me -> {
  58. if(me.getValue() == finalMax)
  59. System.out.println(me.getKey());
  60. });
  61.  
  62. System.out.println();
  63. ArrayList<String> wrongPesels = new ArrayList<>();
  64. for(String x : dane) {
  65. if(Integer.parseInt(x.substring(10, 11)) != calculateChecksum(x)) {
  66. wrongPesels.add(x);
  67. }
  68. }
  69.  
  70. // sortowanie rosnaco wrongPesels
  71. wrongPesels.sort(String::compareTo);
  72. wrongPesels.forEach(pesel -> System.out.println(pesel));
  73.  
  74. // Przygotowujemy dane do podpunktu e
  75. HashMap<String, Integer> map = new HashMap<>(); // rok-liczba
  76. String rocznik;
  77. for(String x : dane) {
  78. rocznik = x.substring(0,1) + "0"; // pelny rocznik
  79.  
  80. if(map.getOrDefault(rocznik, -1) == -1) {
  81. map.put(rocznik, 1);
  82. } else {
  83. map.put(rocznik, map.get(rocznik) + 1);
  84. }
  85. }
  86.  
  87. try(FileWriter fout = new FileWriter("daneEksport.txt")) {
  88. fout.write("Rocznik;Ilosc osob" + "\n");
  89. for (Map.Entry<String, Integer> me : map.entrySet()) {
  90. fout.write(me.getKey() + ";" + me.getValue() + "\n");
  91. }
  92. }
  93. }
  94.  
  95. public static boolean isEven(int x) {
  96. return (x%2 == 0);
  97. }
  98.  
  99. public static int calculateChecksum(String pesel) {
  100. int tablicaWag[] = {
  101. 1, 3, 7, 9, 1, 3, 7, 9, 1, 3
  102. };
  103. int daneZPeselu[] = new int[10];
  104.  
  105. for(int i = 0; i < pesel.length()-1/*bez cyfry kontrolnej*/;i++) {
  106. daneZPeselu[i] = Integer.parseInt(pesel.charAt(i) + "");
  107. }
  108.  
  109. int sumaWynikow = 0;
  110. for(int i = 0; i < 10;i++) {
  111. sumaWynikow += (tablicaWag[i] * daneZPeselu[i]);
  112. }
  113.  
  114. if((sumaWynikow % 10) == 0) {
  115. return 0;
  116. } else {
  117. return (10-(sumaWynikow%10));
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement