Newer
Older
import shape.Circle;
import shape.Rectangle;
import shape.Shape;
import java.io.File;
import java.io.IOException;
public class ShapeWriter implements ShapeVisitor {
private PrintWriter printWriter;
public ShapeWriter(PrintWriter printWriter) {
this.printWriter = printWriter;
}
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());