joejoe joejoe - 3 months ago 11
Java Question

OverLimit Exception handling in java

Program to raise an exception stating "Further Transactions Not Possible until clearance of bill." when a customer's unpaid credit card amount cross $2000 or is unpaid upto 45 days. Assume that the current date is

01/12/2015
.


  1. Create a custom exception class
    OverLimitException
    which extends
    Exception
    .

  2. Add a constructor which takes a
    Throwable
    object, calls the super class constructor using
    super()
    and prints the output as described in the problem statement.



I created two classes one main and the other account

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);
}
}


But am getting an error stating

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


Can any one please help me solve this problem?

Answer

The valiade function has to throw OverLimitException and you have to define it

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.");
    }
    ...

}

and implement the OverLimitException ; here is an example

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