Help pls? Stuck......
package com.codegym.task.task20.task2002;
import java.io.*;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/*
Reading and writing to a file: CodeGym
*/
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
try {
File yourFile = File.createTempFile("your_file_name", null);
OutputStream outputStream = new FileOutputStream(yourFile);
InputStream inputStream = new FileInputStream(yourFile);
CodeGym codeGym = new CodeGym();
User user = new User();
user.setFirstName("Anton");
user.setLastName("Chekhov");
user.setCountry(User.Country.UNITED_STATES);
user.setBirthDate(new java.util.Date());
user.setMale(true);
codeGym.users.add(user);
// Initialize users field for the codeGym object here
codeGym.save(outputStream);
outputStream.flush();
CodeGym loadedObject = new CodeGym();
loadedObject.load(inputStream);
// Here check that the codeGym object is equal to the loadedObject object
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 CodeGym {
public List<User> users = new ArrayList<>();
public void save(OutputStream outputStream) throws Exception {
// Implement this method
PrintWriter writer = new PrintWriter(outputStream);
String isUserPresent = users != null ? "Yes" : "No";
writer.println(isUserPresent);
if (isUserPresent.equals("Yes")){
for (User user : users){
writer.println(user.getFirstName());
writer.println(user.getLastName());
writer.println(user.getBirthDate());
writer.println(user.isMale());
writer.println(user.getCountry().getDisplayName());
}
}
writer.flush();
writer.close();
}
public void load(InputStream inputStream) throws Exception {
// Implement this method
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String isUserPresent = reader.readLine();
if (isUserPresent.equals("Yes")){
while (reader.ready()){
User user = new User();
user.setFirstName(reader.readLine());
user.setLastName(reader.readLine());
user.setBirthDate(new Date(Long.parseLong(reader.readLine())));
user.setMale(Boolean.parseBoolean(reader.readLine()));
user.setCountry(User.Country.valueOf(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;
CodeGym codeGym = (CodeGym) o;
return users != null ? users.equals(codeGym.users) : codeGym.users == null;
}
@Override
public int hashCode() {
return users != null ? users.hashCode() : 0;
}
}
}