package com.codegym.task.task20.task2005;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
Stranger things
*/
public class Solution {
public static void main(String[] args) {
// Update the string passed to the createTempFile method based on the path to a file on your hard drive
try {
File yourFile = File.createTempFile("your_file_name", null);
OutputStream outputStream = new FileOutputStream(yourFile);
InputStream inputStream = new FileInputStream(yourFile);
Human smith = new Human ("Smith", new Asset ("home"), new Asset ("car"));
smith.save(outputStream);
outputStream.flush();
Human somePerson = new Human();
somePerson.load(inputStream);
// Check that smith is equal to somePerson
System.out.println(smith.equals(somePerson));
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 Human {
public String name;
public List<Asset> assets = new ArrayList<>();
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Human human = (Human) o;
if (name != null ? !name.equals(human.name) : human.name != null) return false;
return assets != null ? assets.equals(human.assets) : human.assets == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (assets != null ? assets.hashCode() : 0);
return result;
}
public Human() {
}
public Human(String name, Asset... assets) {
this.name = name;
if (assets != null) {
this.assets.addAll(Arrays.asList(assets));
}
}
public void save(OutputStream outputStream) throws Exception {
// Implement this method
PrintWriter printWriter = new PrintWriter(outputStream);
if (name != null) {
printWriter.println(name);
String isAsset = assets != null ? "yes" : "no";
printWriter.println(isAsset);
}
if (assets != null) {
for (Asset current : assets)
printWriter.println(current.getName());
}
printWriter.close();
}
public void load(InputStream inputStream) throws Exception {
// Implement this method
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
while (reader.ready()) {
String name = reader.readLine();
String isAsset = reader.readLine();
if (isAsset.equals("yes")){
String assetName;
while ((assetName = reader.readLine()) != null){
assets.add(new Asset(assetName));
}
}
}
reader.close();
}
}
}
Pls help!
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Nouser
17 September 2020, 06:15
Validation just asks for one Human object. And it doesn't matter if the name is null.
Therefore it is enough to save the name no matter if it is null. Same for the assets, just save them (without yes:no)
When reading, you read the name. Then you check if there's a next line and if yes, you read as long as there are next lines and add these lines to the assets list.
Cause there's no next Human following that's it.
If you want to make it work for several Human objects (not asked in this task, just if you really want to code it), save a marker (like [[[end-Human]]] or similar) after saving a Human (after the name, if there are no assets, after the assets if you have some).
Loading: after loading the name, check if the next line is the marker, if yes skip reading assets. If not, read till you reach the marker.
0
Youngsan
17 September 2020, 10:39
Modified, but still same problem. I'll try it later. Thanks.
0