-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaccount.cpp
More file actions
81 lines (70 loc) · 1.75 KB
/
account.cpp
File metadata and controls
81 lines (70 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "account.h"
#include <QDebug>
int Account::_id = 0;
int Account::nextId() {
return _id++;
}
Account::Account(Domain *domain)
{
_domain = domain;
id = nextId();
balance = 0;
owed_to_bank = 0;
}
double Account::getBalance()
{
return balance;
}
double Account::getAmountOwed()
{
return owed_to_bank;
}
// To be overridden by classes that could be government supported (i.e. Firm)
bool Account::isGovernmentSupported()
{
return false;
}
// Use transferSafely() in preference to credit() as credit() doesn't upate our
// balance. recipient will be nullptr if trying to transfer to a non-existent
// startup.
bool Account::transferSafely(Account *recipient, double amount, Account *creditor)
{
if (amount > balance || recipient == nullptr)
{
// TODO: This needs to go into a log somewhere
qDebug() << "Account::transferSafely(): done (insufficient funds or no recipient)";
// Q_ASSERT(false);
return false;
}
else
{
// qDebug() << "crediting" << amount;
recipient->credit(amount, creditor);
balance -= amount;
return true;
}
}
void Account::credit(double amount, Account*, bool)
{
balance += amount;
}
void Account::loan(double amount, double rate, Account *creditor)
{
balance += amount;
if (creditor->isBank())
{
// TODO: Note that if interest rate changes on subsequent loans, the
// new rate will replace the old rate for the whole amount. This is not
// very realistic and should be changed eventually.
owed_to_bank += amount;
interest_rate = double(rate);
}
else
{
qCritical() << "Only bank loans allowed at present";
}
}
int Account::getId() const
{
return id;
}