2016-08-26 35 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 { 

并提供了一个构造函数,它以消息字符串为例。