diff --git a/bank.cpp b/bank.cpp new file mode 100644 index 0000000..b8640cd --- /dev/null +++ b/bank.cpp @@ -0,0 +1,148 @@ +#include "bank.h" +#include + +using namespace std; + +double +Client::earn (const double amount) +{ + underMattress += amount; + return amount; +} + +double +Client::getMattress () +{ + return underMattress; +} + +void +Bank::listClients () +{ + for (auto i : clients) + cout << "Name: " << i.id << " Current: " << i.current << endl; + cout << clients.size () << " accounts." << endl; +} + +void +Bank::countEmployees () +{ + cout << employees.size () << " employees." << endl; +} + +// Open account if enough employee capacity. +bool +Bank::openAccount (Client &x) +{ + if (employees.size () == 0) + return false; + if (employees.size () * CLIENT_EMPLOYEE <= clients.size ()) + return false; + for (auto i : x.accounts) + if (i == this) + return false; + + clientRecord newClient; + newClient.id = x.id; + clients.push_back (newClient); + x.accounts.push_back (this); + + return true; +} + +bool +Bank::hire (Employee &x) +{ + if (x.employer != nullptr) + return false; + employees.push_back (&x); + x.employer = this; + return true; +} + +bool +Bank::fire (Employee &x) +{ + if (x.employer != this) + return false; + for (auto i = employees.begin (); i != employees.end (); i++) + if (*i == &x) + { + employees.erase (i); + x.employer = nullptr; + return true; + } + cout << "Bank doesn't have employee listed, but employee does." << endl; + exit (EXIT_FAILURE); +} + +// Deposit money from under the mattress, return amount. +double +Bank::deposit (Client &x, const double amount) +{ + if (amount == 0) + return 0; + if (x.underMattress < amount) + return 0; + for (auto i = clients.begin(); i != clients.end(); i++) + if (i->id == x.id) + { + x.underMattress -= amount; + i->current += amount; + current += amount; + return amount; + } + cout << "Client doesn't have an account at bank." << endl; + return 0; +} + +double +Bank::getCurrent (const Client &x) +{ + for (auto i : clients) + if (i.id == x.id) + return i.current; + cout << "Client doesn't have an account at bank." << endl; + return 0; +} + +void +Gov::associate (Bank *const x) +{ + banks.push_back (x); +} + +int +main () +{ + Client abdul ("abdul"); + abdul.earn (666); + + Employee dude ("dude"); + Employee gal ("gal"); + + Gov fed; + Bank boa (&fed); + + boa.countEmployees (); + + boa.hire (dude); + boa.countEmployees (); + boa.fire (gal); + boa.countEmployees (); + boa.fire (dude); + boa.countEmployees (); + boa.hire (gal); + boa.hire (dude); + boa.countEmployees (); + + boa.openAccount (abdul); + boa.listClients (); + + cout << abdul.getMattress () << " under mattress." << endl; + boa.deposit (abdul, 500); + cout << boa.getCurrent (abdul) << " in bank account." << endl; + cout << abdul.getMattress () << " under mattress." << endl; + + return 0; +} diff --git a/bank.h b/bank.h new file mode 100644 index 0000000..e50c413 --- /dev/null +++ b/bank.h @@ -0,0 +1,98 @@ +#pragma once + +#include +#include +#include + +class Bank; +class Client; +class Employee; +class Gov; + +class Gov +{ +public: + int factor = 6; // Money multiplier. + int rate = 2; // Interest rate (%). + time_t last_bailout = 0; // Time since last bailout. + void associate (Bank *const x); + bool disassociate (Bank *const x); + double reserveDeposit (const Bank &x, const double amount); + double reserveWithdraw (const Bank &x, const double amount); + bool bailout (const Bank &x, double amount); + +private: + double bankReserves = 0; + std::vector banks; +}; + +class Bank +{ + friend class Gov; + +public: + Gov *centralBank; + Bank (Gov *x) : centralBank (x) { x->associate (this); }; + double deposit (Client &x, const double amount); + double withdraw (Client &x, const double amount); + double getCurrent (const Client &x); + bool hire (Employee &x); + bool fire (Employee &x); + bool openAccount (Client &x); + bool newCredit (const std::string id, const double amount, + const int installments, const int days, + const float interest); // interest is additional interest on + // top of Gov rate. + double payInstallment (const int index); + void countEmployees (); + void listClients (); + +private: + const int CLIENT_EMPLOYEE = 2; // Max number of clients to employees. + double current = 0; // Total current accts. of clients. + double creditable = 0; // Amount able to be loaned to clients. + double credited = 0; // Amount loaned. + struct creditRecord + { + time_t start; + long interval; // Interval of payments in number of seconds. + double amount; + int installments; + float interest; + }; + struct clientRecord + { + std::string id; + double current = 0; + std::vector loans; + }; + std::vector clients; + std::vector employees; +}; + +class Employee +{ + friend class Bank; + +public: + std::string name; + Employee (std::string x) : name (x){}; + +private: + Bank *employer = nullptr; +}; + +class Client +{ + friend class Bank; + +public: + std::string id; + Client (std::string x) : id (x), underMattress (0){}; + double earn (const double amount); // Earn money. + double getMattress (); + +private: + double underMattress; + std::vector accounts; +};