I give up. I can not figure out what I am doing wrong. Any ideas? Thank you.
package com.codegym.task.task20.task2002;
import java.io.*;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
/*
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();
// 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);
//is anyone there?
String isUserPresent = users != null ? "Yes" : "No";
if(isUserPresent != null){
for(User user : users){
writer.println(user.getFirstName());
writer.println(user.getLastName());
writer.println(user.getBirthDate());
writer.println(user.isMale());
writer.println(user.getCountry());
writer.flush();
}
}
writer.close();
}
public void load(InputStream inputStream) throws Exception {
// Implement this method
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
while (reader.ready()) {
User user = new User();
user.setFirstName(reader.readLine());
user.setLastName(reader.readLine());
user.setBirthDate(Date.valueOf(reader.readLine()));
user.isMale();
String country = reader.readLine();
if(country.equals("United States")){
user.setCountry(User.Country.UNITED_STATES);
} else if(country.equals("United Kingdon")){
user.setCountry(User.Country.UNITED_KINGDOM);
} else {
user.setCountry(User.Country.OTHER);
}
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;
}
}
}