程序引發異常,聲明「在清算帳單之前不能進行其他交易」。當客戶的未付信用卡金額超過2000美元或未付時間超過45天時。假設當前日期是01/12/2015
。在java中OverLimit異常處理
- 創建一個自定義異常類
OverLimitException
,它延伸Exception
。 - 添加一個構造函數,該對象使用
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
任何一個可以請幫我解決這個問題?
並提示:你不想返回** B ** oolean,但** b ** oolean。 – GhostCat