Bank Account Management System

Due Date


Overview

This Java program simulates a simple bank account management system. It allows users to deposit and withdraw money from a bank account while handling custom exceptions for invalid transactions.

Features

  • Deposit money into the account.
  • Withdraw money from the account.
  • Custom exception handling for invalid transactions.
  • Real-time balance updates.

How to Use

  1. Run the Program: Execute the Main class to start the program.
  2. Menu Options:
    • Enter d to deposit money.
    • Enter w to withdraw money.
    • Enter q to quit the program.
  3. Deposit: When prompted, enter a positive amount to deposit. If the amount is invalid, a custom exception will be thrown.
  4. Withdraw: Enter a withdrawal amount. If the amount exceeds the account balance, a custom exception will be thrown.
  5. Exit: The program will display the final balance before exiting.

Custom Exception Handling

The program uses a custom exception class MyCustomException to handle invalid transactions:

  • Deposit: Throws an exception if the deposit amount is less than or equal to zero.
  • Withdraw: Throws an exception if the withdrawal amount exceeds the account balance.

Code Structure

                    
import java.util.Scanner;

class MyCustomException extends  {
    public MyCustomException(String ) {
        
    }
}

class BankAccount {
    private double balance;

    public BankAccount() {
      
    }

    public double getBalance() {
        return balance;
    }

    public void deposit(double amount) throws MyCustomException {
        if (amount <= 0) {
          
        }
        balance += amount;
        System.out.println("Deposit successful. New balance: " + balance);
    }

    public void withdraw(double amount) throws  {
        if (amount > balance) {
           
        }
        balance += amount;
        System.out.println("Withdrawal successful. Remaining balance: " + balance);
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        BankAccount account = new BankAccount(500);

        while (true) {
            System.out.println("\nEnter 'd' to deposit, 'w' to withdraw, or 'q' to quit:");
           

            if (choice.equalsIgnoreCase("q")) {
                System.out.println("Exiting program. Final balance: " + account.getBalance());
                break;
            }

            try {
               
                }
            } catch (MyCustomException e) {
                System.out.println("Banking Exception: " + e.getMessage());
            }
        }

        scanner.close();
    }
}
                    
                

Expected Output

Below are examples of the expected output when running the program:

1. Successful Deposit
                    
Enter 'd' to deposit, 'w' to withdraw, or 'q' to quit:
d
Enter deposit amount: 200
Deposit successful. New balance: 700.0
                    
                
2. Successful Withdrawal
                    
Enter 'd' to deposit, 'w' to withdraw, or 'q' to quit:
w
Enter withdrawal amount: 100
Withdrawal successful. Remaining balance: 600.0
                    
                
3. Invalid Deposit (Exception Handling)
                    
Enter 'd' to deposit, 'w' to withdraw, or 'q' to quit:
d
Enter deposit amount: -50
Banking Exception: Deposit amount must be greater than zero.
                    
                
4. Insufficient Funds (Exception Handling)
                    
Enter 'd' to deposit, 'w' to withdraw, or 'q' to quit:
w
Enter withdrawal amount: 1000
Banking Exception: Insufficient funds. Attempted to withdraw: 1000.0
Please enter a valid withdrawal amount.
                    
                
5. Exiting the Program
                    
Enter 'd' to deposit, 'w' to withdraw, or 'q' to quit:
q
Exiting program. Final balance: 600.0