引发异常的程序,指出“直到结帐后才能进行进一步交易”。客户的未付信用卡金额超过$ 2000或最多45天未付。假定当前日期为01/12/2015
。
创建扩展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
谁能帮我解决这个问题?
最佳答案
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);
}
}