-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBank.cpp
210 lines (183 loc) · 4 KB
/
Bank.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#define _CRT_SECURE_NO_WARNINGS
#include "Bank.h"
#include <string.h>
#include <iostream>
using namespace std;
//C'tor
Bank::Bank()
{
m_name = NULL;
m_account = NULL;
m_bankCode = 0;
m_numbeOfAccounts= 0 ;
m_totalBalance = 0;
}
//C'tor
Bank::Bank(const char* name, int code) : m_name(NULL)
{
SetBankName(name);
SetCode(code);
}
//D'tor
Bank::~Bank()
{
for (int i = 0; i < m_numbeOfAccounts; i++)
{
delete m_account[i];
}
delete[] m_account;
delete m_name;
}
//Set name of the bank
void Bank::SetBankName(const char* name)
{
if (this->m_name != nullptr)
{
delete[] m_name;
}
m_name = new char[strlen(name) + 1];
strcpy(m_name, name);
}
//Set accounts
void Bank::SetAccount(Account** account, int numbeOfAccounts)
{
for (int i = 0; i < m_numbeOfAccounts; i++)
{
delete m_account[i];
}
delete[] m_account;
m_account = account;
m_numbeOfAccounts = numbeOfAccounts;
}
//Set balance
void Bank::SetTotal(double total)
{
m_totalBalance = total;
}
//Set bank code
void Bank::SetCode(int code)
{
m_bankCode = code;
}
//Return bank name
const char* Bank::GetBankName() const
{
return m_name;
}
//Return accounts array
Account** Bank::GetAccounts() const
{
return m_account;
}
//Return numbers of accounts
int Bank::GetNumberOfAccounts() const
{
return m_numbeOfAccounts;
}
//Return balance
double Bank::GetTotal() const
{
return m_totalBalance;
}
//Return bank's code
int Bank::GetCode() const
{
return m_bankCode;
}
//Add account
void Bank::AddAccount(const Account& account)
{
bool res = false;
for (int i = 0; i < m_numbeOfAccounts; i++)
{
if (m_account[i]->GetAccountNumber() == account.GetAccountNumber())
{
res = true;
}
}
if (res == false)
{
m_totalBalance += account.GetBalance();
Account** NewArr_Account = new Account * [m_numbeOfAccounts + 1];
for (int k = 0; k < m_numbeOfAccounts; k++)
{
//NewArr_Account[k] = m_account[k];
NewArr_Account[k] = new Account(*m_account[k]);
}
NewArr_Account[m_numbeOfAccounts] = new Account(account);
m_numbeOfAccounts++;
delete[] m_account;
m_account = NewArr_Account;
}
}
//Add account
void Bank::AddAccount(const Person& per, double amount)
{
Account new_account(per, amount);
AddAccount(new_account);
}
//Add a person from the account
void Bank::AddPerson(const Person& newPerson, const Account& account, double amount)
{
for (int i = 0; i < m_numbeOfAccounts ; i++)
{
if (m_account[i]->GetAccountNumber() == account.GetAccountNumber())
{
m_account[i]->AddPerson(newPerson, amount);
}
}
}
//Deletes an account
void Bank::DeleteAccount(const Account& account)
{
m_totalBalance -= account.GetBalance();
for (int i = 0; i < m_numbeOfAccounts ; i++)
{
if (m_account[i]->GetAccountNumber() == account.GetAccountNumber())
{
if (m_numbeOfAccounts == 1 )
{
for (int i = 0; i < m_numbeOfAccounts; i++)
{
delete m_account[i];
}
delete[] m_account;
}
else {
int counter = 0;
Account** NewArr_account = new Account * [m_numbeOfAccounts - 1];
for (int k = 0; k < (m_numbeOfAccounts - 1); k++)
{
if (counter == i)
{
continue;
}
else
{
NewArr_account[k] = new Account(*m_account[k]);
counter++;
}
}
m_numbeOfAccounts--;
delete[] m_account;
m_account = NewArr_account;
}
}
}
}
//Deletes a person from the account
void Bank::DeletePerson(const Person& p)
{
for (int i = 0; i < m_numbeOfAccounts; i++)
{
for (int k = 0; k < m_account[i]->GetTotalPersons(); k++)
{
m_account[i]->DeletePerson(p);
if (m_account[i]->GetTotalPersons() == 0)
{
DeleteAccount(*m_account[i]);
break;
}
}
}
}