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.
- 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
- Swift 6.2 or later
- Xcode 15.0 or later (for development)
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:
- File → Add Package Dependencies
- Enter the repository URL
- Select the version you want to use
import BankSystemCore
let account = BankAccount(
owner: "John Doe",
accountNumber: "123456789",
balance: 1000.0
)try await account.deposit(500.0)try await account.withdraw(200.0)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)The BankAccount class defines several error types:
insufficientFunds: Attempted withdrawal or transfer exceeds available balanceinvalidAmount: Amount is zero or negativesameAccountTransfer: Attempted to transfer to the same accountfraudAlert: 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)")
}The project includes comprehensive test coverage using Swift Testing framework. Run the tests using:
swift testOr in Xcode:
- Press
Cmd + Uto run all tests - Or use the Test Navigator to run specific tests
The test suite covers:
- Account initialization
- Successful deposits and withdrawals
- Invalid amount validation
- Insufficient funds scenarios
- Fraud detection
- Successful transfers
- Same account transfer prevention
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
- 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
BankSystemCore/
├── Sources/
│ └── BankSystemCore/
│ └── BankAccount.swift # Main bank account implementation
├── Tests/
│ └── BankSystemCoreTests/
│ └── BankAccountTests.swift # Test suite
└── Package.swift # Swift Package configuration
MIT © 2025 Juan Francisco Dorado Torres