1. 程式人生 > 其它 >7-1 設計一個能處理異常的Loan類 (20分)

7-1 設計一個能處理異常的Loan類 (20分)

技術標籤:java

實驗7
7-1 設計一個能處理異常的Loan類 (20分)
定義一個貸款類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.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		while(true) {
			double air=in.nextDouble();
			int noy=in.nextInt();
			double la=in.nextDouble();
			if(air<=0) {
				System.out.println("java.lang.IllegalArgumentException: Annual interest rate must be positive");
				continue;
			}
			if(noy<=0) {
				System.out.println("java.lang.IllegalArgumentException: Number of years must be positive");
				continue;
			}
			if(la<=0) {
				System.out.println("java.lang.IllegalArgumentException: Loan amount must be positive");
			}
			System.out.printf("%.3f\n",la*12*noy*(air/1200)/(1-(1/Math.pow(1+air/1200,12*noy))));	
		}
	}
}

答案2:

import java.util.*;
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);
	          }
	      }

	  }
}

class Loan{
	
	double annualInterestRate;
	int numberOfYears;
	double loanAmount;
	Date loanDate;
	
	public Loan() {
		this.annualInterestRate=2.5;
		this.loanAmount=100;
		this.numberOfYears=1;
		this.loanDate=new Date();
	}
	
	public Loan(double air,int noy,double la) {
		this.annualInterestRate=air;
		this.numberOfYears=noy;
		this.loanAmount=la;
	}
	
	public double getMonthlyPayment() {
		return (this.loanAmount*this.annualInterestRate/1200)/(1-(1/Math.pow(1+this.annualInterestRate/1200, this.numberOfYears*12)));	
	}
	
	public double getTotalPayment() {
		return getMonthlyPayment()*this.numberOfYears*12;
	}
}

小結:
注意一下,題目沒有述明什麼是月利率,
月利率等於年利率除以1200;
在這裡賦一下相關圖片:
在這裡插入圖片描述