I just copied some code from others and from the lesson 1 but I do not understand it. Maybe someone can revert me to another source to understand the concepts behind this task?
For now a couple of questions
1) Why do we create a variable to check for instance if it has a name. What does it add? Would you just not just getName() and see if it is null?
2) Same question to check if a human has assets. It seems such a convoluted way to check is the array is empty or not. What is the point that I am missing.
3) I'm able to save the human to the file. He is there even though the validator does not think so 😛. But I do not understand what is meant by loading. Is it something different than just reading what is in the file? And why make it again so completed with the isPresent boolean. What does it add?
And what is this part doing
Human somePerson = new Human();
somePerson.load(inputStream);
inputStream.close();
// Check that smith is equal to somePerson
//why would you want to do that. 2 objects are created...
package com.codegym.task.task20.task2001;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
Reading and writing to a file: Human
1. The read/write logic in the save/load methods must work correctly when the assets list is empty.
*/
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 {
//createTempFile should create a new file but doesn't why?
//if I use an exiting file is does not write to it why?
//it only works if I add the filepath in the contructor of the outputstream.
File yourFile = File.createTempFile("/Users/lilianetop/Desktop/taak2001A", ".txt");
OutputStream outputStream = new FileOutputStream(yourFile);
//OutputStream outputStream = new FileOutputStream("/Users/lilianetop/Desktop/taak2001.txt");
InputStream inputStream = new FileInputStream(yourFile);
Human smith = new Human("Smith", new Asset("home", 999_999.99), new Asset("car", 2999.99));
smith.save(outputStream);
outputStream.flush();
Human somePerson = new Human();
somePerson.load(inputStream);
inputStream.close();
// Check that smith is equal to somePerson
} 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<>();
public Human() {
}
public Human(String name, Asset... assets) {
this.name = name;
if (assets != null) {
this.assets.addAll(Arrays.asList(assets));
}
}
@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 void save(PrintWriter writer) throws Exception {
String isCatPresent = cat != null ? "yes" : "no";
writer.println(isCatPresent);
writer.flush();
if (cat != null)
cat.save(writer);
String isDogPresent = dog != null ? "yes" : "no";
writer.println(isDogPresent);
writer.flush();
if (dog != null)
dog.save(writer);
}
*/
public void save(OutputStream outputStream) throws Exception {//writes in bytes
PrintWriter printWriter = new PrintWriter(outputStream);
StringBuilder storage = new StringBuilder();//stores String
//create a variable to check if name is present
String isNamePresent = name != null ? "Yes" : "No";
storage.append(isNamePresent + " ");
//check if name exists
if(name != null){
//store the name of the human
storage.append(name + " ");
}
//create a variable to check if there are any assets
String hasAssets = assets != null ? "Yes" : "No";
storage.append(hasAssets + " ");
//if there are assets store them
if(!assets.isEmpty()){
for(Asset asset : assets){
storage.append(asset.getName() + " ");
storage.append(asset.getPrice() + " ");
}
}
//write storage to the outputfile
printWriter.write(storage.toString());
printWriter.close();
}
public void load(InputStream inputStream) throws Exception {
// Implement this method
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder storage = new StringBuilder();
String line;
while(inputStream.available() > 0){
line = reader.readLine();
storage.append(line);
}
reader.close();
System.out.println(storage.toString());
}
}
}