第9次做業--接口及接口回調

題目描述

利用接口和接口回調,實現簡單工廠模式,當輸入不一樣的字符,表明相應圖形時,利用工廠類得到圖形對象,再計算以該圖形爲底的柱體體積。java

源代碼

Shape 接口

package com.tomotoes.probleam.nine;

public interface Shape {
    double getArea();
}

工廠類

package com.tomotoes.probleam.nine;

import com.tomotoes.probleam.nine.shapes.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.function.Supplier;

public class Factory {
    private static Scanner scanner = new Scanner(System.in);

    private static double input() {
        return scanner.nextDouble();
    }

    private static Map<String, Supplier<Shape>> factory = new HashMap<>();

    static {
        factory.put("rect", () -> new Rect(input(), input()));
        factory.put("circle", () -> new Circle(input()));
        factory.put("square", () -> new Square(input()));
        factory.put("trapezoid", () -> new Trapezoid(input(), input(), input()));
        factory.put("triangle", () -> new Triangle(input(), input(), input()));
    }

    public static Shape getInstance(String type) {
        return factory.getOrDefault(type, () -> null).get();
    }
}

主類

package com.tomotoes.probleam.nine;

import com.tomotoes.probleam.nine.shapes.Column;

import java.util.Objects;
import java.util.Scanner;

public class App {
    private static Scanner scanner = new Scanner(System.in);

    public static void init() {
        System.out.println("矩形類:   輸入 rect -> Rect(寬,長)");
        System.out.println("圓形類:   輸入 circle -> Circle(半徑)");
        System.out.println("三角形類: 輸入 triangle -> Triangle(底邊,高,斜邊)");
        System.out.println("正方形類: 輸入 square -> Square(邊長)");
        System.out.println("梯形類: 輸入 trapezoid -> Trapezoid(頂邊,底邊,高)");
        System.out.printf("輸入其餘值, 程序退出.\n\n");
    }

    public static void main(String[] args) {
        init();
        while (true) {
            System.out.println("請輸入類型參數: ");
            final String input = scanner.next().trim().toLowerCase();

            System.out.println("請輸入類所需的構造參數: ");
            Shape shape = Factory.getInstance(input);

            if (Objects.isNull(shape)) {
                System.out.println("程序退出.");
                return;
            }

            System.out.println("請輸入柱體的高: ");
            final double height = scanner.nextDouble();

            Column column = new Column(height, shape);
            System.out.printf("柱體的體積爲: " + column.getVolume() + "\n\n");
        }

    }
}

柱體類

package com.tomotoes.probleam.nine.shapes;

import com.tomotoes.probleam.nine.Shape;

public class Column {
    double height;
    Shape shape;

    public Column(double height, Shape shape) {
        this.height = height;
        this.shape = shape;
    }

    public double getVolume() {
        return height * shape.getArea();
    }
}

矩形類

package com.tomotoes.probleam.nine.shapes;

import com.tomotoes.probleam.nine.Shape;

public class Rect implements Shape {
    double width;
    double length;

    public Rect(double width, double length) {
        this.width = width;
        this.length = length;
    }

    @Override
    public double getArea() {
        return width * length;
    }
}

圓形類

package com.tomotoes.probleam.nine.shapes;

import com.tomotoes.probleam.nine.Shape;

public class Circle implements Shape {
    double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.pow(radius, 2) * Math.PI;
    }
}

正方形類

package com.tomotoes.probleam.nine.shapes;

import com.tomotoes.probleam.nine.Shape;

public class Square implements Shape {
    double length;

    public Square(double length) {
        this.length = length;
    }

    @Override
    public double getArea() {
        return length * length;
    }
}

梯形類

package com.tomotoes.probleam.nine.shapes;

import com.tomotoes.probleam.nine.Shape;

public class Trapezoid implements Shape {
    double top;
    double base;
    double height;

    public Trapezoid(double top, double base, double height) {
        this.top = top;
        this.base = base;
        this.height = height;
    }

    @Override
    public double getArea() {
        return (top + base) * height * 2;
    }
}

三角形類

package com.tomotoes.probleam.nine.shapes;

import com.tomotoes.probleam.nine.Shape;

public class Triangle implements Shape {
    double base;
    double height;
    double oblique;

    public Triangle(double base, double height, double oblique) {
        this.base = base;
        this.height = height;
        this.oblique = oblique;
    }

    @Override
    public double getArea() {
        return base * height / 2;
    }
}

運行截圖

相關文章
相關標籤/搜索