一个简单的贷款程序
代码如下:
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());
}
}