0709--第六章2题

模拟一个简单的购房商贷月供计算器

月供计算代码:

/**
 * @author Mr.Wang
 * 获取金额以及年限,计算月供
 *
 */
public class BuyHouse {
    public double counts(double money,int years) {
        double counts = 0;
        switch (years) {
        case 3:
            counts = (money + money*0.0603)/36;
            break;
        case 5:
            counts = (money + money*0.0612)/60;
            break;
        case 20:
            counts = (money + money*0.0639)/240;
            break;

        default:
            break;
        }
        
        return counts;
        
    }
}

用户操作界面代码:

import java.util.Scanner;

/**
 * @author Mr.Wang
 * 用户操作界面,获取输入的贷款金额以及客户选择年限
 *
 */
public class Uer {
    Scanner input = new Scanner(System.in);
    BuyHouse bh = new BuyHouse();
    int years;
    double money;
    double counts;
    public void option() {
        System.out.print("请输入贷款金额:");
        money = input.nextDouble();
        System.out.println("请选择贷款年限:1.3年(36个月)    2.5年(60个月)    3.20年(240个月)");
        int i = input.nextInt();
        switch (i) {
        case 1:
            years = 3;
            counts = bh.counts(money, years);
            System.out.println("3年(36个月)月供为:"+counts);
            break;
        case 2:
            years = 5;
            counts = bh.counts(money, years);
            System.out.println("5年(60个月)月供为:"+counts);
            break;
        case 3:
            years = 20;
            counts = bh.counts(money, years);
            System.out.println("20年(240个月)月供为:"+counts);
            break;

        default:
            System.out.println("谢谢使用本系统!");
            break;
        }
        System.out.println("");
    }
}

测试代码:

/**
 * @author Mr.Wang
 * 模拟一个简单的购房商贷月供计算器
 *
 */
public class Text {
    public static void main(String[] args) {
        Uer uer = new Uer();
        uer.option();
    }
}

运行结果测试:

 

posted on 2019-07-10 15:03  迪恩9  阅读(160)  评论(0编辑  收藏  举报