"Check why an exception occurs in the save/load methods"
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) throws Exception {
// 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 {
PrintWriter writer = new PrintWriter(outputStream);
//String isUserPresent = users != null ? "Yes" : "No";
//writer.println(isUserPresent);
if (!users.isEmpty()) {
for (User u : users) {
writer.write(u.getFirstName() + "\n");
writer.write(u.getLastName() + "\n");
writer.write(u.getBirthDate() + "\n");
writer.write(u.isMale() + "\n");
writer.write(u.getCountry() + "\n");
}
}
writer.flush();
writer.close();
}
public void load(InputStream inputStream) throws Exception {
Scanner scanner = new Scanner(inputStream);
// String isUserPresent = scanner.nextLine();
//if (isUserPresent.equals("Yes")) {
User.Country country = null;
String sCountry = null;
while (scanner.hasNext()) {
User user = new User();
user.setFirstName(scanner.nextLine());
user.setLastName(scanner.nextLine());
SimpleDateFormat formatter = new SimpleDateFormat("dd MM yyyy");
Date parsedDate = formatter.parse(scanner.nextLine());
user.setBirthDate(parsedDate);
user.setMale(Boolean.parseBoolean(scanner.nextLine()));
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;
}
}
}