-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path#50 (3) - Delete Client By Account Number.txt
More file actions
203 lines (162 loc) · 4.17 KB
/
Copy path#50 (3) - Delete Client By Account Number.txt
File metadata and controls
203 lines (162 loc) · 4.17 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
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
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<vector>
using namespace std;
const string ClientsFileName = "Clients.txt";
struct sClient
{
string AccountNumber = "";
string PinCode = "";
string Name = "";
string Phone = "";
double AccountBalance = 0.0;
bool MarkForDelete = false;
};
string ReadClientAccountNumber()
{
string AccountNumber = "";
cout << "Please Enter Account Number ? ";
cin >> AccountNumber;
return AccountNumber;
}
vector <string> SplitString(string S1, string delim)
{
short pos = 0;
string sWord = "";
vector <string> vString;
while ((pos = S1.find(delim)) != std::string::npos)
{
sWord = S1.substr(0, pos);
if (sWord != "")
{
vString.push_back(sWord);
}
S1.erase(0, pos + delim.length());
}
if (S1 != "")
{
vString.push_back(S1);
}
return vString;
}
sClient ConvertLineToRecord(string LineRecord, string Seperator = "#//#")
{
sClient Client;
vector <string> vClientData = SplitString(LineRecord, Seperator);
Client.AccountNumber = vClientData[0];
Client.PinCode = vClientData[1];
Client.Name = vClientData[2];
Client.Phone = vClientData[3];
Client.AccountBalance = stod(vClientData[4]);
return Client;
}
vector <sClient> LoadClientsDataFromFile(string FileName)
{
vector <sClient> vClients;
fstream MyFile;
MyFile.open(FileName, ios::in);
if (MyFile.is_open())
{
string Line;
sClient Client;
while (getline(MyFile, Line))
{
Client = ConvertLineToRecord(Line);
vClients.push_back(Client);
}
MyFile.close();
}
return vClients;
}
void PrintClientCard(sClient& Client)
{
cout << "\n\nThe following are the Client Details :\n\n";
cout << "AccountNumber : " << Client.AccountNumber << endl;
cout << "PinCode : " << Client.PinCode << endl;
cout << "Name : " << Client.Name << endl;
cout << "Phone : " << Client.Phone << endl;
cout << "AccountBalance : " << Client.AccountBalance << endl;
}
bool FindClientByAccountNumber(string AccountNumber, sClient& Client, vector <sClient> &vClients)
{
for (sClient& C : vClients)
{
if (C.AccountNumber == AccountNumber)
{
Client = C;
return true;
}
}
return false;
}
bool MarkClientForDeleteByAccountNumber(string AccountNumber, vector <sClient>& vClients)
{
for (sClient& C : vClients)
{
if (C.AccountNumber == AccountNumber)
{
C.MarkForDelete = true;
return true;
}
}
return false;
}
string ConvertRecordToLine(sClient& BankClientData, string Seperator = "#//#")
{
string sClientRecord = "";
sClientRecord += BankClientData.AccountNumber + Seperator;
sClientRecord += BankClientData.PinCode + Seperator;
sClientRecord += BankClientData.Name + Seperator;
sClientRecord += BankClientData.Phone + Seperator;
sClientRecord += to_string(BankClientData.AccountBalance);
return sClientRecord;
}
void SaveClientsDataToFile(string FileName, vector <sClient>& vClients)
{
fstream MyFile;
MyFile.open(FileName, ios::out);
if (MyFile.is_open())
{
string DataLine = "";
for (sClient &C : vClients)
{
if (C.MarkForDelete == false)
{
DataLine = ConvertRecordToLine(C);
MyFile << DataLine << endl;
}
}
MyFile.close();
}
}
void DeleteClientByAccountNumber(string AccountNumber, vector <sClient> &vClients)
{
sClient Client;
char Answer = 'n';
if (FindClientByAccountNumber(AccountNumber, Client, vClients))
{
PrintClientCard(Client);
cout << "\n\nAre you sure you want delete this Client ? Y/N? ";
cin >> Answer;
if (Answer == 'Y' || Answer == 'y')
{
MarkClientForDeleteByAccountNumber(AccountNumber, vClients);
SaveClientsDataToFile(ClientsFileName, vClients);
vClients = LoadClientsDataFromFile(ClientsFileName);
cout << "\n\nClient Deleted Successfully.\n";
}
}
else
{
cout << "\nClient with Account (" << AccountNumber << ") Not Found!\n";
}
}
int main()
{
vector <sClient> vClients = LoadClientsDataFromFile(ClientsFileName);
string AccountNumber = ReadClientAccountNumber();
DeleteClientByAccountNumber(AccountNumber, vClients);
return 0;
}