Console output:
true
[First name: Mike
Last name: Smith
Birth date: 1989 01 27
Sex: male
Country: United States, First name: Liliane
Last name: Top
Birth date: 1991 03 31
Sex: female
Country: United Kingdom]
package com.codegym.task.task20.task2002;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
/*
Reading and writing to a file: CodeGym
C:\Users\Администратор\CodeGymTasks\2.JavaCore\src\com\codegym\task\task20\task2002\file2002.tmp
*/
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
String fileName = "C:\\Users\\Администратор\\CodeGymTasks\\2.JavaCore\\src\\com\\codegym\\task\\task20\\task2002\\file2002.tmp";
try {
File yourFile = File.createTempFile(fileName, null);
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("yyyy MM dd", Locale.ENGLISH);
codeGym.users.add(new User("Mike", "Smith", dateFormat.parse("1989 01 27"), true, User.Country.UNITED_STATES));
codeGym.users.add(new User("Liliane", "Top", dateFormat.parse("1991 03 31"), false, User.Country.UNITED_KINGDOM));
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));
System.out.println(loadedObject.users);
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
if (users.isEmpty()) return;
try(PrintWriter writer = new PrintWriter(outputStream)) {
for (User user : users) {
writer.println(user.getFirstName());
writer.println(user.getLastName());
writer.println(user.getBirthDate().getTime());
writer.println(user.isMale());
writer.println(user.getCountry());
}
}
}
public void load(InputStream inputStream) throws Exception {
// Implement this method
try(BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
while (reader.ready()) {
String firstName = reader.readLine();
String lastName = reader.readLine();
Date birthDate = new Date(Long.parseLong(reader.readLine()));
boolean isMale = Boolean.parseBoolean(reader.readLine());
User.Country country;
switch (reader.readLine()) {
case "UNITED_STATES": country = User.Country.UNITED_STATES;
break;
case "UNITED_KINGDOM": country = User.Country.UNITED_KINGDOM;
break;
default: country = User.Country.OTHER;
}
users.add(new User(firstName, lastName, birthDate, isMale, country));
}
}
}
@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;
}
}
}
/* Console output:
true
[First name: Mike
Last name: Smith
Birth date: 1989 01 27
Sex: male
Country: United States, First name: Liliane
Last name: Top
Birth date: 1991 03 31
Sex: female
Country: United Kingdom]
---------------------------------------------------------------------------------
Reading and writing to a file: CodeGym
Implement the logic for file IO for the CodeGym class.
The file your_file_name.tmp can contain several CodeGym objects.
The main method exists just for you and is not involved in testing the solution.
Requirements:
1. The read/write logic in the save/load methods must work correctly when the users list is empty.
2. The read/write logic in the save/load methods must work correctly when the users list is not empty.
3. The Solution.CodeGym class should not support the Serializable interface.
4. The Solution.CodeGym class must be public.
5. The Solution.CodeGym class should not support the Externalizable interface.
*/