#include "account.h"
#include "bank.h"
#include "logger.h"
int main()
{
Logger::LogLine("Banking program: Start");
Bank bank;
for (auto i = 0; i < 10; i++) {
auto account = bank.CreateAccount();
account->Deposit(i * 2.0);
account->SetType(AccountType::kCheckingAccount);
}
bank.PayInterest();
Logger::LogLine("\nBanking program: End");
return 0;
}
#ifndef ACCOUNT_TYPE_H_
#define ACCOUNT_TYPE_H_
enum class AccountType {
kInvalid,
kCheckingAccount,
kSavingsAccount,
};
#endif // ACCOUNT_TYPE_H_
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include "account_type.h"
class Account {
private:
double balance_;
AccountType type_;
public:
Account() noexcept;
double GetBalance() const noexcept;
AccountType GetType() const noexcept;
void SetType(AccountType type) noexcept;
void Deposit(double amount);
double Withdraw(double amount);
double CalculateInterest() const;
};
#endif // ACCOUNT_H_
#include "account.h"
#include "logger.h"
Account::Account() noexcept : balance_{0.0}, type_{AccountType::kInvalid} {}
double Account::GetBalance() const noexcept { return balance_; }
AccountType Account::GetType() const noexcept { return type_; }
void Account::SetType(const AccountType type) noexcept { type_ = type; }
void Account::Deposit(const double amount)
{
balance_ += amount;
Logger::Log("\nDeposit, balance: ");
Logger::LogLine(balance_);
}
double Account::Withdraw(const double amount)
{
balance_ -= amount;
Logger::Log("\nWithdraw, balance: ");
Logger::LogLine(balance_);
return amount;
}
double Account::CalculateInterest() const
{
auto interest_rate = 0.0;
switch (type_) {
case AccountType::kCheckingAccount: {
interest_rate = 0.001;
break;
}
}
const auto interest = balance_ * interest_rate;
Logger::Log("\nCalculate interest, amount: ");
Logger::LogLine(interest);
return interest;
}
#ifndef BANK_H_
#define BANK_H_
#include <list>
#include <memory>
class Account;
class Bank {
private:
std::list<std::shared_ptr<Account>> accounts_;
public:
std::shared_ptr<Account> CreateAccount();
void PayInterest();
};
#endif // BANK_H_
#include "bank.h"
#include <ctime>
#include "account.h"
#include "logger.h"
std::shared_ptr<Account> Bank::CreateAccount()
{
auto account = std::make_shared<Account>();
accounts_.push_back(account);
Logger::Log("\nCreate account, number of accounts: ");
Logger::LogLine(accounts_.size());
return account;
}
void Bank::PayInterest()
{
for (auto& account : accounts_) {
const auto interest = account->CalculateInterest();
account->Deposit(interest);
}
Logger::Log("\nInterest paid on: ");
time_t current_time;
time(¤t_time);
auto time_string = ctime(¤t_time);
if (time_string != nullptr) {
Logger::LogLine(time_string);
}
}
#ifndef LOGGER_H_
#define LOGGER_H_
#include <iostream>
class Logger {
public:
template <class T>
static void Log(const T& message)
{
LogIntern(message, "");
}
template <class T>
static void LogLine(const T& message)
{
LogIntern(message, "\n");
}
template <class T>
static void LogIntern(const T& message, const char* additional_text)
{
std::cout << message << additional_text;
}
};
#endif // LOGGER_H_