-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccount.cpp
280 lines (243 loc) · 7.92 KB
/
Account.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#define _CRT_SECURE_NO_WARNINGS
#include "Person.h"
#include "Account.h"
#include "Transaction.h"
using namespace std;
//Default C'tor
Account::Account()
{
m_transactionList = NULL;
m_numberOfTransaction = 0;
m_persons = NULL;
m_totalPersons = 0;
m_accountNumber = 0;
m_balance = 0;
}
//C'tor
Account::Account(Person** persons, int count, double balance) : m_persons(NULL) , m_transactionList(NULL)
{
m_transactionList = nullptr;
m_numberOfTransaction = 0;
SetBalance(balance);
m_accountNumber = 0;
SetPersons(persons, count);
for (int i = 0; i < m_totalPersons; i++)
{
m_accountNumber += persons[i]->GetId();
}
}
//C'tor of one person
Account::Account(const Person& person, double balance) : m_persons(NULL) , m_transactionList(NULL)
{
m_transactionList = nullptr;
m_numberOfTransaction = 0;
m_totalPersons = 1;
m_persons = new Person * [m_totalPersons];
m_persons[0] = new Person(person);
SetAccountNumber(person.GetId());
SetBalance(balance);
}
//Copy C'tor
Account::Account(const Account& other) : m_persons(NULL) , m_transactionList(NULL)
{
this->SetAccountNumber(other.m_accountNumber);
this->SetBalance(other.m_balance);
this->SetTransactions(other.m_transactionList , other.m_numberOfTransaction);
this->SetPersons(other.m_persons ,other.m_totalPersons);
}
//D'tor
Account::~Account()
{
clearPersons();
// clearTransactions();
}
//Defines the array of actions that belong to the account
void Account::SetTransactions(Transaction** newTransaction, int count)
{
clearTransactions();
m_numberOfTransaction = count;
m_transactionList = new Transaction * [count];
for (int k = 0; k < m_numberOfTransaction; k++)
{
// NewArr_persons[k] = m_persons[k];
m_transactionList[k] = new Transaction(*newTransaction[k]);
}
}
//Defines the array of people who belong to the account
void Account::SetPersons(Person** persons, int count)
{
clearPersons();
m_totalPersons = count;
m_persons = new Person * [count];
for (int k = 0; k < m_totalPersons; k++)
{
// NewArr_persons[k] = m_persons[k];
m_persons[k] = new Person(*persons[k]);
}
}
//Set the account number
void Account::SetAccountNumber(int number)
{
m_accountNumber = number;
}
//Set the balance
void Account::SetBalance(double balance)
{
m_balance = balance;
}
//Return the array of actions that belong to the account
Transaction** Account::GetTransactions()
{
return m_transactionList;
}
//Return the number of the Transactions
int Account::GetNumOfTransactions()
{
return m_numberOfTransaction;
}
//Return the array of people who belong to the account
Person** Account::GetPersons() const
{
return m_persons;
}
//Return the number of the person
int Account::GetTotalPersons()const
{
return m_totalPersons;
}
//Return the account number
int Account::GetAccountNumber() const
{
return m_accountNumber;
}
//Return the balance
double Account::GetBalance() const
{
return m_balance;
}
//Add Transaction of Withdraw
void Account::Withdraw(double amount, const char* date)
{
m_balance -= amount;
Transaction* NewTrans = new Transaction(this, this, amount, date);
AddTransaction(*NewTrans);
//delete NewTrans;
}
//Add Transaction of Deposit
void Account::Deposit(double amount, const char* date)
{
m_balance += amount;
Transaction* NewTrans = new Transaction(this, this, amount, date);
AddTransaction(*NewTrans);
//delete NewTrans;
}
//Add new person to the list of persons that related to the account
void Account::AddPerson(const Person& newPerson, double amount)
{
bool res = false;
for (int i = 0; i < m_totalPersons; i++)
{
if (m_persons[i]->GetId() == newPerson.GetId())
{
res = true;
}
}
if (res == false)
{
m_balance += amount;
Person** NewArr_persons = new Person * [m_totalPersons + 1];
for (int k = 0; k < m_totalPersons; k++)
{
// NewArr_persons[k] = m_persons[k];
NewArr_persons[k] = new Person(*m_persons[k]);
}
NewArr_persons[m_totalPersons] = new Person(newPerson);
m_totalPersons++;
delete[] m_persons;
m_persons = NewArr_persons;
m_accountNumber += newPerson.GetId();
}
}
//Delete a person form the list of person that related to the account
void Account::DeletePerson(const Person& oldPerson)
{
for (int i = 0; i < m_totalPersons ; i++)
{
if (m_persons[i]->GetId() == oldPerson.GetId())
{
if (m_totalPersons == 1)
{
clearPersons();
m_totalPersons--;
}
else
{
int counter = 0;
Person** NewArr_persons = new Person * [m_totalPersons - 1];
for (int k = 0; k < (m_totalPersons - 1); k++)
{
if (counter == i)
{
continue;
}
else
{
NewArr_persons[k] = m_persons[k];
//NewArr_persons[k] = new Person(*m_persons[k]);
counter++;
}
}
m_totalPersons--;
delete[] m_persons;
m_persons = NewArr_persons;
}
}
}
}
//Add new Transaction to the Transaction list
void Account::AddTransaction(const Transaction& newTransaction)
{
int newArraySize = newTransaction.GetSource()->GetNumOfTransactions();
Transaction** NewArr_Transaction = new Transaction * [newArraySize + 1];
for (int k = 0; k < newArraySize; k++)
{
NewArr_Transaction[k] = new Transaction(*newTransaction.GetSource()->GetTransactions()[k]);
}
NewArr_Transaction[newArraySize] = new Transaction(newTransaction);
newTransaction.GetSource()->SetTransactions(NewArr_Transaction, newTransaction.GetSource()->GetNumOfTransactions() + 1 );
newTransaction.GetDes()->SetBalance(newTransaction.GetDes()->GetBalance() + newTransaction.GetAmount());
newTransaction.GetSource()->SetBalance(newTransaction.GetSource()->GetBalance() - newTransaction.GetAmount());
if (newTransaction.GetDes()->GetAccountNumber() == newTransaction.GetSource()->GetAccountNumber())
{
return;
}
else
{
int newArraySize2 = newTransaction.GetDes()->GetNumOfTransactions();
Transaction** NewArr_Transaction2 = new Transaction * [newArraySize2 + 1];
for (int k = 0; k < newArraySize2; k++)
{
NewArr_Transaction2[k] = new Transaction(*newTransaction.GetDes()->GetTransactions()[k]);
}
NewArr_Transaction2[newArraySize2] = new Transaction(newTransaction);
newTransaction.GetDes()->SetTransactions(NewArr_Transaction2, newTransaction.GetDes()->GetNumOfTransactions() + 1);
}
}
//Clear transactions list
void Account::clearTransactions()
{
for (int i = 0; i < m_numberOfTransaction; i++)
{
delete(m_transactionList[i]->GetDate());
}
delete[] m_transactionList;
}
//Clear persons list
void Account::clearPersons()
{
for (int i = 0; i < m_totalPersons; i++)
{
(m_persons[i])->~Person();
}
delete[]m_persons;
}