Does anyone have any links or documentation that is actually useful for completing these tasks. I tried combing the questions section and didn't find anything and then I tried to just read and write some code I found in the same places hoping to get some understanding but that did not work out at all. I'm getting fed up with the lack of actual teaching this site does while throwing all kinds of tasks with requirements they didn't cover and the worst part is is that is still the best thing for learning to code I have come across on the web. Please, someone set me strait if I am wrong. Thanks in advance!
If I was keen to looking everything up to do a task as hand without help or guidance then why wouldn't I just use Codingame for free because that is all that seems to happen on this site as of late.
package com.codegym.task.task20.task2001;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/*
Reading and writing to a file: Human
*/
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", 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(OutputStream outputStream) throws Exception {
PrintWriter writer = new PrintWriter(outputStream);
String isHumanPresent = name != null ? "yes" : "no";
writer.println(isHumanPresent);
;
if (name != null) {
for (Asset a : assets) {
writer.println(a.getName());
writer.println(a.getPrice());
}
}
writer.flush();
writer.close();
}
public void load(InputStream inputStream) throws Exception {
Scanner scanner = new Scanner(inputStream);
name = scanner.nextLine();
String isAssetPresent = scanner.nextLine();
if (isAssetPresent.equals("yes")) {
while (scanner.hasNext()) {
Asset asset = new Asset(scanner.nextLine(), Double.parseDouble(scanner.nextLine()));
assets.add(asset);
}
scanner.close();
}
}
}
}