Real-World Projects

Banking System - Account Management

A comprehensive banking system program that demonstrates account creation, deposits, withdrawals, balance inquiry, and transaction history using Object-Oriented Programming principles.

Problem Description

Create a banking system that allows users to:

  • Create new bank accounts
  • Deposit money
  • Withdraw money
  • Check account balance
  • View transaction history
  • Transfer money between accounts

This program demonstrates real-world application of OOP concepts, data validation, and user interaction.

Code

BankingSystem.java
import java.util.*;

class Account {
    private String accountNumber;
    private String accountHolder;
    private double balance;
    private ArrayList<String> transactionHistory;
    
    public Account(String accountNumber, String accountHolder, double initialDeposit) {
        this.accountNumber = accountNumber;
        this.accountHolder = accountHolder;
        this.balance = initialDeposit;
        this.transactionHistory = new ArrayList<>();
        transactionHistory.add("Account created with initial deposit: $" + initialDeposit);
    }
    
    public String getAccountNumber() {
        return accountNumber;
    }
    
    public String getAccountHolder() {
        return accountHolder;
    }
    
    public double getBalance() {
        return balance;
    }
    
    public void deposit(double amount) {
        if (amount <= 0) {
            System.out.println("Invalid amount. Deposit must be positive.");
            return;
        }
        balance += amount;
        transactionHistory.add("Deposited: $" + amount + " | New Balance: $" + balance);
        System.out.println("Successfully deposited $" + amount);
    }
    
    public boolean withdraw(double amount) {
        if (amount <= 0) {
            System.out.println("Invalid amount. Withdrawal must be positive.");
            return false;
        }
        if (amount > balance) {
            System.out.println("Insufficient funds. Current balance: $" + balance);
            return false;
        }
        balance -= amount;
        transactionHistory.add("Withdrew: $" + amount + " | New Balance: $" + balance);
        System.out.println("Successfully withdrew $" + amount);
        return true;
    }
    
    public void displayAccountInfo() {
        System.out.println("\n===== Account Information =====");
        System.out.println("Account Number: " + accountNumber);
        System.out.println("Account Holder: " + accountHolder);
        System.out.println("Current Balance: $" + balance);
        System.out.println("===============================\n");
    }
    
    public void displayTransactionHistory() {
        System.out.println("\n===== Transaction History =====");
        System.out.println("Account: " + accountNumber + " (" + accountHolder + ")");
        System.out.println("-------------------------------");
        for (String transaction : transactionHistory) {
            System.out.println(transaction);
        }
        System.out.println("===============================\n");
    }
}

class Bank {
    private HashMap<String, Account> accounts;
    private int accountCounter;
    
    public Bank() {
        accounts = new HashMap<>();
        accountCounter = 1000;
    }
    
    public String createAccount(String name, double initialDeposit) {
        if (initialDeposit < 100) {
            System.out.println("Minimum initial deposit is $100");
            return null;
        }
        
        String accountNumber = "ACC" + (++accountCounter);
        Account account = new Account(accountNumber, name, initialDeposit);
        accounts.put(accountNumber, account);
        
        System.out.println("\n✓ Account created successfully!");
        System.out.println("Account Number: " + accountNumber);
        System.out.println("Account Holder: " + name);
        System.out.println("Initial Deposit: $" + initialDeposit + "\n");
        
        return accountNumber;
    }
    
    public Account getAccount(String accountNumber) {
        return accounts.get(accountNumber);
    }
    
    public boolean transferMoney(String fromAccount, String toAccount, double amount) {
        Account sender = accounts.get(fromAccount);
        Account receiver = accounts.get(toAccount);
        
        if (sender == null || receiver == null) {
            System.out.println("Invalid account number(s)");
            return false;
        }
        
        if (sender.withdraw(amount)) {
            receiver.deposit(amount);
            System.out.println("Transfer successful!");
            System.out.println("Transferred $" + amount + " from " + fromAccount + " to " + toAccount);
            return true;
        }
        
        return false;
    }
}

