[JAVA] 3. Java中的重构

Posted modai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[JAVA] 3. Java中的重构相关的知识,希望对你有一定的参考价值。

1. 重构

为了让之前的贷款额度计算程序简化,使其更符合DRY的原则。

这里就将输入数字抽取为一个函数,然后将计算贷款抽取成一个函数。

import java.util.Scanner;

public class Main {
    private static double getNumber(String prompt, double min, double max,Scanner scanner) {
        double value = 0;
        while (true) {
            System.out.print(prompt + ‘:‘);
            value = scanner.nextFloat();
            if (value > min && value < max)
                break;
            System.out.println("数据应当在" + min + " " + max + "之间。");
        }
        return value;
    }

    private static double getMortgage(float principle, float interest, byte years) {
        final byte MONTH = 12;
        final byte PERCENT = 100;
        float monthlyInterest = interest / MONTH / PERCENT;
        short numberOfMonth = (short) (MONTH * years);
        double mortgage = principle
                * monthlyInterest * Math.pow(1 + monthlyInterest, numberOfMonth)
                / (Math.pow(1 + monthlyInterest, numberOfMonth) - 1);
        return mortgage;

    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        float principle = (float)getNumber("请输入本金",1000,1_000_000,scanner);
        float interest = (float)getNumber("请输入年利率",1,2,scanner);
        byte years = (byte)getNumber("请输入贷款年限",5,20,scanner);
        double mortgage = getMortgage(principle,interest,years);
        System.out.println("月供为:"+mortgage);

    }
}

之后重构的时候,会遇到函数中的长两个在多个函数中出现的情况,这时,我们应该将这个常量,放在类内,作为类变量,当然注意函数为static,它也能读取static的常量。而后,会发现月份数和每月利率这两个数值也是重复的,但是这两个数值并不会在其他函数中变更,属于函数之间的独立项,所以虽然重复了,但是也可以不更改(当然想让其作为类内变量也可以,只不过最后类内变量会变得较多)。

这个在后面的面向对象的编程中,会有更好的解决方案。

2. 将还款之后的剩余款项(结余)打印出来

import java.util.Scanner;

public class Main {
    final static byte MONTH = 12;
    final static byte PERCENT = 100;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        float principle = (float) getNumber("请输入本金", 1000, 1_000_000, scanner);
        float interest = (float) getNumber("请输入年利率", 1, 2, scanner);
        byte years = (byte) getNumber("请输入贷款年限", 5, 20, scanner);

        double mortgage = getMortgage(principle, interest, years);
        System.out.println();
        System.out.println(" 贷 款 ");
        System.out.println("-------");
        System.out.println("月供为:" + mortgage);

        System.out.println();
        System.out.println(" 结 余 ");
        System.out.println("-------");
        for (short month = 1; month <= years * MONTH; month++) {
            double balance = getBalance(principle,interest,years,month);
            System.out.println(month+"月之后的结余: "+balance);
        }
    }

    private static double getNumber(String prompt, double min, double max, Scanner scanner) {
        double value = 0;
        while (true) {
            System.out.print(prompt + ‘:‘);
            value = scanner.nextFloat();
            if (value > min && value < max)
                break;
            System.out.println("数据应当在" + min + " " + max + "之间。");
        }
        return value;
    }

    private static double getBalance(
            float principle,
            float interest,
            byte years,
            short numberOfPaymentsMade
    ) {
        float monthlyInterest = interest / MONTH / PERCENT;
        short numberOfMonth = (short) (MONTH * years);
        double balance = principle
                * (Math.pow(1 + monthlyInterest, numberOfMonth) - Math.pow(1 + monthlyInterest, numberOfPaymentsMade))
                / (Math.pow(1 + monthlyInterest, numberOfMonth) - 1);
        return balance;
    }

    private static double getMortgage(float principle, float interest, byte years) {
        float monthlyInterest = interest / MONTH / PERCENT;
        short numberOfMonth = (short) (MONTH * years);
        double mortgage = principle
                * monthlyInterest * Math.pow(1 + monthlyInterest, numberOfMonth)
                / (Math.pow(1 + monthlyInterest, numberOfMonth) - 1);
        return mortgage;

    }
}

以上是关于[JAVA] 3. Java中的重构的主要内容,如果未能解决你的问题,请参考以下文章

LockSupport.java 中的 FIFO 互斥代码片段

自动将通配符导入重构为 IntelliJ 中的显式导入(用于 Scala/Java)

java代码在片段活动中不起作用

《Java8实战》读书笔记07:Lambda 重构测试和调试(设计模式实现)

《Java8实战》读书笔记07:Lambda 重构测试和调试(设计模式实现)

java重构原则