user
Implement a Java program that simulates a bank system with basic operations like deposit, withdrawal, transfer, and balance inquiry. The program should also include exception handling mechanisms for cases such as insufficient funds or invalid account details. PLAINFORMAT
assistant
// Create a custom exception for insufficient funds
class InsufficientFundsException extends Exception {
    public InsufficientFundsException(String message) {
        super(message);
    }
}

// Create a custom exception for invalid account details
class InvalidAccountDetailException extends Exception {
    public InvalidAccountDetailException(String message) {
        super(message);
    }
}

// Define the Bank Account class
class BankAccount {
    private String accountId;
    private double balance;

    // Constructor to initialize bank account with ID and initial balance
    public BankAccount(String accountId, double initialBalance) {
        this.accountId = accountId;
        this.balance = initialBalance;
    }

    // Method to deposit amount in the account
    public void deposit(double amount) {
        balance += amount;
    }

    // Method to withdraw amount from the account
    public void withdraw(double amount) throws InsufficientFundsException {
        if (balance < amount) {
            throw new InsufficientFundsException("Insufficient Funds!");
        } else {
            balance -= amount;
        }
    }

    // Method to get current balance
    public double getBalance() {
        return balance;
    }

    // Method to get account id
    public String getAccountId() {
        return accountId;
    }
}

// Define the Bank System class
public class BankSystem {

    // Main method to simulate banking operations
    public static void main(String[] args) {
        try {
            // Creating two accounts
            BankAccount account1 = new BankAccount("123456", 5000.00);
            BankAccount account2 = new BankAccount("789101", 3000.00);

            // Displaying initial balances
            System.out.println("Initial Balance of " + account1.getAccountId() + ": " + account1.getBalance());
            System.out.println("Initial Balance of " + account2.getAccountId() + ": " + account2.getBalance());

            // Performing some transactions
            account1.deposit(2000.00);
            account1.withdraw(1000.00);
            account2.deposit(1500.00);
            account2.withdraw(4000.00); // This will throw an exception

            // Displaying final balances
            System.out.println("Final Balance of " + account1.getAccountId() + ": " + account1.getBalance());
            System.out.println("Final Balance of " + account2.getAccountId() + ": " + account2.getBalance());

        } catch (InsufficientFundsException e) {
            System.out.println(e.getMessage());
        }
    }
}