Back to portfolio

Najae Hall CSC223 Java

Java Bank Account Simulator

A console account program with deposits, withdrawals, balance checks, and custom exceptions.

Download source ZIP

Technical notes

  • BankAccount holds the balance privately; deposit and withdraw methods control changes to it.
  • Three custom exception classes distinguish invalid deposits, invalid withdrawal amounts, and insufficient funds.
  • The console menu catches those exceptions and shows a message. The balance is kept in memory and resets when the program restarts.

Compile and run

With JDK 17 or newer installed, open a terminal in the extracted project folder.

javac -d out src/Module1/*.java
java -cp out Module1.TesterExceptionsBankAccount

Enter numeric menu choices and transaction amounts. The menu starts with a sample balance of 250.0. Select 4 to exit. Account state is held in memory.

Source code

src/Module1/BankAccount.java
/**
 * 
 */
package Module1;

public class BankAccount {
    private String accountNumber;
    private double balance;

    public BankAccount(String accountNumber, double balance) {
        this.accountNumber = accountNumber;
        this.balance = balance;
    }

    public void deposit(double amount) throws InvalidDepositException {
        if (amount <= 0) {
            throw new InvalidDepositException("Deposit amount must be greater than zero.");
        }
        balance += amount;
    }

    public void withdraw(double amount)
            throws InvalidAmountException, InsufficientFundsException {

        if (amount <= 0) {
            throw new InvalidAmountException("Withdrawal amount must be greater than zero.");
        }

        if (amount > balance) {
            throw new InsufficientFundsException(
                "Insufficient funds. Current balance: " + balance
            );
        }

        balance -= amount;
    }

    public double getBalance() {
        return balance;
    }
}
src/Module1/InvalidAmountException.java
/**
 * 
 */
package Module1;

public class InvalidAmountException extends Exception {
    public InvalidAmountException(String message) {
        super(message);
    }
}
src/Module1/InvalidDepositException.java
/*
 * 
 * 
 */
package Module1;

public class InvalidDepositException extends Exception {
    public InvalidDepositException(String message) {
        super(message);
    }
}
src/Module1/InsufficientFundsException.java
/**
 * 
 */
package Module1;

public class InsufficientFundsException extends Exception {
    public InsufficientFundsException(String message) {
        super(message);
    }
}
src/Module1/TesterExceptionsBankAccount.java
/**
 * 
 */
package Module1;

import java.util.Scanner;

public class TesterExceptionsBankAccount {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        BankAccount account = new BankAccount("12345", 250.0);

        boolean running = true;

        while (running) {
            System.out.println("\n--- BANK MENU ---");
            System.out.println("1. Deposit");
            System.out.println("2. Withdraw");
            System.out.println("3. Check Balance");
            System.out.println("4. Exit");
            System.out.print("Choose an option: ");

            int choice = keyboard.nextInt();

            try {
                switch (choice) {
                    case 1:
                        System.out.print("Enter deposit amount: ");
                        double depositAmount = keyboard.nextDouble();
                        account.deposit(depositAmount);
                        System.out.println("Transaction completed.");
                        break;

                    case 2:
                        System.out.print("Enter withdrawal amount: ");
                        double withdrawAmount = keyboard.nextDouble();
                        account.withdraw(withdrawAmount);
                        System.out.println("Transaction completed.");
                        break;

                    case 3:
                        System.out.println("Current balance: " + account.getBalance());
                        break;

                    case 4:
                        running = false;
                        System.out.println("Goodbye!");
                        break;

                    default:
                        System.out.println("Invalid menu option.");
                }
            } catch (InvalidDepositException |
                     InvalidAmountException |
                     InsufficientFundsException e) {
                System.out.println("Error: " + e.getMessage());
            }
        }

        keyboard.close();
    }
}