Skip to content

Commit 113e1a2

Browse files
committed
Work on serializer.
1 parent 27a11aa commit 113e1a2

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.iluwatar;
2+
3+
import java.io.Serializable;
4+
5+
public class RainbowFish implements Serializable {
6+
7+
private static final long serialVersionUID = 1L;
8+
9+
private String name;
10+
private int age;
11+
private int lengthMeters;
12+
private int weightTons;
13+
14+
public String getName() {
15+
return name;
16+
}
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
public int getAge() {
21+
return age;
22+
}
23+
public void setAge(int age) {
24+
this.age = age;
25+
}
26+
public int getLengthMeters() {
27+
return lengthMeters;
28+
}
29+
public void setLengthMeters(int lengthMeters) {
30+
this.lengthMeters = lengthMeters;
31+
}
32+
public int getWeightTons() {
33+
return weightTons;
34+
}
35+
public void setWeightTons(int weightTons) {
36+
this.weightTons = weightTons;
37+
}
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.iluwatar;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.IOException;
5+
import java.io.ObjectOutputStream;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
public class RainbowFishSerializer {
10+
11+
public void write(RainbowFish rainbowFish, String filename) {
12+
Map<String, String> map = new HashMap<>();
13+
map.put("name", rainbowFish.getName());
14+
map.put("age", String.format("%d", rainbowFish.getAge()));
15+
map.put("lengthMeters", String.format("%d", rainbowFish.getLengthMeters()));
16+
map.put("weightTons", String.format("%d", rainbowFish.getWeightTons()));
17+
try {
18+
FileOutputStream fileOut = new FileOutputStream("fish.ser");
19+
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
20+
objOut.writeObject(map);
21+
objOut.close();
22+
fileOut.close();
23+
} catch (IOException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
28+
// public RainbowFish read(String filename) {
29+
// }
30+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.iluwatar;
2+
3+
public class RainbowFishV2 extends RainbowFish {
4+
5+
private boolean sleeping;
6+
private boolean hungry;
7+
private boolean angry;
8+
9+
}

0 commit comments

Comments
 (0)