The validation says I need to check the exceptions in the save/load methods, but I don't see any? Can anyone help me figure out where the exceptions are coming from?
package com.codegym.task.task20.task2002;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
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();
// 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 isUsersPresent = users.size() > 0 ? "Yes" : "No";
writer.println(isUsersPresent);
writer.flush();
if (isUsersPresent.equals("Yes")) {
writer.println(users.size());
writer.flush();
for(User user : users){
boolean hasFirstName = user.getFirstName() != null;
writer.println(String.valueOf(hasFirstName));
writer.flush();
if(hasFirstName){
writer.println(user.getFirstName());
writer.flush();
}
boolean hasLastName = user.getLastName() != null;
writer.println(hasLastName);
writer.flush();
if(hasLastName){
writer.println(user.getLastName());
writer.flush();
}
boolean hasBirthDate = user.getBirthDate() != null;
writer.println(hasBirthDate);
writer.flush();
if(hasBirthDate){
writer.println(user.getBirthDate().getTime());
writer.flush();
}
//default for boolean is false: this will always have a value.
writer.println(user.isMale());
writer.flush();
boolean hasCountry = user.getCountry() != null;
writer.println(hasCountry);
writer.flush();
if(hasCountry){
writer.println(user.getCountry().getDisplayName());
writer.flush();
}
}
writer.println();
}
writer.close();
}
public void load(InputStream inputStream) throws Exception {
// Implement this method
Scanner reader = new Scanner(inputStream);
if(users.isEmpty()){
String isUsersPresent = reader.nextLine();
if(isUsersPresent.equals("Yes")){
for(int i = 0; i < Integer.parseInt(reader.nextLine()); i++){
User newUser = new User();
//check if firstName exists or not
if(reader.nextLine().equals("true")){
//set First Name
newUser.setFirstName(reader.nextLine());
}
//check if lastName exists or not
if(reader.nextLine().equals("true")){
//set Last Name
newUser.setLastName(reader.nextLine());
}
//check if BirthDate exists or not
if(reader.nextLine().equals("true")){
//set Birth Date
newUser.setBirthDate(new Date(Long.parseLong(reader.nextLine())));
}
//set the IsMale variable
String isMale = reader.nextLine();
if(isMale.equals("true")){
newUser.setMale(true);
}
else{
newUser.setMale(false);
}
//check if Country exists or not
if(reader.nextLine().equals("true")){
//set Country
newUser.setCountry(User.Country.valueOf(reader.nextLine()));
}
users.add(newUser);
}
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;
}
}
}