Hi team,
could you please help me with the error. i could not able to find any.
regards,
Manish
package com.codegym.task.task20.task2002;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
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 isUserEmpty= (users != null)? "yes":"no";
writer.println(isUserEmpty);
if(!users.isEmpty()){
for (User u:users
) {
writer.println(u.getFirstName());
writer.println(u.getLastName());
writer.println(u.getBirthDate());
writer.println(u.isMale());
writer.println(u.getCountry().name());
}
}
}
public void load(InputStream inputStream) throws Exception {
Scanner reader = new Scanner(inputStream);
String isUserPresent=reader.nextLine();
if(isUserPresent.equals("yes")){
users=new ArrayList<User>();
while(reader.hasNext()){
String firstName=reader.nextLine();
String lastName=reader.nextLine();
Date birthDate=new Date(reader.nextLine());
String isMaleString=reader.nextLine();
boolean isMale=Boolean.parseBoolean(isMaleString);
User.Country country= User.Country.valueOf(reader.nextLine());
User u = new User();
u.setFirstName(firstName);
u.setLastName(lastName);
u.setBirthDate(birthDate);
u.setMale(isMale);
u.setCountry(country);
users.add(u);
}
}
}
@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;
}
}
}