X Tutup
Skip to content

Commit b58311a

Browse files
committed
Completed Tolerant Reader example.
1 parent 113e1a2 commit b58311a

File tree

5 files changed

+74
-28
lines changed

5 files changed

+74
-28
lines changed
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
package com.iluwatar;
22

3-
public class App
4-
{
5-
public static void main( String[] args ) {
3+
import java.io.IOException;
4+
5+
public class App {
6+
7+
public static void main( String[] args ) throws IOException, ClassNotFoundException {
8+
RainbowFish fishV1 = new RainbowFish("Zed", 10, 11, 12);
9+
RainbowFishSerializer.write(fishV1, "fish1.out");
10+
RainbowFish deserializedFishV1 = RainbowFishSerializer.read("fish1.out");
11+
System.out.println(String.format("deserializedFishV1 name=%s age=%d length=%d weight=%d", deserializedFishV1.getName(),
12+
deserializedFishV1.getAge(), deserializedFishV1.getLengthMeters(), deserializedFishV1.getWeightTons()));
13+
RainbowFishV2 fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true);
14+
RainbowFishSerializer.write(fishV2, "fish2.out");
15+
RainbowFish deserializedFishV2 = RainbowFishSerializer.read("fish2.out");
16+
System.out.println(String.format("deserializedFishV2 name=%s age=%d length=%d weight=%d", deserializedFishV2.getName(),
17+
deserializedFishV2.getAge(), deserializedFishV2.getLengthMeters(), deserializedFishV2.getWeightTons()));
618
}
719
}

tolerant-reader/src/main/java/com/iluwatar/RainbowFish.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,27 @@ public class RainbowFish implements Serializable {
1111
private int lengthMeters;
1212
private int weightTons;
1313

14+
public RainbowFish(String name, int age, int lengthMeters, int weightTons) {
15+
this.name = name;
16+
this.age = age;
17+
this.lengthMeters = lengthMeters;
18+
this.weightTons = weightTons;
19+
}
20+
1421
public String getName() {
1522
return name;
1623
}
17-
public void setName(String name) {
18-
this.name = name;
19-
}
24+
2025
public int getAge() {
2126
return age;
2227
}
23-
public void setAge(int age) {
24-
this.age = age;
25-
}
28+
2629
public int getLengthMeters() {
2730
return lengthMeters;
2831
}
29-
public void setLengthMeters(int lengthMeters) {
30-
this.lengthMeters = lengthMeters;
31-
}
32+
3233
public int getWeightTons() {
3334
return weightTons;
3435
}
35-
public void setWeightTons(int weightTons) {
36-
this.weightTons = weightTons;
37-
}
36+
3837
}
Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
package com.iluwatar;
22

3+
import java.io.FileInputStream;
4+
import java.io.FileNotFoundException;
35
import java.io.FileOutputStream;
46
import java.io.IOException;
7+
import java.io.ObjectInputStream;
58
import java.io.ObjectOutputStream;
69
import java.util.HashMap;
710
import java.util.Map;
811

912
public class RainbowFishSerializer {
1013

11-
public void write(RainbowFish rainbowFish, String filename) {
14+
public static void write(RainbowFish rainbowFish, String filename) throws IOException {
1215
Map<String, String> map = new HashMap<>();
1316
map.put("name", rainbowFish.getName());
1417
map.put("age", String.format("%d", rainbowFish.getAge()));
1518
map.put("lengthMeters", String.format("%d", rainbowFish.getLengthMeters()));
1619
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-
}
20+
FileOutputStream fileOut = new FileOutputStream(filename);
21+
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
22+
objOut.writeObject(map);
23+
objOut.close();
24+
fileOut.close();
2625
}
2726

28-
// public RainbowFish read(String filename) {
29-
// }
27+
public static RainbowFish read(String filename) throws IOException, ClassNotFoundException {
28+
Map<String, String> map = null;
29+
FileInputStream fileIn = new FileInputStream(filename);
30+
ObjectInputStream objIn = new ObjectInputStream(fileIn);
31+
map = (Map<String, String>) objIn.readObject();
32+
objIn.close();
33+
fileIn.close();
34+
return new RainbowFish(map.get("name"),
35+
Integer.parseInt(map.get("age")),
36+
Integer.parseInt(map.get("lengthMeters")),
37+
Integer.parseInt(map.get("weightTons")));
38+
}
3039
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
package com.iluwatar;
22

33
public class RainbowFishV2 extends RainbowFish {
4+
5+
private static final long serialVersionUID = 1L;
46

57
private boolean sleeping;
68
private boolean hungry;
79
private boolean angry;
810

11+
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons) {
12+
super(name, age, lengthMeters, weightTons);
13+
}
14+
15+
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons, boolean sleeping, boolean hungry, boolean angry) {
16+
this(name, age, lengthMeters, weightTons);
17+
this.sleeping = sleeping;
18+
this.hungry = hungry;
19+
this.angry = angry;
20+
}
21+
22+
public boolean getSleeping() {
23+
return sleeping;
24+
}
25+
26+
public boolean getHungry() {
27+
return hungry;
28+
}
29+
30+
public boolean getAngry() {
31+
return angry;
32+
}
933
}

tolerant-reader/src/test/java/com/iluwatar/AppTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.iluwatar;
22

3+
import java.io.IOException;
4+
35
import org.junit.Test;
46

57

68
public class AppTest {
79

810
@Test
9-
public void test() {
11+
public void test() throws ClassNotFoundException, IOException {
1012
String[] args = {};
1113
App.main(args);
1214
}

0 commit comments

Comments
 (0)
X Tutup