2016-08-26 28 views
1

程序引發異常,聲明「在清算帳單之前不能進行其他交易」。當客戶的未付信用卡金額超過2000美元或未付時間超過45天時。假設當前日期是01/12/2015在java中OverLimit異常處理

  1. 創建一個自定義異常類OverLimitException,它延伸Exception
  2. 添加一個構造函數,該對象使用Throwable對象,使用super()調用超類構造函數,並按問題描述中所述輸出輸出。

我創建了兩個班一主,另一個帳戶

Account.java

import java.text.*; 
import java.util.*; 
import java.util.concurrent.TimeUnit; 

public class Account { 

String accountNumber; 
String accountName; 
Double dueAmount; 

public Account(String accountNumber, String accountName,Double dueAmount) throws ParseException { 
    this.accountNumber = accountNumber; 
    this.accountName = accountName; 
    this.dueAmount = dueAmount; 
} 

public Account() { 
} 

public Boolean validate(String dueDate,Double unpaid,Double amount){ 
    DateFormat sf = new SimpleDateFormat("dd/MM/yyyy"); 
    sf.setLenient(false); 
    try{ 
     Date d = sf.parse(dueDate); 
     Date d1 = sf.parse("01/12/2015"); 
     // long curDate = new Date().getTime(); 
     long diff =d1.getTime() - d.getTime(); 
     long daysDiff = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS); 
     if(daysDiff > 45 || unpaid > 2000){ 
      throw new OverLimitException("Further Transactions Not Possible until clearance of bill."); 
     } 
    }catch(Exception e){ 
     return false; 
    } 
    return true; 
} 

public void display() {  
    System.out.println("Transaction successsfully completed."); 
    System.out.println("Account Number : "+this.accountNumber); 
    System.out.println("Account Name : "+this.accountName); 
    System.out.println("Unpaid Amount : "+this.dueAmount); 
} 
} 

但我得到一個錯誤,說明

error: cannot find symbol 
throw new OverLimitException("Further Transactions Not Possible until clearance of bill."); 
^ 
symbol: class OverLimitException 

任何一個可以請幫我解決這個問題?

+0

並提示:你不想返回** B ** oolean,但** b ** oolean。 – GhostCat

回答

1

的valiade函數拋出OverLimitException,你必須把它定義

public Boolean validate(String dueDate,Double unpaid,Double amount) 
     throws OverLimitException{ 
     ... 
     if(daysDiff > 45 || unpaid > 2000){ 
      throw new OverLimitException("Further Transactions Not Possible until clearance of bill."); 
    } 
    ... 

} 

和實施OverLimitException;這裏是一個example

 public class OverLimitException extends Exception { 
     public OverLimitException(String message) { 
      super(message); 
     } 
    } 
3

OverLimitException不是Java自帶的異常。

與您創建的其他類一樣,您也必須編寫該類;如:

public class OverLimitException extends RuntimeException { 

並提供了一個構造函數,它以消息字符串爲例。