一个简单的贷款程序

一个简单的贷款程序

代码如下:

package Day06;
import java.util.Date; public class Loan {
//年利率
private double annualInterestRate;
//贷款年限
private int numberOfYears;
//贷款总额
private double loanAmount;
//贷款生成时间
private Date loanDate;


/**
*
*/
public Loan() {
this(0, 1, 1, new Date());
} /**
* @param annualInterestRate
* @param numberOfYears
* @param loanAmount
*/
public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
this.loanDate = new Date();
} /**
* @param annualInterestRate
* @param numberOfYears
* @param loanAmount
* @param loanDate
*/
public Loan(double annualInterestRate, int numberOfYears,
double loanAmount, Date loanDate) {
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
this.loanDate = loanDate;
} /**
* @return the annualInterestRate
*/
public double getAnnualInterestRate() {
return annualInterestRate;
} /**
* @param annualInterestRate the annualInterestRate to set
*/
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
} /**
* @return the numberOfYears
*/
public int getNumberOfYears() {
return numberOfYears;
} /**
* @param numberOfYears the numberOfYears to set
*/
public void setNumberOfYears(int numberOfYears) {
this.numberOfYears = numberOfYears;
} /**
* @return the loanAmount
*/
public double getLoanAmount() {
return loanAmount;
} /**
* @param loanAmount the loanAmount to set
*/
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
} /**
* @return the loanDate
*/
public Date getLoanDate() {
return loanDate;
} /**
* @param loanDate the loanDate to set
*/
public void setLoanDate(Date loanDate) {
this.loanDate = loanDate;
}

/**
* 计算月还款数额
*/
public double getMonthlyPayment() {
double monthlyInterestRate = this.annualInterestRate / 1200;
double monthPayment =
this.loanAmount * monthlyInterestRate
/ (1 - 1 / Math.pow(1 + monthlyInterestRate, this.numberOfYears * 12));
return monthPayment;
}

/**
* 计算还款总金额
*/
public double getTotalPayment() {
return this.getMonthlyPayment() * this.numberOfYears * 12;
}

/**
* 计算贷款结清时间
*/
@SuppressWarnings("deprecation")
public Date getLoanPaidDate() {
Date loanDay = this.loanDate;
loanDay.setYear(loanDay.getYear() + this.numberOfYears);
return loanDay;
}
}
第二部分
package Day06;

import javax.swing.JOptionPane;

public class TestLoan {
public static void main(String[] args) {
String annualInterestRateString = JOptionPane.showInputDialog("Please enter yearly interest rate(0~100):");
double annualInterestRate = Double.parseDouble(annualInterestRateString);
String numberOfYearsString = JOptionPane.showInputDialog("Please input the number of years:(0~100)");
int numberOfYears = Integer.parseInt(numberOfYearsString);
String loanString = JOptionPane.showInputDialog("Enter loan amount, for example 120000.95:");
double loanAmout = Double.parseDouble(loanString);
Loan loan = new Loan(annualInterestRate, numberOfYears, loanAmout);
System.out.println("贷款生成时间: " + loan.getLoanDate().toString());
System.out.println("贷款结清时间: " + loan.getLoanPaidDate().toString());
System.out.println("月还款数额: " + loan.getMonthlyPayment());
System.out.println("还款总金额: " + loan.getTotalPayment());
}
}

转载于:https://www.cnblogs.com/F001li/p/7055819.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值