Skip to content
Snippets Groups Projects
ShapeWriter.java 1.02 KiB
Newer Older
  • Learn to ignore specific revisions
  • BasileCouetoux's avatar
    BasileCouetoux committed
    package serializer;
    
    
    import shape.Circle;
    import shape.Rectangle;
    
    BasileCouetoux's avatar
    BasileCouetoux committed
    import shape.Shape;
    
    import java.io.File;
    import java.io.IOException;
    
    COUETOUX Basile's avatar
    COUETOUX Basile committed
    import java.io.PrintWriter;
    
    BasileCouetoux's avatar
    BasileCouetoux committed
    import java.util.List;
    
    
    public class ShapeWriter implements ShapeVisitor {
    
        private PrintWriter printWriter;
    
        public ShapeWriter(PrintWriter printWriter) {
            this.printWriter = printWriter;
        }
    
    
    BasileCouetoux's avatar
    BasileCouetoux committed
        public static void write (File file, List<Shape> shapes) throws IOException {
    
            try(PrintWriter printWriter = new PrintWriter(file)){
            ShapeWriter shapeWriter = new ShapeWriter(printWriter);
    
            for (Shape shape : shapes) {
                shape.accept(shapeWriter);
            }
            }
        }
    
        @Override
        public void visit(Circle circle) {
            printWriter.printf("Circle %f %f %f%n", circle.getX(), circle.getY(), circle.getRadius());
        }
    
        @Override
        public void visit(Rectangle rectangle) {
            printWriter.printf("Rectangle %f %f %f %f%n", rectangle.getX(), rectangle.getY(), rectangle.getWidth() ,rectangle.getHeight());
    
    BasileCouetoux's avatar
    BasileCouetoux committed
        }
    }