public class BankingSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Bank bank = new Bank();
        
        System.out.println("╔═══════════════════════════════════════╗");
        System.out.println("║   WELCOME TO JAVAPEDIA BANK SYSTEM    ║");
        System.out.println("╚═══════════════════════════════════════╝\n");
        
        boolean running = true;
        
        while (running) {
            System.out.println("\n──────────────────────────────────────");
            System.out.println("         MAIN MENU");
            System.out.println("──────────────────────────────────────");
            System.out.println("1. Create New Account");
            System.out.println("2. Deposit Money");
            System.out.println("3. Withdraw Money");
            System.out.println("4. Check Balance");
            System.out.println("5. View Transaction History");
            System.out.println("6. Transfer Money");
            System.out.println("7. Exit");
            System.out.println("──────────────────────────────────────");
            System.out.print("Enter your choice (1-7): ");
            
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline
            
            switch (choice) {
                case 1:
                    System.out.print("\nEnter account holder name: ");
                    String name = scanner.nextLine();
                    System.out.print("Enter initial deposit (min $100): $");
                    double initialDeposit = scanner.nextDouble();
                    bank.createAccount(name, initialDeposit);
                    break;
                    
                case 2:
                    System.out.print("\nEnter account number: ");
                    String accNum = scanner.nextLine();
                    Account acc = bank.getAccount(accNum);
                    if (acc != null) {
                        System.out.print("Enter amount to deposit: $");
                        double depositAmount = scanner.nextDouble();
                        acc.deposit(depositAmount);
                    } else {
                        System.out.println("Account not found!");
                    }
                    break;
                    
                case 3:
                    System.out.print("\nEnter account number: ");
                    String accNum2 = scanner.nextLine();
                    Account acc2 = bank.getAccount(accNum2);
                    if (acc2 != null) {
                        System.out.print("Enter amount to withdraw: $");
                        double withdrawAmount = scanner.nextDouble();
                        acc2.withdraw(withdrawAmount);
                    } else {
                        System.out.println("Account not found!");
                    }
                    break;
                    
                case 4:
                    System.out.print("\nEnter account number: ");
                    String accNum3 = scanner.nextLine();
                    Account acc3 = bank.getAccount(accNum3);
                    if (acc3 != null) {
                        acc3.displayAccountInfo();
                    } else {
                        System.out.println("Account not found!");
                    }
                    break;
                    
                case 5:
                    System.out.print("\nEnter account number: ");
                    String accNum4 = scanner.nextLine();
                    Account acc4 = bank.getAccount(accNum4);
                    if (acc4 != null) {
                        acc4.displayTransactionHistory();
                    } else {
                        System.out.println("Account not found!");
                    }
                    break;
                    
                case 6:
                    System.out.print("\nEnter sender account number: ");
                    String fromAcc = scanner.nextLine();
                    System.out.print("Enter receiver account number: ");
                    String toAcc = scanner.nextLine();
                    System.out.print("Enter amount to transfer: $");
                    double transferAmount = scanner.nextDouble();
                    bank.transferMoney(fromAcc, toAcc, transferAmount);
                    break;
                    
                case 7:
                    System.out.println("\n╔═══════════════════════════════════════╗");
                    System.out.println("║   Thank you for using Javapedia Bank  ║");
                    System.out.println("╚═══════════════════════════════════════╝\n");
                    running = false;
                    break;
                    
                default:
                    System.out.println("\n✗ Invalid choice! Please enter a number between 1-7");
            }
        }
        
        scanner.close();
    }
}

Sample Output

╔═══════════════════════════════════════╗
║   WELCOME TO JAVAPEDIA BANK SYSTEM    ║
╚═══════════════════════════════════════╝


──────────────────────────────────────
         MAIN MENU
──────────────────────────────────────
1. Create New Account
2. Deposit Money
3. Withdraw Money
4. Check Balance
5. View Transaction History
6. Transfer Money
7. Exit
──────────────────────────────────────
Enter your choice (1-7): 1

Enter account holder name: John Doe
Enter initial deposit (min $100): $500

✓ Account created successfully!
Account Number: ACC1001
Account Holder: John Doe
Initial Deposit: $500.0

──────────────────────────────────────
Enter your choice (1-7): 2

Enter account number: ACC1001
Enter amount to deposit: $250
Successfully deposited $250.0

──────────────────────────────────────
Enter your choice (1-7): 4

Enter account number: ACC1001

===== Account Information =====
Account Number: ACC1001
Account Holder: John Doe
Current Balance: $750.0
===============================

Explanation

Classes and Structure

  1. Account Class:

    • Private Fields: accountNumber, accountHolder, balance, transactionHistory
    • Constructor: Initializes account with number, holder name, and initial deposit
    • Methods:
      • deposit(): Adds money to account with validation
      • withdraw(): Deducts money with balance checking
      • displayAccountInfo(): Shows account details
      • displayTransactionHistory(): Lists all transactions
  2. Bank Class:

    • HashMap: Stores accounts with account number as key
    • accountCounter: Auto-generates unique account numbers
    • Methods:
      • createAccount(): Creates new account with validation
      • getAccount(): Retrieves account by number
      • transferMoney(): Transfers funds between accounts
  3. Main Class (BankingSystem):

    • Provides user interface with menu-driven system
    • Handles user input and calls appropriate methods

Key Concepts Demonstrated

  • Encapsulation: Private fields with getter methods
  • Data Validation: Checks for valid amounts and sufficient funds
  • Collections: Uses HashMap for account storage and ArrayList for transaction history
  • User Interaction: Menu-driven interface with Scanner
  • Error Handling: Validates user input and provides feedback

Real-World Applications

This program demonstrates:

  • Account management systems
  • Transaction tracking
  • Data persistence (in-memory)
  • User authentication concepts
  • Financial calculations

Try Yourself

  1. Interest Calculator: Add a method to calculate and add monthly interest to accounts.
  2. Account Types: Create different account types (Savings, Current) with different interest rates.
  3. Transaction Limits: Add daily withdrawal limits (e.g., max $1000 per day).
  4. PIN System: Implement a 4-digit PIN for account security.
  5. Loan Feature: Add functionality to apply for and manage loans.
  6. File Persistence: Save account data to a file so it persists between program runs.
  7. Multi-Currency: Add support for multiple currencies with exchange rates.
  8. Account Statement: Generate monthly account statements with all transactions.
  9. Minimum Balance: Enforce minimum balance requirements (e.g., $100 minimum).
  10. Fixed Deposits: Add functionality for creating fixed deposits with maturity dates.