Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. package com.javarush.task.task20.task2002;
  2.  
  3. import java.io.*;
  4. import java.sql.Date;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8.  
  9. /*
  10. Читаем и пишем в файл: JavaRush
  11. */
  12. public class Solution {
  13. public static void main(String[] args) {
  14. //you can find your_file_name.tmp in your TMP directory or adjust outputStream/inputStream according to your file's actual location
  15. //вы можете найти your_file_name.tmp в папке TMP или исправьте outputStream/inputStream в соответствии с путем к вашему реальному файлу
  16. try {
  17. File yourFile = File.createTempFile("your_file_name", null);
  18. OutputStream outputStream = new FileOutputStream(yourFile);
  19. InputStream inputStream = new FileInputStream(yourFile);
  20.  
  21. JavaRush javaRush = new JavaRush();
  22. //initialize users field for the javaRush object here - инициализируйте поле users для объекта javaRush тут
  23. User user = new User();
  24. User user1 = new User();
  25.  
  26. user.setCountry(User.Country.RUSSIA);
  27. user.setFirstName("Kolya");
  28. user.setLastName("Kolya2");
  29. user.setMale(true);
  30. user.setBirthDate(new Date(1508944516163L));
  31. javaRush.users.add(user );
  32.  
  33. user1.setCountry(User.Country.RUSSIA);
  34. user1.setFirstName("I");
  35. user1.setLastName("I2");
  36. user1.setMale(false);
  37. user1.setBirthDate(new Date(1508944516163L));
  38. javaRush.users.add(user1 );
  39.  
  40.  
  41. javaRush.save(outputStream);
  42. outputStream.flush();
  43.  
  44. JavaRush loadedObject = new JavaRush();
  45. loadedObject.load(inputStream);
  46. //here check that the codeGym object is equal to the loadedObject object - проверьте тут, что javaRush и loadedObject равны
  47. if (javaRush.equals(loadedObject) )
  48. System.out.println("Ура");
  49. else
  50. System.out.println("нет");
  51.  
  52. outputStream.close();
  53. inputStream.close();
  54.  
  55. } catch (IOException e) {
  56. //e.printStackTrace();
  57. System.out.println("Oops, something is wrong with my file");
  58. } catch (Exception e) {
  59. //e.printStackTrace();
  60. System.out.println("Oops, something is wrong with the save/load method");
  61. }
  62. }
  63.  
  64. public static class JavaRush {
  65. public List<User> users = new ArrayList<>();
  66.  
  67. public void save(OutputStream outputStream) throws Exception {
  68.  
  69. //implement this method - реализуйте этот метод
  70. PrintWriter writer = new PrintWriter(outputStream);
  71. if(users.size() == 0){ //если нет user
  72.  
  73. } else
  74. {
  75.  
  76. for(int i = 0; i < users.size(); i++)
  77. {
  78. StringBuilder sb = new StringBuilder();
  79. User us= users.get(i);
  80. if ( us.getCountry()!=null)
  81. sb.append(us.getFirstName() + ";" + us.getLastName() + ";"+ us.getBirthDate().getTime() +";" + us.getCountry().getDisplayName()+";" + us.isMale() );
  82. else
  83. sb.append(us.getFirstName() + ";" + us.getLastName() + ";"+ us.getBirthDate().getTime() +";" + "null"+";" + us.isMale() );
  84.  
  85. writer.println(sb.toString());
  86. writer.flush();
  87. }
  88.  
  89. }
  90.  
  91. }
  92.  
  93. public void load(InputStream inputStream) throws Exception {
  94. //implement this method - реализуйте этот метод
  95. BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
  96. while (br.ready())
  97. {
  98. String[] as = br.readLine().split("\\;");
  99. User us = new User();
  100.  
  101. //String[] as = asset.split(" ");
  102. if (!as[0].equals("null") )
  103. us.setFirstName(as[0]);
  104.  
  105. if (!as[1].equals("null") )
  106. us.setLastName(as[1]);
  107.  
  108. Date date = new Date(Long.parseLong(as[2]));
  109.  
  110. //if (!as[2].equals("null") )
  111. us.setBirthDate( date );
  112.  
  113. if (as[3].equals("null"))
  114. {;}
  115. else if (as[3].equals("Russia"))
  116. us.setCountry( User.Country.RUSSIA);
  117. else if (as[3].equals("Ukraine"))
  118. us.setCountry( User.Country.UKRAINE);
  119. else if (as[3].equals("Other"))
  120. us.setCountry( User.Country.OTHER);
  121.  
  122. if (!as[4].equals("null") )
  123. us.setMale( Boolean.valueOf(as[4]) );
  124. this.users.add(us);
  125.  
  126. }
  127.  
  128. }
  129.  
  130. @Override
  131. public boolean equals(Object o) {
  132. if (this == o) return true;
  133. if (o == null || getClass() != o.getClass()) return false;
  134.  
  135. JavaRush javaRush = (JavaRush) o;
  136.  
  137. return users != null ? users.equals(javaRush.users) : javaRush.users == null;
  138.  
  139. }
  140.  
  141. @Override
  142. public int hashCode() {
  143. return users != null ? users.hashCode() : 0;
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement