When checking with my test file everything seems fine to me.
Still getting a "false" output, not to mention the validation.
I've been staring at this for too long. Maybe someone has a hint for me?
Thanks in advance!
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);
File yourFile = new File("/Users/test/Desktop/Folder/file1.txt"); //just for testing
OutputStream outputStream = new FileOutputStream(yourFile);
InputStream inputStream = new FileInputStream(yourFile);
CodeGym codeGym = new CodeGym();
// Initialize users field for the codeGym object here
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
codeGym.users.add(new User("Harry", "Potter", (dateFormat.parse("01-03-2017")), true, User.Country.UNITED_KINGDOM));
codeGym.users.add(new User("Ron", "Weasley", (dateFormat.parse("04-05-2015")), true, User.Country.UNITED_STATES));
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 {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
PrintStream writer = new PrintStream(outputStream);
writer.println(this.users.size());
for(User u : users) {
writer.println(String.format("%s %s %s %s %s", u.getFirstName(), u.getLastName(), dateFormat.format(u.getBirthDate()), u.isMale(), u.getCountry()));
}
writer.close();
}
public void load(InputStream inputStream) throws Exception {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
while(reader.ready()) {
int readSize = Integer.parseInt(reader.readLine());
CodeGym codeGymInput = new CodeGym();
for(int i = 0; i < readSize; i++){
String[] fullText = reader.readLine().split(" ");
String firstName = fullText[0];
String lastName = fullText[1];
Date birthDate = dateFormat.parse(fullText[2]);
boolean isMale = fullText[3].equals("true") ? true : false;
User.Country country = fullText[4].equals("UNITED_KINGDOM") ? User.Country.UNITED_KINGDOM : fullText[4].equals("UNITED_STATES") ? User.Country.UNITED_STATES : User.Country.OTHER;
codeGymInput.users.add(new User(firstName, lastName, birthDate, isMale, country));
}
}
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;
}
}
}
I added a constructor in the User class as well, for convenience :
public User(String firstName, String lastName, Date birthDate, boolean isMale, Country country) {
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.isMale = isMale;
this.country = country;
}