Skip to content
Snippets Groups Projects
Commit d21a1951 authored by MANSOUR Chadi's avatar MANSOUR Chadi
Browse files

corrected the ShapeWriter class

parent 0784c6fd
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -7,17 +7,26 @@ import shape.Shape;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ShapeWriter implements ShapeVisitor {
private PrintWriter printWriter;
private static final Map<Class<? extends Shape>, ShapeSerializer<? extends Shape>> serializers = new HashMap<>();
static {
serializers.put(Circle.class, new CircleSerializer());
serializers.put(Rectangle.class, new RectangleSerializer());
}
public ShapeWriter(PrintWriter printWriter) {
this.printWriter = printWriter;
}
public static void write (File file, List<Shape> shapes) throws IOException {
public static void write (File file, Iterable<Shape> shapes) throws IOException {
try(PrintWriter printWriter = new PrintWriter(file)){
ShapeWriter shapeWriter = new ShapeWriter(printWriter);
......@@ -29,11 +38,23 @@ public class ShapeWriter implements ShapeVisitor {
@Override
public void visit(Circle circle) {
printWriter.printf("Circle %f %f %f%n", circle.getX(), circle.getY(), circle.getRadius());
ShapeSerializer<Circle> serializer = (ShapeSerializer<Circle>) serializers.get(Circle.class);
if (serializer != null) {
String serializedData = serializer.serialize(circle);
printWriter.println(serializedData);
} else {
throw new IllegalArgumentException("No serializer found for Circle");
}
}
@Override
public void visit(Rectangle rectangle) {
printWriter.printf("Rectangle %f %f %f %f%n", rectangle.getX(), rectangle.getY(), rectangle.getWidth() ,rectangle.getHeight());
ShapeSerializer<Rectangle> serializer = (ShapeSerializer<Rectangle>) serializers.get(Rectangle.class);
if (serializer != null) {
String serializedData = serializer.serialize(rectangle);
printWriter.println(serializedData);
} else {
throw new IllegalArgumentException("No serializer found for Rectangle");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment