Hey Guys, I passed but rewrote my code ...
Why my first solution couldn't pass the verification? Any ideas?
public static class CodeGym {
public List<User> users = new ArrayList<>();
public void save(OutputStream outputStream) throws Exception {
// Implement this method
try(PrintWriter writer = new PrintWriter(outputStream)){
while (!(users.isEmpty())) {
for (User user : users) {
writer.print(user.getFirstName());
writer.print(user.getLastName());
String birthDate = user.getBirthDate().toString();
writer.print(birthDate);
String sex = user.isMale() ? "true" : "false";
writer.print(sex);
String country = user.getCountry().toString();
writer.print(country);
writer.flush();
}
}
}
}
public void load(InputStream inputStream) throws Exception {
// Implement this method
try(BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))){
while (!(users.isEmpty())){
while (reader.ready()){
User user = new User();
user.setFirstName(reader.readLine());
user.setLastName(reader.readLine());
//String birthDate = reader.readLine();
user.setBirthDate(new Date(reader.readLine()));
String sex = reader.readLine();
if (sex.equalsIgnoreCase("true")){
user.setMale(true);
} else {
user.setMale(false);
}
String country = reader.readLine();
if (country.equals("United States")){
user.setCountry(User.Country.UNITED_STATES);
}
else if (country.equals("United Kingdom")){
user.setCountry(User.Country.UNITED_KINGDOM);
}
else {
user.setCountry(User.Country.OTHER);
}
users.add(user);
}
}
}
}
I passed but rewrote my code. Where is the issue in my first code?
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
syan
14 August 2020, 18:04
public void save(OutputStream outputStream) throws Exception {
PrintWriter pw = new PrintWriter(outputStream);
for (User user : users) {
pw.write(user.getFirstName() + "," + user.getLastName() + "," + user.getBirthDate()+ "," + user.isMale() + "," + user.getCountry().getDisplayName() + "\n");
}
pw.flush();
}
public void load(InputStream inputStream) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String str;
String[] temp;
while ((str = br.readLine()) != null) {
temp = str.split(",");
User user = new User();
user.setFirstName(temp[0]);
user.setLastName(temp[1]);
user.setBirthDate(new Date(temp[2]));
switch (temp[3]) {
case "true":
user.setMale(true);
break;
case "false":
user.setMale(false);
break;
}
switch (temp[4]) {
case "United States":
user.setCountry(User.Country.UNITED_STATES);
break;
case "United Kingdom":
user.setCountry(User.Country.UNITED_KINGDOM);
break;
case "Other":
user.setCountry(User.Country.OTHER);
break;
}
users.add(user);
}
}
0
Nouser
4 May 2020, 12:46
I like the solution, guess I rewrite mine so it is working like yours. I saved the list size as first line into the file.
Problems I've seen in your solution that may stop it from working
1. infinite loop in the save method - while (!(users.isEmpty())){
2. it's far easier to save the birth date as long
3. in the load method... there are no users in the list, so it'll never load while (!(users.isEmpty())){
Just a hint, if you want to get rid of all the comparisons..
0
Nouser
4 May 2020, 12:58
OK, have changed my methods, too, thanks ;)
0