package cn.bunny.pattern9;

import lombok.Data;

import java.util.Random;

public class Adapter3 {
    public static void main(String[] args) {
        Power power = new Power();
        PowerAdapter powerAdapter = new Power220VAdapter(power);
        Integer integer = powerAdapter.output220V();
    }

    // 电压输出
    interface PowerTarget {
        /**
         * 输出电压24V
         *
         * @return 输出电压
         */
        Integer output24V();

        /**
         * 输出电压110V
         *
         * @return 输出电压
         */
        Integer output110V();

        /**
         * 输出电压220V
         *
         * @return 输出电压
         */
        Integer output220V();
    }

    @Data
    static abstract class PowerAdapter implements PowerTarget {
        protected Power power;

        public PowerAdapter(Power power) {
            this.power = power;
        }

        /**
         * 输出电压24V
         *
         * @return 输出电压
         */
        @Override
        public Integer output24V() {
            return 0;
        }

        /**
         * 输出电压110V
         *
         * @return 输出电压
         */
        @Override
        public Integer output110V() {
            return 0;
        }

        /**
         * 输出电压220V
         *
         * @return 输出电压
         */
        @Override
        public Integer output220V() {
            return 0;
        }
    }

    // 缺省适配器转换器
    static class Power220VAdapter extends PowerAdapter {
        public Power220VAdapter(Power power) {
            super(power);
        }

        /**
         * 输出电压220V
         *
         * @return 输出电压
         */
        @Override
        public Integer output220V() {
            Integer output = power.getOutput();
            System.out.println("变压器开始转换：" + output + "v转220v三相电变两项电。。。");

            double adapter = new Random().nextDouble(1.64, 1.72);
            Integer newOutput = (int) (output / adapter);
            System.out.println("变压器转换完成：电压输出为：" + newOutput);

            return newOutput;
        }
    }

    @Data
    static class Power {
        private Integer output = 380;

        public Integer output380V() {
            System.out.println("输出电压：" + output);
            return output;
        }
    }
}
