Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. public class Person implements Comparable<Person>{
  2. public int id;
  3. public int person_id;
  4. public String pesel;
  5. public String city;
  6. public String name_surname;
  7.  
  8. public Person(int i, int pi, String pes, String c, String nam) {
  9. id=i;
  10. person_id=pi;
  11. pesel=pes;
  12. city=c;
  13. name_surname=nam;
  14. }
  15. public int compareTo(Person o) {
  16. return pesel.compareTo(o.pesel);
  17. }
  18. }
  19.  
  20.  
  21.  
  22. import java.sql.Connection;
  23. import java.sql.DriverManager;
  24. import java.sql.ResultSet;
  25. import java.sql.SQLException;
  26. import java.util.ArrayList;
  27. import java.util.Collections;
  28. import java.util.List;
  29. import java.util.StringTokenizer;
  30.  
  31. public class Main {
  32. public static void main(String[] args) {
  33. List<Person> lista = new ArrayList<Person>();
  34.  
  35. Connection connection = null;
  36. java.sql.Statement stmt = null;
  37.  
  38. String DB_URL = "jdbc:mysql://ricky.heliohost.org/cackoa_people";
  39. String USER = "cackoa_user";
  40. String PASS = "cackoa_password";
  41. String driverName = "com.mysql.jdbc.Driver";
  42. String miasto = null;
  43.  
  44. try {
  45. Class.forName(driverName);
  46.  
  47. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  48. stmt = connection.createStatement();
  49.  
  50. String sql;
  51. ResultSet rs;
  52. sql = "SELECT p.id, p.person_id, p.pesel, p.city, c.name_surname FROM parameters p JOIN person c ON c.id=p.person_id";
  53. rs = stmt.executeQuery(sql);
  54.  
  55. while (rs.next()) {
  56. // Retrieve by column name
  57. int id = rs.getInt("id");
  58. int person_id = rs.getInt("person_id");
  59. String pesel = rs.getString("pesel");
  60. String city = rs.getString("city");
  61. String name_surname = rs.getString("name_surname");
  62. // Wyświetlenie danych:
  63. // System.out.print(id + "\t" + person_id + "\t" + pesel + "\t"
  64. // + city + "\t\t\t" + name_surname + "\n");
  65. lista.add(new Person(id, person_id, pesel, city, name_surname));
  66. }
  67.  
  68. sql = "SELECT p.city, count(*) AS liczba FROM parameters p GROUP BY p.city ORDER BY liczba DESC LIMIT 1";
  69. rs = stmt.executeQuery(sql);
  70. rs.next();
  71. miasto = rs.getString("city");
  72. rs.close();
  73. stmt.close();
  74.  
  75. connection.close();
  76. } catch (ClassNotFoundException e) {
  77. System.out.println("brak sterownika\n" + e.getMessage());
  78. } catch (SQLException e) {
  79. System.out.println("Nie można się polaczyc\n" + e.getMessage());
  80. }
  81.  
  82.  
  83. Collections.sort(lista);
  84. int maxw=1;
  85. Person max_wiek = lista.get(0);
  86. while(max_wiek.pesel.charAt(max_wiek.pesel.length()-2)!='0' &&
  87. max_wiek.pesel.charAt(max_wiek.pesel.length()-2)!='2' &&
  88. max_wiek.pesel.charAt(max_wiek.pesel.length()-2)!='4' &&
  89. max_wiek.pesel.charAt(max_wiek.pesel.length()-2)!='6' &&
  90. max_wiek.pesel.charAt(max_wiek.pesel.length()-2)!='8')
  91. {
  92. max_wiek = lista.get(maxw);
  93. maxw++;
  94. }
  95. Person min_wiek = lista.get(lista.size() - 1);
  96. int minw = lista.size()-2;
  97. while(min_wiek.pesel.charAt(min_wiek.pesel.length()-2)!='0' &&
  98. min_wiek.pesel.charAt(min_wiek.pesel.length()-2)!='2' &&
  99. min_wiek.pesel.charAt(min_wiek.pesel.length()-2)!='4' &&
  100. min_wiek.pesel.charAt(min_wiek.pesel.length()-2)!='6' &&
  101. min_wiek.pesel.charAt(min_wiek.pesel.length()-2)!='8')
  102. {
  103. min_wiek=lista.get(minw);
  104. minw--;
  105. }
  106. int kobiaty = 0;
  107. int mezczyzni = 0;
  108. for (Person a : lista) {
  109. String pom = a.pesel;
  110. char pom2 = pom.charAt(pom.length() - 1);
  111. if (pom2 == '0' || pom2 == '2' || pom2 == '4' || pom2 == '6' || pom2 == '8') {
  112. kobiaty++;
  113. } else {
  114. mezczyzni++;
  115. }
  116. // System.out.print(a.id + "\t" + a.person_id + "\t" + a.pesel +
  117. // "\t" + a.city + "\t\t\t" + a.name_surname + "\n");
  118. }
  119. //////////////////////////////////////////////////////////////////////////////////
  120. Person z = lista.get(0);
  121. int pom = 1;
  122. while(z.pesel.charAt(z.pesel.length()-2)!='1' &&
  123. z.pesel.charAt(z.pesel.length()-2)!='3' &&
  124. z.pesel.charAt(z.pesel.length()-2)!='5' &&
  125. z.pesel.charAt(z.pesel.length()-2)!='7' &&
  126. z.pesel.charAt(z.pesel.length()-2)!='9' )
  127. {
  128. z=lista.get(pom);
  129. pom++;
  130. }
  131. StringTokenizer stringTokenizer = new StringTokenizer(z.name_surname);
  132. String pimie=stringTokenizer.nextToken();
  133. String pnazwisko=stringTokenizer.nextToken();
  134. int maxnaz=pnazwisko.length();
  135. for(Person a : lista)
  136. {
  137. if(a.pesel.charAt(a.pesel.length()-2)=='1' ||
  138. a.pesel.charAt(a.pesel.length()-2)=='3' ||
  139. a.pesel.charAt(a.pesel.length()-2)=='5' ||
  140. a.pesel.charAt(a.pesel.length()-2)=='7' ||
  141. a.pesel.charAt(a.pesel.length()-2)=='9' )
  142. {
  143. StringTokenizer Tokenizer = new StringTokenizer(a.name_surname);
  144. String pim=Tokenizer.nextToken();
  145. String pnaz=Tokenizer.nextToken();
  146. if(pnaz.length()>maxnaz)
  147. {
  148. //System.out.println(pnaz);
  149. z=a;
  150. maxnaz=pnaz.length();
  151. }
  152. }
  153. }
  154. /////////////////////////////////////////////////////////////////////////
  155. System.out.println("Najmłodsza kobieta:\t" + min_wiek.name_surname + "\t" + min_wiek.pesel);
  156. System.out.println("Najstarsza kobieta:\t" + max_wiek.name_surname + "\t" + max_wiek.pesel);
  157. System.out.println("Mężczyzna z najdłuższym nazwiskiem:\t"+z.name_surname+" [ " + maxnaz +" ]");
  158. System.out.println("Liczba mezczyzn:\t" + mezczyzni);
  159. System.out.println("Liczba kobiet:\t" + kobiaty);
  160. int mediana;
  161. String z1 = lista.get((lista.size() / 2) - 1).pesel;
  162. String z2 = lista.get(lista.size() / 2).pesel;
  163. z1 = z1.substring(0, 2);
  164. z2 = z2.substring(0, 2);
  165. int x1 = (int) Double.parseDouble(z1);
  166. int x2 = (int) Double.parseDouble(z2);
  167. x1 = 117 - x1;
  168. x2 = 117 - x2;
  169. mediana = (x1 + x2) / 2;
  170. System.out.println("Mediana wieku: " + mediana);
  171. System.out.println("Najwięcej osób pochodzi z miasta: " + miasto);
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement