FinanceSim 0.1.0
Financial Simulation Library
Loading...
Searching...
No Matches
account_base.cpp
Go to the documentation of this file.
2
3namespace financesim {
4
5AccountBase::AccountBase(std::string id, std::string name, std::string routing_tag,
6 double initial_balance,
7 Schedule schedule)
8 : id_(std::move(id))
9 , name_(std::move(name))
10 , routing_tag_(std::move(routing_tag))
11 , balance_(initial_balance)
12 , initial_balance_(initial_balance)
13 , schedule_(schedule) {
14}
15
17 bus_ = &bus;
18
19 // Subscribe to income events
21 [this](const IncomeEvent& event) {
22 if (should_handle_event(event.target_account())) {
23 on_income(event);
24 }
25 }
26 );
27
28 // Subscribe to expense events
30 [this](const ExpenseEvent& event) {
31 if (should_handle_event(event.target_account())) {
32 on_expense(event);
33 }
34 }
35 );
36}
37
39 // Base account has no scheduled updates
40 // Derived classes (like SavingsAccount) may override for interest calculations
41}
42
44 // Nothing to finalize
45}
46
48 if (bus_) {
51 }
52 bus_ = nullptr;
53 income_sub_ = 0;
54 expense_sub_ = 0;
56}
57
58bool AccountBase::should_handle_event(const std::string& target_account) const {
59 if (target_account.empty()) {
60 // No explicit target - accept only if we're the default
61 return routing_tag_ == "default";
62 }
63 // Explicit target - accept only if we match
64 return target_account == routing_tag_;
65}
66
67void AccountBase::deposit(SimTime time, double amount, const std::string& reason) {
68 if (amount <= 0) return;
69 balance_ += amount;
70 emit<AccountEvent>(time, id_, balance_, amount, reason);
71}
72
73void AccountBase::withdraw(SimTime time, double amount, const std::string& reason) {
74 if (amount <= 0) return;
75 balance_ -= amount;
76 emit<AccountEvent>(time, id_, balance_, -amount, reason);
77}
78
80 deposit(event.timestamp(), event.amount(), event.category());
81}
82
84 withdraw(event.timestamp(), event.amount(), event.category());
85}
86
87} // namespace financesim
SubscriptionId income_sub_
virtual void on_income(const IncomeEvent &event)
Called when an income event is routed to this account.
SubscriptionId expense_sub_
void reset() override
Reset model to initial state (for replay)
void deposit(SimTime time, double amount, const std::string &reason)
Deposit funds into the account.
void finalize() override
bool should_handle_event(const std::string &target_account) const
void update(SimTime time) override
void emit(SimTime time, Args &&... args)
AccountBase(std::string id, std::string name, std::string routing_tag, double initial_balance=0.0, Schedule schedule=make_account_schedule())
virtual void on_expense(const ExpenseEvent &event)
Called when an expense event is routed to this account.
void withdraw(SimTime time, double amount, const std::string &reason)
Withdraw funds from the account.
void initialize(EventBus &bus) override
SubscriptionId subscribe(std::function< void(const EventType &)> callback)
Definition event_bus.hpp:31
void unsubscribe(SubscriptionId id)
Unsubscribe using subscription ID.
Definition event_bus.hpp:52
Event emitted when an expense occurs.
Definition event.hpp:62
Event emitted when income is received.
Definition event.hpp:39
double SimTime
Represents a point in simulation time (continuous, in days)
Definition time.hpp:6