import java.util.
Scanner;
// Custom exception for insufficient funds
class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
// Custom exception for invalid account number
class InvalidAccountNumberException extends Exception {
public InvalidAccountNumberException(String message) {
super(message);
public class BankAccount {
private String accountNumber;
private double balance;
// Constructor to initialize account with account number and balance
public BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
// Method to withdraw money from the account
public void withdraw(double amount) throws InsufficientFundsException {
if (amount > balance) {
throw new InsufficientFundsException("Insufficient funds! Your balance is " + balance);
}
balance -= amount;
System.out.println("Withdrawal successful! New balance: " + balance);
// Method to get the account balance
public double getBalance() {
return balance;
// Method to get account number
public String getAccountNumber() {
return accountNumber;
class BankApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Predefined bank accounts
BankAccount account1 = new BankAccount("ACC123", 1000.00);
BankAccount account2 = new BankAccount("ACC456", 500.00);
try {
System.out.println("Welcome to the bank!");
// Get account number input
System.out.print("Enter your account number: ");
String enteredAccountNumber = scanner.nextLine();
// Validate account number
BankAccount account = null;
if (enteredAccountNumber.equals(account1.getAccountNumber())) {
account = account1;
} else if (enteredAccountNumber.equals(account2.getAccountNumber())) {
account = account2;
if (account == null) {
throw new InvalidAccountNumberException("Invalid account number! Please check and try
again.");
// Get withdrawal amount input
System.out.print("Enter amount to withdraw: ");
double withdrawalAmount = scanner.nextDouble();
// Attempt withdrawal
account.withdraw(withdrawalAmount);
} catch (InvalidAccountNumberException e) {
System.out.println("Error: " + e.getMessage());
} catch (InsufficientFundsException e) {
System.out.println("Error: " + e.getMessage());
} catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.getMessage());
} finally {
scanner.close();
System.out.println("Thank you for using our banking system.");
} }}
Explanation:
1. Custom Exceptions:
o InsufficientFundsException: This exception is thrown if the withdrawal amount is
greater than the account balance. It takes a message as input and passes it to the
parent Exception class.
o InvalidAccountNumberException: This exception is thrown if the user enters an
invalid account number. It also takes a message to be displayed.
2. BankAccount Class:
o This class represents a bank account with methods to withdraw money (withdraw())
and to get account details like balance and account number.
o The withdraw() method throws an InsufficientFundsException if the balance is lower
than the withdrawal amount.
3. BankApp Class:
o This is the main application that simulates a user interacting with the bank.
o First, the program asks the user for an account number and checks if it's valid.
o If the account number is invalid, the program throws an
InvalidAccountNumberException.
o Then, it asks the user for the withdrawal amount. If the amount is greater than the
balance, it throws an InsufficientFundsException.
o The program uses a try-catch block to handle exceptions and provide feedback to the
user.
o The finally block ensures that the Scanner resource is closed, preventing resource
leaks.
Example Execution:
Scenario 1: Valid Account and Sufficient Funds
Welcome to the bank!
Enter your account number: ACC123
Enter amount to withdraw: 200
Withdrawal successful! New balance: 800.0
Thank you for using our banking system.
Scenario 2: Invalid Account Number
Welcome to the bank!
Enter your account number: ACC999
Error: Invalid account number! Please check and try again.
Thank you for using our banking system.
Scenario 3: Insufficient Funds
Welcome to the bank!
Enter your account number: ACC123
Enter amount to withdraw: 1500
Error: Insufficient funds! Your balance is 1000.0
Thank you for using our banking system.