-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsClient.cpp
271 lines (233 loc) · 5.53 KB
/
clsClient.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
#include "clsClient.h"
string clsClient::_ConvertClientObjectToLine(clsClient Client,string Line, string Delimeter = "#//#")
{
string ClientLine = "";
ClientLine += Client.firstname+ Delimeter;
ClientLine += Client.lastname + Delimeter;
ClientLine += Client.email+ Delimeter;
ClientLine += Client.phone+ Delimeter;
ClientLine += Client.account_number+ Delimeter;
ClientLine += Client.pin_code + Delimeter;
ClientLine += to_string(Client.balance);
return ClientLine;
}
clsClient clsClient::_ConvertClientLineToClientObject(string Line,string Delimeter = "#//#")
{
vector<string> vData = clsString::Split(Line, Delimeter);
return clsClient(UpdateMode, vData[0], vData[1], vData[2], vData[3], vData[4], vData[5], stof(vData[6]));
}
clsClient clsClient::_GetEmptyClientObject()
{
return clsClient(EmptyMode, "", "", "", "", "", "", 0);
}
void clsClient::_SaveClienstDataToFile(vector<clsClient> &vClients)
{
string ClientLine = "";
fstream ClientsFile;
ClientsFile.open(ClientFileName, ios::out);
if (ClientsFile.is_open())
{
for (clsClient &C : vClients) {
ClientLine = clsClient::_ConvertClientObjectToLine(C, "#//#");
if (!C.markForDeletion)
{
ClientsFile << ClientLine << endl;
}
}
ClientsFile.close();
}
}
vector<clsClient> clsClient::_LoadClientsDataFromFile()
{
fstream ClientsFile;
string ClientLine;
vector<clsClient> vClients;
ClientsFile.open(ClientFileName, ios::in);
if (ClientsFile.is_open())
{
while (getline(ClientsFile,ClientLine))
{
clsClient Client = _ConvertClientLineToClientObject(ClientLine);
vClients.push_back(Client);
}
ClientsFile.close();
}
return vClients;
}
void clsClient::_AddClientDataToFile(string ClientLine)
{
fstream ClientFile;
ClientFile.open(ClientFileName, ios::app);
if (ClientFile.is_open())
{
ClientFile << ClientLine << endl;
ClientFile.close();
}
}
void clsClient::_Update()
{
vector<clsClient> vClients;
vClients = clsClient::_LoadClientsDataFromFile();
for (clsClient &C:vClients) {
if (C.account_number == account_number)
{
C = *this; break;
}
}
_SaveClienstDataToFile(vClients);
}
void clsClient::_Add()
{
_AddClientDataToFile(_ConvertClientObjectToLine(*this, "#//#"));
}
void clsClient::_MarkClientForDelete(vector<clsClient>& vClients, string AccountNumber)
{
for (auto& C : vClients)
{
if (C.account_number==AccountNumber)
{
C.markForDeletion = true;
break;
}
}
}
void clsClient::_MarkAllClientsForDelete(vector<clsClient>& vClients)
{
for (auto& C : vClients)
{
C.markForDeletion = true;
}
}
float clsClient::_GetTotalBalances(vector<clsClient> vClients)
{
float Total = 0;
for (auto& C : vClients)
{
Total += C.balance;
}
return Total;
}
clsClient::clsClient(enMode Mode, string FirstName, string LastName, string Email, string Phone, string AccountNumber, string PinCode, float Balance)
:clsPerson(FirstName, LastName, Email, Phone), _AccountNumber(AccountNumber), _PinCode(PinCode), _Balance(Balance),_Mode(Mode) {}
string clsClient::GetAccountNumber()
{
return _AccountNumber;
};
float clsClient::GetBalance() {
return _Balance;
}
void clsClient::SetBalance(float Balance) {
_Balance = Balance;
}
void clsClient::SetPinCode(string PinCode) {
_PinCode = PinCode;
}
string clsClient::GetPinCode() {
return _PinCode;
}
clsClient clsClient::Find(string AccountNumber) {
fstream ClientsFile;
string ClientLine = "";
ClientsFile.open(ClientFileName, ios::in);
if (ClientsFile.is_open())
{
while (getline(ClientsFile,ClientLine))
{
clsClient Client = _ConvertClientLineToClientObject(ClientLine);
if (Client.account_number==AccountNumber)
{
ClientsFile.close();
return Client;
}
}
}
return _GetEmptyClientObject();
};
clsClient clsClient::Find(string AccountNumber,string PinCode) {
clsClient Client = Find(AccountNumber);
if (Client.pin_code == PinCode && !Client.IsEmplty()) {
return Client;
}
return _GetEmptyClientObject();
}
bool clsClient::IsEmplty()
{
return (_Mode == EmptyMode);
}
bool clsClient::IsClientExisits(string AccountNumber)
{
clsClient Client =clsClient::Find(AccountNumber);
return !Client.IsEmplty();
}
clsClient::enSaveResult clsClient::Save()
{
switch (_Mode)
{
case clsClient::EmptyMode:
return svFailed;
break;
case clsClient::UpdateMode:
_Update();
return svSuccess;
break;
case clsClient::AddMode:
_Add();
return clsClient::svSuccess;
default:
break;
}
}
clsClient clsClient::GetAddNewClientObject(string AccountNumber)
{
return clsClient(AddMode, "", "", "", "", AccountNumber, "", 0);
}
bool clsClient::Delete()
{
vector<clsClient> vClients = _LoadClientsDataFromFile();
_MarkClientForDelete(vClients, this->account_number);
_SaveClienstDataToFile(vClients);
*this = _GetEmptyClientObject();
return true;
}
vector<clsClient> clsClient::GetClientsList()
{
return _LoadClientsDataFromFile();
}
double clsClient::GetTotalBalances()
{
double TotalBalances = 0;
vector<clsClient> vClients=_LoadClientsDataFromFile();
TotalBalances = _GetTotalBalances(vClients);
return TotalBalances;
}
bool clsClient::DeleteAll()
{
vector<clsClient> vClietns = _LoadClientsDataFromFile();
if (vClietns.size()==0)
{
return false;
}
_MarkAllClientsForDelete(vClietns);
_SaveClienstDataToFile(vClietns);
return true;
}
void clsClient::Deposite(float amount)
{
this->balance += amount;
Save();
}
void clsClient::Withdraw(float amount)
{
this->balance -= amount;
Save();
}
bool clsClient::Transfer(float amount, clsClient& DestinationClient)
{
if (amount > balance)
{
return false;
}
Withdraw(amount);
DestinationClient.Deposite(amount);
return true;
}