FinanceSim 0.1.0
Financial Simulation Library
Loading...
Searching...
No Matches
savings_account.cpp
Go to the documentation of this file.
2#include <cmath>
3
4namespace financesim {
5
6SavingsAccount::SavingsAccount(std::string id, std::string name,
7 double apy,
8 double initial_balance,
9 std::string routing_tag,
10 Schedule schedule)
11 : AccountBase(std::move(id), std::move(name),
12 std::move(routing_tag), initial_balance,
13 schedule)
14 , apy_(apy)
15 , last_interest_time_(0.0) {
16}
17
20 // Interest is calculated event-driven, no scheduled updates needed
21}
22
24 // Savings account is event-driven, but update can be called
25 // to accrue interest up to the current time
27}
28
30 // Note: We can't accrue final interest here since we don't know
31 // the final simulation time. The simulation should call update()
32 // with the final time before finalizing.
34}
35
40
42 // Accrue interest up to this event's time before depositing
43 accrue_interest_up_to(event.timestamp());
45}
46
48 // Accrue interest up to this event's time before withdrawing
49 accrue_interest_up_to(event.timestamp());
51}
52
54 if (balance() <= 0 || time <= last_interest_time_) return;
55
56 // Monthly interest rate (APY compounded monthly)
57 // Monthly rate = (1 + APY)^(1/12) - 1
58 constexpr double days_per_month = 30.0;
59 double monthly_rate = std::pow(1.0 + apy_, 1.0 / 12.0) - 1.0;
60
61 // Accrue interest for each complete month
63 double interest = balance() * monthly_rate;
65 if (interest > 0) {
67 }
68 }
69}
70
71} // namespace financesim
virtual void on_income(const IncomeEvent &event)
Called when an income event is routed to this account.
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
void emit(SimTime time, Args &&... args)
double balance() const
Get current balance.
virtual void on_expense(const ExpenseEvent &event)
Called when an expense event is routed to this account.
void initialize(EventBus &bus) override
Event emitted when an expense occurs.
Definition event.hpp:62
Event emitted when income is received.
Definition event.hpp:39
void reset() override
Reset model to initial state (for replay)
void initialize(EventBus &bus) override
void on_income(const IncomeEvent &event) override
Called when an income event is routed to this account.
void accrue_interest_up_to(SimTime time)
void on_expense(const ExpenseEvent &event) override
Called when an expense event is routed to this account.
SavingsAccount(std::string id, std::string name, double apy, double initial_balance=0.0, std::string routing_tag="savings", Schedule schedule=make_savings_schedule())
void update(SimTime time) override
double SimTime
Represents a point in simulation time (continuous, in days)
Definition time.hpp:6