日常作业2019.1.12

作业:购房商贷计算器

package com.job1_12;

import java.util.Scanner;
/**
 * 购房商贷计算器	类
 * @author ZZH
 *	2019.1.12
 */
public class Counter {
	/**
	 * 房贷计算机属性
	 */
	private double monthPay;		//月供 
	private int month;		     //月
	private double interest;		//利息
	private double money;			//金额

	/**
	 * 防止money为负数
	 */
	public double minus(double money) {
		Scanner sc=new Scanner(System.in);
		while(this.money<0) {
			if(this.money<0) {
				System.out.println("请输入正确的数值");	
				this.money=sc.nextInt();
			}
		}
		return money;
	}
	/**
	 * 客户输入金额系统
	 */
	public void counter() {
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入贷款金额");
		this.money=sc.nextInt();
		this.minus(money);			//调用防止money为负数方法
		System.out.println("请选择贷款年限:1、三年(36个月)  2、五年(60个月)  3、二十年(240个月)");
		String choose=sc.next();
		switch(choose) {
		case "1":
			this.interest=this.money*0.0603;			//三年(36个月)的利息
			this.month=36;
			break;
		case "2":
			this.interest=this.money*0.0612;			//五年(60个月)的利息
			this.month=60;
			break;
		case "3":
			this.interest=this.money*0.0639;			//二十年(240个月)的利息
			this.month=240;
			break;
		default:
			System.out.println("请输入正确的数值!");
		}
	}

	/**
	 * 展示结果
	 */
	public void showInfo() {
		this.monthPay=(this.money+this.interest)/this.month;
		System.out.println("月供为:"+this.monthPay+"元  总利息为:"+this.interest+"元");
	}
}

main方法

 

package com.job1_12;
/**
 * 购房商贷计算器
 * @author     ZZH
 *    2019.1.12
 */
public class CounterTest {
    public static void main(String[] args) {
        Counter count=new Counter();        //创建对象
        count.counter();                    //调用counter对象
        count.showInfo();                    //调用shouInfo对象展示结果
    }
}

 

运行

 

posted @ 2019-01-12 21:06  纸灰  阅读(168)  评论(0编辑  收藏  举报