Repository navigation
Expand file tree
/
Copy pathSolution.java
More file actions
114 lines (93 loc) · 4.53 KB
/
Copy pathSolution.java
File metadata and controls
114 lines (93 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.javarush.task.task20.task2001;
import java.io.*;
import java.util.*;
// Читаем и пишем в файл: Human
// Реализуй логику записи в файл и чтения из файла для класса Human.
// Поле name в классе Human не может быть пустым.
// Метод main реализован только для вас и не участвует в тестировании.
public class Solution {
public static void main(String[] args) {
//исправьте outputStream/inputStream в соответствии с путем к вашему реальному файлу
try {
File file1 = File.createTempFile("C:\\Users\\Igor\\Desktop\\data1.txt", null);
OutputStream outputStream = new FileOutputStream(file1);
InputStream inputStream = new FileInputStream(file1);
Human ivanov = new Human("Ivanov", new Asset("home"), new Asset("car")); //asset in english = актив
ivanov.save(outputStream); //wrote
outputStream.flush(); //flush the buffer
Human somePerson = new Human(); //create new Human
somePerson.load(inputStream); //and load it from the file
if(ivanov.equals(somePerson)) //check that loaded Human is equals our saved Human
System.out.println("they are equal");
else
System.out.println("they are not equal");
inputStream.close();
} catch (IOException e) {
//e.printStackTrace();
System.out.println("Oops, something wrong with my file");
} catch (Exception e) {
//e.printStackTrace();
System.out.println("Oops, something wrong with save/load method");
}
}
public static class Human {
public String name;
public List<Asset> assets = new ArrayList<>();
public Human() {
}
@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(String name, Asset... assets) {
this.name = name;
if (assets != null) {
this.assets.addAll(Arrays.asList(assets));
}
}
public void save(OutputStream outputStream) throws Exception {
PrintWriter writer = new PrintWriter(outputStream);
String isNameExist = (name != null) ? "yes" : "no"; //we are marking file - is there an information about object??? - it will need us later - during loading
writer.println(isNameExist);
if(isNameExist.equals("yes")) {
writer.println(name);
if(assets.size() > 0){
for(Asset asset : assets){
writer.println(asset.getName());
writer.println(asset.getPrice());
}
}
}
writer.flush();
writer.close();
}
public void load(InputStream inputStream) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String isNameExist = reader.readLine();
if(isNameExist.equals("yes")){
name = reader.readLine();
try {
String s = reader.readLine();
this.assets = new ArrayList<>();
while (!s.equals("")) {
Asset as = new Asset(s);
as.setPrice(Double.parseDouble(reader.readLine()));
this.assets.add(as);
s = reader.readLine();
}
}catch (NullPointerException e){}
}
reader.close();
}
}
}