Help please
package com.codegym.task.task20.task2002;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
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();
// 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 printWriter = new PrintWriter(outputStream);
String isUsersPresent = users != null ? "Yes" : "No";
printWriter.println(isUsersPresent);
if (users != null) {
for(User user : users) {
printWriter.write(user.getFirstName());
printWriter.write(user.getLastName());
printWriter.write(user.getBirthDate().toString());
printWriter.write(user.getCountry().toString());
}
}
printWriter.flush();
printWriter.close();
}
public void load(InputStream inputStream) throws Exception {
// Implement this method
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
String sDate = "";
String sCountry = "";
User.Country country = null;
Scanner scanner = new Scanner(inputStream);
String isUsersPresent = scanner.nextLine();
if (isUsersPresent.equals("Yes")) {
while (scanner.hasNext()) {
User user = new User();
user.setFirstName(scanner.nextLine());
user.setLastName(scanner.nextLine());
sDate = scanner.nextLine();
user.setBirthDate(sdf.parse(sDate));
sCountry = scanner.nextLine();
if(sCountry.equals(User.Country.UNITED_STATES)) {
country = User.Country.UNITED_STATES;
} else if(sCountry.equals(User.Country.UNITED_KINGDOM)) {
country = User.Country.UNITED_KINGDOM;
} else {
country = User.Country.OTHER;
}
user.setCountry(country);
users.add(user);
}
}
scanner.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;
}
}
}