001package com.ericlam.mc.bungee.hnmc.builders;
002
003import com.ericlam.mc.bungee.hnmc.builders.function.Calculation;
004
005/**
006 * 計算機
007 */
008public class CalculationBuilder {
009    private double result;
010
011    public CalculationBuilder(double result) {
012        this.result = result;
013    }
014
015    /**
016     * @param add +
017     * @return this
018     */
019    public CalculationBuilder add(double add) {
020        this.result += add;
021        return this;
022    }
023
024    /**
025     * @param minus -
026     * @return this
027     */
028    public CalculationBuilder minus(double minus) {
029        this.result -= minus;
030        return this;
031    }
032
033    /**
034     * @param multiply *
035     * @return this
036     */
037    public CalculationBuilder multiply(double multiply) {
038        this.result *= multiply;
039        return this;
040    }
041
042    /**
043     * @param divide /
044     * @return this
045     */
046    public CalculationBuilder divide(double divide) {
047        this.result /= divide;
048        return this;
049    }
050
051    /**
052     * @param pow ^
053     * @return this
054     */
055    public CalculationBuilder pow(double pow) {
056        this.result = Math.pow(this.result, pow);
057        return this;
058    }
059
060    /**
061     * @return √x
062     */
063    public CalculationBuilder sqrt() {
064        this.result = Math.sqrt(this.result);
065        return this;
066    }
067
068    /**
069     * @return *3.14
070     */
071    public CalculationBuilder pi() {
072        this.result *= Math.PI;
073        return this;
074    }
075
076    /**
077     * @return ⌊x⌋
078     */
079    public CalculationBuilder roundDown() {
080        this.result = Math.floor(this.result);
081        return this;
082    }
083
084    /**
085     * @return 四捨五入
086     */
087    public CalculationBuilder round() {
088        this.result = Math.round(this.result);
089        return this;
090    }
091
092    /**
093     * @return ⌈x⌉
094     */
095    public CalculationBuilder roundUp() {
096        this.result = Math.ceil(this.result);
097        return this;
098    }
099
100    /**
101     * @param calculate 計算函式
102     * @return this
103     * @see Calculation
104     */
105    public CalculationBuilder doOther(Calculation calculate) {
106        this.result = calculate.cal(this.result);
107        return this;
108    }
109
110    /**
111     * @return 計算結果
112     */
113    public double getResult() {
114        return result;
115    }
116}
117// 如何使用
118/*
119class CalculationUse{
120    double result = new CalculationBuilder(5).add(7).divide(6).minus(2).multiply(10).pow(2).round().getResult();
121    // ((((5 + 7) / 6 ) - 2 ) * 10 ) ^ 2
122}
123
124 */