Overview
TheAccount class represents a single accounting account in the system. It maintains a balance, tracks entries, and supports optimistic locking for concurrent updates. Accounts are classified by type (ASSET, LIABILITY, EXPENSE, REVENUE, OFF_BALANCE) and accumulate entries through transactions.
Package: com.softwarearchetypes.accounting
Access: Package-private class (accessed through AccountingFacade)
Constructors
Primary Constructor
Unique identifier for the account (must not be null)
Account classification: ASSET, LIABILITY, EXPENSE, REVENUE, or OFF_BALANCE
Human-readable account name
Initial balance (defaults to zero PLN if null)
Version for optimistic locking
Simplified Constructors
Properties
id
Returns the unique account identifier.The account’s unique identifier
name
Returns the account name as a string.Human-readable account name
type
Returns the account type classification.Account classification (ASSET, LIABILITY, EXPENSE, REVENUE, OFF_BALANCE)
balance
Returns the current account balance.Current balance reflecting all entries
version
Returns the version for optimistic locking.Current version number for concurrency control
entries
Returns a copy of the account’s entries.Copy of new entries added to this account (not all historical entries when loaded from database)
AccountView through AccountingFacade.
Methods
addEntry
Adds a single entry to the account and updates the balance.The entry to add (AccountCredited or AccountDebited)
- Updates the account balance
- Records a pending domain event (CreditEntryRegistered or DebitEntryRegistered)
- Adds entry to the newEntries collection
addEntries
Adds multiple entries to the account and updates the balance.List of entries to add
- Updates the account balance for each entry
- Records pending domain events for each entry
- Adds all entries to the newEntries collection
Event Management
getPendingEvents
Retrieves domain events pending publication.Immutable copy of pending events (CreditEntryRegistered, DebitEntryRegistered)
clearPendingEvents
Clears all pending events after publication.AccountingFacade after successfully publishing events.
Account Types
TheAccountType enum defines the classification and behavior of accounts:
ASSET
Represents owned economic resources with future value.true - Must balance in transactionsLIABILITY
Represents obligations or debts owed by the entity.true - Must balance in transactionsREVENUE
Represents inflows from sales, services, or income-generating activities.true - Must balance in transactionsEXPENSE
Represents outflows for operations, costs, or losses.true - Must balance in transactionsOFF_BALANCE
Represents tracking accounts that don’t affect financial statements.false - Does not require balancing in transactionsRelated Types
AccountId
AccountName
Value object wrapping the account name string.AccountView
Read model for querying account data:AccountingFacade.findAccount() to retrieve complete account information including all historical entries.
Example Usage
Creating an Account
Querying Account Details
Concurrency and Locking
Accounts use optimistic locking through theVersion field. When multiple transactions attempt to update the same account concurrently:
- Each transaction reads the account with its current version
- The transaction adds entries and executes
- When saving, the repository checks if the version has changed
- If the version changed, the transaction fails and must retry
Related Classes
- AccountingFacade - Main interface for account operations
- Transaction - Updates accounts through entries
- Entry - Individual debit/credit entries
