Why do I fail the test?
1) I also do not understand why you can create another CodeGym. You would assume that it should be a singleton?
2) I do not understand why you use codeGym.save(outputStream) what is this suppose to do?
I would think that you would save new users to the list that is in the class Codegym But that is not what we're doing? This I find very confusing.
3) Why did we have a lesson on the isPresent boolean to check if a field is blank or not. When are we suppose to use this?
4) What do they mean on line 23 initialize users field for the codeGym object. The only field is the arrayList why would we initialize it here as it is initialized in its own class. So what is the meaning of this?
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;
/*
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("task2002", ".txt", new File("/Users/lilianetop/Desktop"));
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
System.out.println(codeGym.equals(loadedObject));
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 output = new PrintWriter(outputStream);
if (!users.isEmpty()) {
for (User user : users) {
StringBuilder storage = new StringBuilder();
storage.append(user.getFirstName() + "\n");
storage.append(user.getLastName() + "\n");
storage.append(user.getBirthDate() + "\n");
storage.append(user.isMale() + "\n");
storage.append(user.getCountry() + "\n");
output.print(storage.toString());
}
}
output.close();
}
public void load(InputStream inputStream) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while (reader.ready()) {
line = reader.readLine();
String[] fields = line.split("\n");
if (fields.length < 5) {
break;
}
User user = new User();
user.setFirstName(fields[0]);
user.setLastName(fields[1]);
Date birthDate = new SimpleDateFormat("MM dd yyyy").parse(fields[2]);
user.setBirthDate(birthDate);
user.setMale(Boolean.parseBoolean(fields[3]));
switch (fields[4]) {
case "UNITED_STATES":
user.setCountry(User.Country.UNITED_STATES);
break;
case "UNITED_KINGDOM":
user.setCountry(User.Country.UNITED_KINGDOM);
break;
default:
user.setCountry(User.Country.OTHER);
break;
}
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;
}
}
}