Java实验—银行存款的计算

Java实验—银行存款的计算

文章目录

  • Java实验—银行存款的计算
    • 简介
    • 代码

简介

其实本身是一个很简单的实验程序,之前由于考虑要不要使用大数高精度计算而浪费了许多时间。注意数据类型尽量设置为double类型,以免造成精度的丢失(爆0,程序直接异常)。
另外由于没有看清楚实验要求,定义了好多无效的变量。。。后面根本用不到。

代码

package scuec.cscollege.javatest1;
import java.util.Scanner;
class Money{
    double loanMoney;//贷款本金
    double loanPeriod;//贷款年限
    double yearRate;//总利率
    double month;//还款月数
    double monthlyPayment;//每月金额
    double totalInstrest;//等额利息
    double havePay;//已偿还的贷款
    int fun;//还款方式
    double monthRate;//月利率
    double date;//还款月序号

    double judge(){//计算月利息
        if(month<=120) monthRate=0.003;
        if(month<=240&&month>=120) monthRate=0.004;
        else monthRate=0.006;
        return monthRate;
    }
}
class meanMoney extends Money{//等额本金
    double cal_1(){//计算月供
        return  loanMoney/month+(loanMoney-havePay)*monthRate;

    }
    double cal_2(){//每月应还本金
        return loanMoney/month;
    }
    double cal_3(){//每月应还贷款数
        return (loanMoney-havePay)*monthRate;
    }
    double cal_4(){//每月月供递减
        return loanMoney/month*monthRate;
    }
    double cal_5(){//总利息
        return  ((loanMoney/month+loanMoney*monthRate)+loanMoney/month*(1+monthRate))/2*month-loanMoney;
    }
}
class meanRate extends Money{//等息本金
     double rate=Math.pow((1+monthRate),month);
     double temp=Math.pow((1+monthRate),(date-1));
    double cal_1(){//月供额
        return (loanMoney*monthRate*rate/(rate-1));
    }
    double cal_2(){//每月偿还本息
        return (loanMoney*monthRate*rate/(rate-1));
    }
    double cal_3(){//每月偿还利息
        return (loanMoney*monthRate*(rate-temp)/(rate-1));
    }
    double cal_4(){//每月偿还本金
        return  (loanMoney*monthRate*temp/(rate-1));
    }
    double cal_5(){//总利息
        return month*cal_1()-loanMoney;
    }
}
public class AverageCapitalPlusInterest {
    public static void main(String[] args) {
        Money my=new Money();
        Scanner scan=new Scanner(System.in);
        System.out.print("请输入贷款本金:");
        my.loanMoney=scan.nextDouble();
        scan.nextLine();
        System.out.print("请输入已还贷款:");
        my.havePay=scan.nextDouble();
        scan.nextLine();
        System.out.print("请输入贷款年限:");
        my.loanPeriod=scan.nextDouble();
        scan.nextLine();
        System.out.print("请输入贷款月数:");
        my.month=scan.nextDouble();
        scan.nextLine();
        my.judge();
        System.out.print("请选择你的还款方式,等息本金选择1,等额本金选择2");
        my.fun=scan.nextInt();
        if(my.fun==2){
            meanRate mr=new meanRate();
            System.out.println("你的月供金额是:"+mr.cal_1()+"\n");
            System.out.println("你一共支付了:"+mr.cal_5()+"\n");
        }
        else if(my.fun==1){
            meanMoney mm=new meanMoney();
            System.out.println("你的月供金额是:"+mm.cal_1()+"\n");
            System.out.println("你一共支付了:"+mm.cal_5()+"\n");
        }
    }
}

你可能感兴趣的:(课程设计/实验报告,Java)