Newer
Older
commitconf24 / snippets / Shape.java
sealed interface Shape {

    record Square(int side) implements Shape {}
    record Rectangle(int weight, int height) implements Shape {}
    record Circle(int radius) implements Shape {}

    default double area() {
        var area = switch (this) {
            case Square(var side) -> side * side;
            case Rectangle(var weight, var height) -> weight * height;
            case Circle(var radius) -> Math.PI * Math.pow(radius, 2);
        };
        return area;
    }
}