本文主要是介绍(IllegalArgumentException异常)修改程序清单10-2中的Loan类,如果贷款总额、利率、年数小于或等于零,则抛出IllegalArgumentException异常,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
public class Loan {
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private java.util.Date loanDate;
public Loan() {
this(2.5, 1, 1000);
}
public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new java.util.Date();
judge();
}
public void judge() throws IllegalArgumentException{
if(this.loanAmount<=0) {
throw new IllegalArgumentException("贷款总额小于等于0!");
}
if(this.annualInterestRate<=0) {
throw new IllegalArgumentException("利率小于等于0!");
}
if(this.numberOfYears<=0) {
throw new IllegalArgumentException("贷款年数小于等于0!");
}
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
judge();
}
public int getNumberOfYears() {
return numberOfYears;
}
public void setNumberOfYears(int numberOfYears) {
this.numberOfYears = numberOfYears;
judge();
}
public double getLoanAmount() {
return loanAmount;
}
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
judge();
}
public double getMonthlyPayment() {
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (1 / Math.pow(1+monthlyInterestRate, numberOfYears * 12)));
return monthlyPayment;
}
public double getTotalPayment() {
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
return totalPayment;
}
public java.util.Date getLoanDate(){
return loanDate;
}
}
这篇关于(IllegalArgumentException异常)修改程序清单10-2中的Loan类,如果贷款总额、利率、年数小于或等于零,则抛出IllegalArgumentException异常的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!