Skip to content

SwiftyJourney/bank-account-case-study

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Bank Account — Case Study

A Swift Package implementation of a thread-safe bank account system that supports deposits, withdrawals, and transfers between accounts. This project demonstrates modern Swift concurrency patterns using actors to ensure data integrity in concurrent environments.

Features

  • Deposits: Add funds to an account with validation
  • Withdrawals: Remove funds with balance and fraud checks
  • Transfers: Transfer funds between different accounts
  • Thread Safety: Built using Swift actors to prevent data races
  • Error Handling: Comprehensive error handling for various edge cases
  • Fraud Detection: Built-in fraud alert system for large transactions

Requirements

  • Swift 6.2 or later
  • Xcode 15.0 or later (for development)

Installation

Swift Package Manager

Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/yourusername/bank-account-case-study.git", from: "1.0.0")
]

Or add it through Xcode:

  1. File → Add Package Dependencies
  2. Enter the repository URL
  3. Select the version you want to use

Usage

Creating a Bank Account

import BankSystemCore

let account = BankAccount(
    owner: "John Doe",
    accountNumber: "123456789",
    balance: 1000.0
)

Making a Deposit

try await account.deposit(500.0)

Making a Withdrawal

try await account.withdraw(200.0)

Transferring Funds

let sender = BankAccount(owner: "Alice", accountNumber: "111", balance: 1000.0)
let receiver = BankAccount(owner: "Bob", accountNumber: "222", balance: 500.0)

try await sender.transfer(to: receiver, amount: 300.0)

Error Handling

The BankAccount class defines several error types:

  • insufficientFunds: Attempted withdrawal or transfer exceeds available balance
  • invalidAmount: Amount is zero or negative
  • sameAccountTransfer: Attempted to transfer to the same account
  • fraudAlert: Transaction amount exceeds the fraud threshold (default: $5,000)
do {
    try await account.withdraw(10000.0)
} catch BankAccount.Error.insufficientFunds {
    print("Not enough funds")
} catch BankAccount.Error.fraudAlert {
    print("Transaction flagged for fraud review")
} catch {
    print("An error occurred: \(error)")
}

Testing

The project includes comprehensive test coverage using Swift Testing framework. Run the tests using:

swift test

Or in Xcode:

  • Press Cmd + U to run all tests
  • Or use the Test Navigator to run specific tests

Test Coverage

The test suite covers:

  • Account initialization
  • Successful deposits and withdrawals
  • Invalid amount validation
  • Insufficient funds scenarios
  • Fraud detection
  • Successful transfers
  • Same account transfer prevention

Architecture

Swift Actors

The BankAccount is implemented as a Swift actor, which provides:

  • Thread Safety: Automatic synchronization prevents data races
  • Isolation: State is isolated and can only be accessed through async methods
  • Concurrency: Safe concurrent access from multiple threads

Design Decisions

  • Actor-based: Ensures thread-safe operations without manual locking
  • Async/Await: Modern Swift concurrency for safe asynchronous operations
  • Error Types: Custom error enum for clear error handling
  • Fraud Threshold: Configurable static property for fraud detection limits

Project Structure

BankSystemCore/
├── Sources/
│   └── BankSystemCore/
│       └── BankAccount.swift      # Main bank account implementation
├── Tests/
│   └── BankSystemCoreTests/
│       └── BankAccountTests.swift # Test suite
└── Package.swift                   # Swift Package configuration

License

MIT © 2025 Juan Francisco Dorado Torres

About

A Swift Package implementation of a thread-safe bank account system that supports deposits, withdrawals, and transfers between accounts. This project demonstrates modern Swift concurrency patterns using actors to ensure data integrity in concurrent environments.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages