码迷,mamicode.com
首页 > 编程语言 > 详细

[JAVA] 5. Java重构之后的贷款计算器

时间:2020-04-22 22:54:02      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:ack   pac   res   get   text   for   amp   enc   format   

MorgageCalculator


package com.modai;

import java.util.Scanner;

public class MorgageCalculator {
    public final static byte MONTH = 12;
    public final static byte PERCENT = 100;
    private float principle;

    private float monthlyInterest;
    private short numberOfMonth;


    public MorgageCalculator(float principle, float interest, byte years) {
        this.principle = principle;
        monthlyInterest = interest / MONTH / PERCENT;
        numberOfMonth = (short) (MONTH * years);
    }

    public 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;
    }


    public double getBalance(short numberOfPaymentsMade) {
        double balance = principle
                * (Math.pow(1 + monthlyInterest, numberOfMonth) - Math.pow(1 + monthlyInterest, numberOfPaymentsMade))
                / (Math.pow(1 + monthlyInterest, numberOfMonth) - 1);
        return balance;
    }

    public double getMortgage() {
        double mortgage = principle
                * monthlyInterest * Math.pow(1 + monthlyInterest, numberOfMonth)
                / (Math.pow(1 + monthlyInterest, numberOfMonth) - 1);
        return mortgage;

    }
    public double[] getBalanceMonthly(){
        var balanceArr = new double[numberOfMonth];
        for (short month = 1; month <= numberOfMonth; month++) {
            balanceArr[month-1]= getBalance(month);
        }
        return balanceArr;
    }
}

Console.java

package com.modai;

import java.text.NumberFormat;

public class Console {
    private static MorgageCalculator calcuator;
    private static NumberFormat currency;

    public Console(MorgageCalculator calcuator) {
        this.calcuator = calcuator;

        currency = NumberFormat.getCurrencyInstance();
    }

    public static void printBalanceInfo(byte years) {
        System.out.println(" 结 余 ");
        System.out.println("-------");
        for (double balance: calcuator.getBalanceMonthly())
            System.out.println("月结余: " + currency.format(balance));

    }

    public static void printMorgageInfo() {
        double mortgage = calcuator.getMortgage();
        System.out.println();
        System.out.println(" 贷 款 ");
        System.out.println("-------");
        System.out.println("月供为:" + currency.format(mortgage));
        System.out.println();
    }
}

Main.java

import java.util.Scanner;

public class Main {

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

        var morgageCalculator = new MorgageCalculator(principle, interest, years);
        var console = new Console(morgageCalculator);
        console.printMorgageInfo();
        console.printBalanceInfo();
    }

}

[JAVA] 5. Java重构之后的贷款计算器

标签:ack   pac   res   get   text   for   amp   enc   format   

原文地址:https://www.cnblogs.com/modai/p/12751001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!