package com.codegym.task.task20.task2002;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.util.*;
import java.text.*;
import java.lang.IllegalArgumentException;
/*
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("C:\\Users\\Julian Orellana\\Desktop\\ejercicios_java\\ejercicio.txt", null);
OutputStream outputStream = new FileOutputStream(yourFile);
InputStream inputStream = new FileInputStream(yourFile);
SimpleDateFormat dateformat_f = new SimpleDateFormat("yyyy.MM.dd");
Date curDate = new Date();
String DateToStr = dateformat_f.format(curDate);
Date strToDate = dateformat_f.parse(DateToStr);
User ua = new User("Juan Jose", "Toala", strToDate, true, User.Country.UNITED_STATES);
User ub = new User("Pablo ", "Colimes", strToDate, true, User.Country.UNITED_KINGDOM);
User uc = new User("Daniela", "Gomez", strToDate, false, User.Country.OTHER);
CodeGym codeGym = new CodeGym();
User[] arreglo_users = new User[]{ua, ub, uc} ;
//int[] abc = new int[]{1,2,3};
// Initialize users field for the codeGym object here
codeGym.users.addAll( Arrays.asList(arreglo_users) );
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 CodeGym( User... users) {
if (users != null) {
this.users.addAll(Arrays.asList(users));
}
}
*/
public void save(OutputStream outputStream) throws IOException, Exception {
// Implement this method
PrintWriter printWriter = new PrintWriter(outputStream);
String firstName;
String lastName;
Date birthDate;
Boolean isMale;
User.Country country;
try{
if ( users.size() > 0 ) {
Iterator<User> usersIterator = users.iterator();
while(usersIterator.hasNext()) {
User u = usersIterator.next();
firstName = u.getFirstName();
lastName = u.getLastName();
birthDate = u.getBirthDate();
isMale = u.isMale();
country = u.getCountry();
// country = User.Country.OTHER;
// Boolean.toString(isMale)
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy.MM.dd");
if ( (firstName != null) && (lastName!=null)
&& (birthDate!=null)
//&& ( Boolean.toString(isMale) != null )
&& ( isMale != null)
&& (country.getDisplayName() != null)
)
{
String DateToStr = dateformat.format(birthDate);
printWriter.println(firstName);
printWriter.println(lastName);
printWriter.println(DateToStr);
printWriter.println( Boolean.toString(isMale) );
printWriter.println(country.getDisplayName());
//printWriter.println(country.toString() );
/*
printWriter.println("02-1-2019 10:20:20");
printWriter.println("true");
printWriter.println("USA");*/
}
}
//printWriter.flush();
}
printWriter.close();
}
catch(NullPointerException f){
f.printStackTrace();
}
}
public void load(InputStream inputStream) throws IOException, Exception {
// Implement this method
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String fname;
String lname;
String bdate;
String ismale;
String country;
//SimpleDateFormat dateformat= null;
while ( ( (fname = reader.readLine()) != null) &&
( (lname = reader.readLine()) != null) &&
( (bdate = reader.readLine()) != null) &&
( (ismale = reader.readLine()) != null) &&
( (country = reader.readLine()) != null)
)
{
//country = country.toUpperCase();
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy.MM.dd");
//bdate = "2019.03.03";
Date bdate_d = dateformat.parse(bdate);
//User.Country country_c = User.Country.UNITED_STATES;
User.Country country_c = User.Country.valueOf(country.toUpperCase() );
boolean ismale_bol = Boolean.parseBoolean(ismale);
//Boolean ismale_bol = true;
users.add( new User( fname, lname, bdate_d, ismale_bol, country_c ) );
}
}
catch(NullPointerException f){
f.printStackTrace();
}
catch(IllegalArgumentException f){
f.printStackTrace();
}
catch(ParseException e){
e.printStackTrace();
}
}
@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;
}
}
}
help me , i have this error : Check why an exception occurs in the save/load methods.
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Andrew
20 March 2019, 18:37
A few things:
1. The User constructor is default so accepts no parameters. Check how new Users are created and how to assign their variables. Currently the code won't compile and if it could, the created users will have all variables = null. Check user creation in the load method for this too.
2. The save/load methods need to be able to read/write content even if some of the User variables are null. In the code, save/load methods only read/write the content if none of the fields are null. (Hint: think of a place holder to use if a variable is null).
Some nice to haves:
1. In the save/load methods, if the method throws Exception, you don't need to include any other Exceptions since Exception covers them all.
2. Near the top, the Date/SimpleDateFormat stuff can be removed, it's not used (at least in the current iteration).
3. Once you get the above working in your code, you shouldn't need NullPointerException catch statements.
+1