JaJava · Lesson 8 of 9

Mini Project: Simple Bank

A console banking application that puts together classes, collections, exceptions, and user input.

Java
import java.util.*;

class Account {
    private static int nextId = 1000;
    private final int id;
    private final String owner;
    private double balance;
    private final List<String> transactions = new ArrayList<>();

    Account(String owner, double initial) {
        this.id = nextId++;
        this.owner = owner;
        this.balance = initial;
        transactions.add(String.format("Initial deposit: +$%.2f", initial));
    }

    void deposit(double amount) {
        if (amount <= 0) throw new IllegalArgumentException("Amount must be positive");
        balance += amount;
        transactions.add(String.format("Deposit: +$%.2f (balance: $%.2f)", amount, balance));
    }

    void withdraw(double amount) {
        if (amount <= 0) throw new IllegalArgumentException("Amount must be positive");
        if (amount > balance) throw new IllegalStateException(
            String.format("Insufficient funds: needed $%.2f, have $%.2f", amount, balance));
        balance -= amount;
        transactions.add(String.format("Withdrawal: -$%.2f (balance: $%.2f)", amount, balance));
    }

    void printStatement() {
        System.out.printf("%nAccount #%d — %s%n", id, owner);
        System.out.printf("Balance: $%.2f%n", balance);
        System.out.println("Transactions:");
        transactions.forEach(t -> System.out.println("  " + t));
    }

    int getId() { return id; }
    String getOwner() { return owner; }
    double getBalance() { return balance; }
}

public class Bank {
    private final Map<Integer, Account> accounts = new HashMap<>();
    private final Scanner scanner = new Scanner(System.in);

    void createAccount() {
        System.out.print("Owner name: ");
        String name = scanner.nextLine();
        System.out.print("Initial deposit: $");
        double amount = scanner.nextDouble(); scanner.nextLine();
        Account acc = new Account(name, amount);
        accounts.put(acc.getId(), acc);
        System.out.printf("Account created: #%d%n", acc.getId());
    }

    Account findAccount() {
        System.out.print("Account ID: ");
        int id = scanner.nextInt(); scanner.nextLine();
        Account acc = accounts.get(id);
        if (acc == null) throw new NoSuchElementException("Account not found: " + id);
        return acc;
    }

    public static void main(String[] args) {
        Bank bank = new Bank();
        Scanner sc = new Scanner(System.in);
        System.out.println("=== Simple Bank ===");

        while (true) {
            System.out.println("\n1) New account  2) Deposit  3) Withdraw  4) Statement  5) Quit");
            System.out.print("> ");
            String choice = sc.nextLine().trim();
            try {
                switch (choice) {
                    case "1" -> bank.createAccount();
                    case "2" -> {
                        Account a = bank.findAccount();
                        System.out.print("Amount: $");
                        a.deposit(sc.nextDouble()); sc.nextLine();
                        System.out.println("Deposited.");
                    }
                    case "3" -> {
                        Account a = bank.findAccount();
                        System.out.print("Amount: $");
                        a.withdraw(sc.nextDouble()); sc.nextLine();
                        System.out.println("Withdrawn.");
                    }
                    case "4" -> bank.findAccount().printStatement();
                    case "5" -> { System.out.println("Goodbye!"); return; }
                    default -> System.out.println("Invalid choice");
                }
            } catch (Exception e) {
                System.err.println("Error: " + e.getMessage());
            }
        }
    }
}
Bash
javac Bank.java
java Bank