Убедись, что дни рождения пользователей корректно записываются и читаются, с точностью до миллисекунд.
Помогите спасите помираю.
package com.javarush.task.task20.task2002;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Date;
/*
Читаем и пишем в файл: JavaRush
*/
public class Solution {
public static void main(String[] args) {
//you can find your_file_name.tmp in your TMP directory or adjust outputStream/inputStream according to your file's actual location
//вы можете найти your_file_name.tmp в папке TMP или исправьте outputStream/inputStream в соответствии с путем к вашему реальному файлу
try {
File yourFile = File.createTempFile("your_file_name", null);
OutputStream outputStream = new FileOutputStream(yourFile);
InputStream inputStream = new FileInputStream(yourFile);
JavaRush javaRush = new JavaRush();
//initialize users field for the javaRush object here - инициализируйте поле users для объекта javaRush тут
javaRush.save(outputStream);
outputStream.flush();
JavaRush loadedObject = new JavaRush();
loadedObject.load(inputStream);
//here check that the javaRush object is equal to the loadedObject object - проверьте тут, что javaRush и loadedObject равны
outputStream.close();
inputStream.close();
} catch (IOException e) {
//e.printStackTrace();
System.out.println("Oops, something is wrong with my file");
} catch (Exception e) {
//e.printStackTrace();
System.out.println("Oops, something is wrong with the save/load method");
}
}
public static class JavaRush {
public List<User> users = new ArrayList<>();
public void save(OutputStream outputStream) throws Exception {
PrintWriter write = new PrintWriter ( outputStream);
String isRush = users != null ? "yes" : "no";
write.println(isRush);
if (users!= null){
for(User x : users){
write.println(x.getCountry());
write.println(x.getFirstName());
write.println(x.getLastName());
write.println(x.getBirthDate().getDate());
write.println(x.isMale());
}
}
write.close();
}
public void load(InputStream inputStream) throws Exception {
BufferedReader reader =new BufferedReader (new InputStreamReader(inputStream));
///SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
String isRush = reader.readLine();
if (isRush.equals("yes")) {
while (reader.ready()) {
isRush = reader.readLine();
User user = new User();
switch (isRush){
case "UKRAINE" :
user.setCountry(User.Country.UKRAINE);
break;
case "RUSSIA" :
user.setCountry(User.Country.RUSSIA);
break;
case "OTHER" :
user.setCountry(User.Country.OTHER);
break;
}
user.setFirstName(reader.readLine());
user.setLastName(reader.readLine());
Date date = new Date(Long.parseLong(reader.readLine()));
user.setBirthDate(date);
user.setMale(Boolean.parseBoolean(reader.readLine()));
users.add(user);
}
}
reader.close();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JavaRush javaRush = (JavaRush) o;
return users != null ? users.equals(javaRush.users) : javaRush.users == null;
}
@Override
public int hashCode() {
return users != null ? users.hashCode() : 0;
}
}
}