用Java写PTA 7-11 设计一个能处理异常的Loan类
定义一个贷款类Loan,其中有属性:
annualInterestRate:double,表示贷款的年利率(默认值:2.5)
numberOfYears:int,表示贷款的年数(默认值:1)
loanAmount:double,表示贷款额(默认值:100)
loanDate:java.util.Date,表示创建贷款的日期
定义方法:
(1)默认的无参构造方法
(2)带指定利率、年数和贷款额的构造方法
(3)所有属性的get/set方法
(4)返回这笔贷款的月支付额getMonthlyPayment()
月支付额 = (贷款额度月利率)/(1-(1/Math.pow(1+月利率,年数12)))
(5)返回这笔贷款的总支付额getTotalPayment()
总支付额度 = 月支付额度年数12
附上如下的测试类。
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
double AIR = input.nextDouble();
int NOY = input.nextInt();
double LA = input.nextDouble();
try {
Loan m = new Loan(AIR, NOY, LA);
System.out.printf("%.3f\n",m.getTotalPayment());
} catch (Exception ex) {
System.out.println(ex);
}
}
}
}
输入格式:
输入有多组数据,一个实数表示年利率,一个整数表示年数,一个实数表示贷款总额。
输出格式:
若任意一项小于或等于零,抛出IllegalArgumentException异常及相应描述(Number of years must be positive或Annual interest rate must be positive或Loan amount must be positive);有多项不符合,以不符合最前项为准;
若均符合要求,按照格式输出总额。
输入样例:
在这里给出一组输入。例如:
1 1 1000
2.0 0 2000
0 0 0
输出样例:
在这里给出相应的输出。例如:
1005.425
java.lang.IllegalArgumentException: Number of years must be positive
java.lang.IllegalArgumentException: Annual interest rate must be positive
import java.util.Scanner;
class Loan{
double annualInterestRate;
int numberOfYears;
double loanAmount;
java.util.Date loanDate;
Loan(){
this.annualInterestRate=2.5;
this.numberOfYears=1;
this.loanAmount=100;
this.loanDate=loanDate;
}
Loan(double annualInterestRate,int numberOfYears,double loanAmount){
this.annualInterestRate=annualInterestRate;
this.numberOfYears=numberOfYears;
this.loanAmount=loanAmount;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public int getNumberOfYears() {
return numberOfYears;
}
public void setNumberOfYears(int numberOfYears) {
this.numberOfYears = numberOfYears;
}
public double getLoanAmount() {
return loanAmount;
}
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
}
public java.util.Date getLoanDate() {
return loanDate;
}
public void setLoanDate(java.util.Date loanDate) {
this.loanDate = loanDate;
}
public double getMonthlyPayment(){
double monthRate=annualInterestRate/1200;
double monthlyPayment=(monthRate)/(1-(1/Math.pow(1+monthRate,numberOfYears*12)));
return monthlyPayment;
}
public double getTotalPayment(){
double totalPayment=getMonthlyPayment()*12*numberOfYears;
return totalPayment;
}
}
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
double AIR = input.nextDouble();
int NOY = input.nextInt();
double LA = input.nextDouble();
try {
Loan m = new Loan(AIR, NOY, LA);
if (AIR<=0)throw new IllegalArgumentException("Annual interest rate must be positive");
else if (NOY<=0)throw new IllegalArgumentException("Number of years must be positive");
else if (LA<=0)throw new IllegalArgumentException("Loan amount must be positive");
System.out.printf("%.3f\n",m.getTotalPayment()*1000);
} catch (Exception ex) {
System.out.println(ex);
}
}
}
}