2011-11-26 131 views
0

我對Java很新,我遇到了一些讓我的代碼捕獲並顯示異常的問題。如果貸款的任何值爲零或更少,代碼的想法是拋出異常。以下是我的代碼,儘管loan1和loan3只有零,但沒有任何異常輸出。任何幫助將由六個星期的新手讚賞。謝謝!沒有發現異常?

package loan; 

public class Loan { 

    public static void main(String[] args) { 
     try { 
     Loan loan1 = new Loan(0, 0, 00.00); 
     Loan loan2 = new Loan(5, 10, 500.00); 
     Loan loan3 = new Loan(0, 0, 0.00); 

     } 
     catch (IllegalArgumentException ex) { 
       System.out.println(ex); 
     } 
    } 


    // declare variables for Loan class 
    double annualInterestRate; 
    int numberOfYears; 
    double loanAmount; 
    private java.util.Date loanDate; 

    // Loan class 
    public Loan() { 
    } 
    public Loan(double annualInterestRate, int numberOfYears, double loanAmount) { 
     this.annualInterestRate = annualInterestRate; 
     this.numberOfYears = numberOfYears; 
     this.loanAmount = loanAmount; 
     loanDate = new java.util.Date(); 
    } 


    public double getAnnualInterestRate() throws IllegalArgumentException {    //exception added if value is zero or less, added on each variable 
     if (annualInterestRate >= 0) 
     throw new IllegalArgumentException("Interest rate cannot be zero or   negative."); 
     else 
      return annualInterestRate; 

    } 

    public void setAnnualInterestRate(double annualInterestRate) { 
     this.annualInterestRate = annualInterestRate; 
    } 

    public int getNumberOfYears() throws IllegalArgumentException { 
     if (numberOfYears >= 0) 
      return numberOfYears; 
     else 
      throw new IllegalArgumentException("Number of years cannot be zero"); 
     } 

    public void setNumberOfYears(int numberOfYears) { 
     this.numberOfYears = numberOfYears; 
    } 

    public double getLoanAmount() throws IllegalArgumentException { 
     if (loanAmount >= 0) 
      return loanAmount; 
     else 
      throw new IllegalArgumentException("Loan amount cannot be zero"); 
    } 

    public void setLoanAmount(double loanAmount) { 
     this.loanAmount = loanAmount; 
    } 

    public double getMonthlyPayment() { 
     double monthlyInterestRate = annualInterestRate/1200; 
     double monthlyPayment = loanAmount * monthlyInterestRate/(1 - (Math.pow(1/(1 + monthlyInterestRate), numberOfYears *12))); 
     return monthlyPayment; 
     } 
    public double getTotalPayment() { 
     double totalPayment = getMonthlyPayment() * numberOfYears * 12; 
     return totalPayment; 
    } 

    public java.util.Date getLoanDate() { 
     return loanDate; 
    } 
} 

回答

1

你只在「吸氣劑」的方法拋出一個異常,如getAnnualInterestRate等,但你從來沒有給他們打電話。要麼調用你的一些getter,要麼(更好)拋出構造函數和setter方法的異常(因爲這是你設置值的地方)。

如果你想確實
3

希望你的構造函數拋出一個異常,在某些情況下,你應該是這樣的:

public Loan(double annualInterestRate, int numberOfYears, double loanAmount) { 
    if (annualInterestRate<0) 
     throw new IllegalArgumentException("value for anualInterestRate is too small"); 
    if (numberOfYears<0) 
     throw new IllegalArgumentException("value for numberOfYears is too small"); 
    this.annualInterestRate = annualInterestRate; 
    this.numberOfYears = numberOfYears; 
    this.loanAmount = loanAmount; 
    loanDate = new java.util.Date(); 
}