-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContact_Book.cpp
449 lines (415 loc) · 10.4 KB
/
Contact_Book.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include<bits/stdc++.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
class Trie
{
public:
Trie* Children[10];
int count;
// count indicates the total possible node emergining from it
Trie()
{
count=0;
for(int i=0;i<10;i++)
{
Children[i]=NULL;
}
}
bool insert(Trie*,string ph_no);
};
int iscorrectchar(string s)
{
if(s=="M"||s=="F")
{
return 1;
}
return 0;
}
int check(string s)
{
for(int i=0;i<s.length();i++)
{
if(s.at(i)<'0'||s.at(i)>'9')
{
return 0;
}
}
return 1;
}
int validno(string n,char ch)
{
if(ch=='u'&&n.length()==12&&check(n))
{
return 1;
}
else if(ch=='p'&&n.length()==10&&((n.at(0)=='9')||(n.at(0)=='8')||(n.at(0)=='7')||(n.at(0)=='6'))&&check(n))
{
return 1;
}
return 0;
}
bool Trie::insert(Trie* root,string s)
{
Trie* temp=root;
if(temp->count==0)
{
return false;
}
bool fl=true;
for(int i=0;i<10;i++)
{
if(temp->Children[s[i]-'0']!=NULL)
{
temp=temp->Children[s[i]-'0'];
}
else
{
Trie *temp1=new Trie();
temp1->count=0;
temp->Children[s[i]-'0']=temp1;
temp=temp->Children[s[i]-'0'];
fl=false;
}
}
if(!fl)
{
Trie* temp1=root;
for(int i=0;i<10;i++)
{
temp1=temp1->Children[s[i]-'0'];
temp1->count++;
}
temp1->count=-1;
return true;
}
return false;
}
void addrecord(Trie* root)
{
string Ph_no;
string info[5];
string z;
getline(cin,z);
cout<<"ENTER YOUR NAME \n";
getline(cin,info[0]); //info[0]=name
cout<<"Enter Your Father's Name \n";
getline(cin,info[1]); //info[1]=father name
cout<<"Enter Your Mother's Name \n";
getline(cin,info[2]); //info[2]=mother name
cout<<"Enter Your Gender:(M/F) \n";
getline(cin,info[3]);
//CHECKING IF GENDER IS CORRECT OR NOT
while((!iscorrectchar(info[3]))){
cout<<"Enter valid Gender as instructed(M/F) \n";
getline(cin,info[3]);
}
cout<<"Enter UID NO(12-digit No:) \n";
getline(cin,info[4]); //info[4]=UID
//CHECKING IF UID IS CORRECT OR NOT
while(!validno(info[4],'u')){
cout<<"Enter valid UID NO: \n";
getline(cin,info[4]);
}
cout<<"Enter phone number \n";
getline(cin,Ph_no); //all item[5]=PHN NO;
//CHECKING IF VALID PHONE NUMBER OR NOT;
while(!validno(Ph_no,'p'))
{
cout<<"Enter valid phone number \n";
getline(cin,Ph_no);
}
// passing allitems and object to insert function
// if we are able to insert phno in trie then create a file of name phno.txt and
// store info in it.
if(root->insert(root,Ph_no))
{
ofstream myfile;
string dummy=Ph_no;
dummy+=".txt";
myfile.open (dummy);
for(string i:info)
{
string z=i;
i+="\n";
myfile<<i;
}
myfile.close();
// cout<<"CAME\n";
return;
}
else
{
cout<<"** Number Already Exists ** \n";
}
}
vector<string>getdetails(string& num)
{
// fetch value from file
string line;
string dummy=num;
dummy+=".txt";
ifstream myfile(dummy);
vector<string>details;
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
details.push_back(line);
}
myfile.close();
}
else
{
cout<<"CANNOT OPEN FILE";
}
return details;
}
void updatedetails(vector<string>&details,string& num)
{
cout<<details[0]<<" ARE \n";
ofstream myfile;
string dummy=num;
dummy+=".txt";
myfile.open(dummy);
for(string i:details)
{
string z=i;
z+="\n";
myfile<<z;
}
myfile.close();
}
void iterateTrie(Trie* root,string& num)
{
if(root==NULL)
{
return;
}
if(root->count==0)
{
return;
}
if(num.length()==10)
{
vector<string>s=getdetails(num);
cout<<"NAME: "<<s[0]<<"\n";
cout<<"FATHER's NAME: "<<s[1]<<"\n";
cout<<"MOTHER's NAME: "<<s[2]<<"\n";
cout<<"GENDER: "<<s[3]<<"\n";
cout<<"UID: "<<s[4]<<"\n";
cout<<"\n\n --Record-- \n\n";
return;
}
for(int i=0;i<10;i++)
{
num.push_back(i+'0');
iterateTrie(root->Children[i],num);
num.pop_back();
}
}
void listrecord(Trie *root){
Trie* temp=root;
string num="";
// cout<<"CAME\n";
iterateTrie(temp,num);
}
void modifyrecord(Trie *root){
//modifying the value in this we need to also check if the user inout is valid or not
// in this we need to try to optimise the way we take input for which thing to be modified
// we also need an option of changing phn no
// we can also check if previous and present are same
// modify record done
// in modify check if list is empty TRIE IS empty
cout<<" AS A PART OF SECURITY CONCERNS WE NEED TO VEREIFY WHETHER ITS YOU OR SOMEBODY ELSE\n";
cout<<" SO PLEASE ENTER YOU MOBILE NUMBER\n";
string z;
getline(cin,z);
string number;
getline(cin,number);
cout<<"ENTER NAME\n";
string name;
getline(cin,name);
string uid;
cout<<"ENTER UID\n";
getline(cin,uid);
int i;
if(number.length()!=10)
{
cout<<" Invlaid \n";
return;
}
Trie* temp=root;
for( i=0;i<10;i++)
{
if(root->Children[number.at(i)-'0']==NULL)
{
cout<<" Invalid Number\n";
return;
}
root=root->Children[number.at(i)-'0'];
}
root=temp;
vector<string>s;
s=getdetails(number);
root=temp;
if(uid.length()!=s[4].length())
{
cout<<" Invalid \n";
return;
}
for(i=0;i<uid.length();i++)
{
if(s[4][i]!=uid[i])
{
cout<<" Invalid UID\n";
return;
}
}
if(s[0].length()!=name.length())
{
cout<<" Invalid \n";
return;
}
for(int i=0;i<name.length();i++)
{
if(s[0][i]!=name[i])
{
cout<<" Invalid \n";
return;
}
}
cout<<"**## PLEASE WAIT VALIDATING ##**\n";
//usleep(10);
cout<<"ENTER DETAILS ARE CORRECT\n";
cout<<"You are eligible to edit only your UID Father's Mother's and you name\n";
while(1){
int changer;
cout<<"Enter 1. for changing name\n2. for changing father's name\n3. for changing mother's name\n4. for editng UID\nany other key to if modification is done\n";
cin>>changer;
switch(changer){
case 1:
{
cout<<"Enter Correct name\n";
string z;
getline(cin,z);
string cname;
getline(cin,cname);
// cout<<"cname is "<<cname;
// cin>>z;
if(s[0]==cname)
{
cout<<"INVALID THIS IS THE PREVIOUS NAME\n";
}
s[0]=cname;
// cout<<s[0]<<" IS \n";
updatedetails(s,number);
break;
}
case 2:
{
cout<<"Enter correct fathers name\n";
string z;
// getline(cin,z);
cin>>z;
string fname;
getline(cin,fname);
if(fname==s[1])
{
cout<<"INVALID THIS IS THE PREVIOUS NAME\n";
}
s[1]=fname;
updatedetails(s,number);
break;
}
case 3:
{
cout<<"Enter correct Mothers name\n";
string z;
// getline(cin,z);
cin>>z;
string mname;
getline(cin,mname);
if(mname==s[2])
{
cout<<"INVALIID THIS IS THE PREVIOUS NAME\n";
}
s[2]=mname;
updatedetails(s,number);
break;
}
case 4:
{
cout<<"Enter correct UID\n";
string z;
// getline(cin,z);
cin>>z;
string uid;
getline(cin,uid);
if(uid==s[4])
{
cout<<"INVALID THIS IS THE PREVIOUS UID\n";
}
s[4]=uid;
updatedetails(s,number);
break;
}
default:
{
//1goto end;
cout<<"\n\n **UPDATED SUCCESS FULLY **\n\n";
return;
break;
}
cout<<"\n\n **UPDATED SUCCESS FULLY **\n\n";
}
}
return;
}
void menu(){
cout<<"------------------------------------*-MENU-*-------------------------------------\n";
cout<<"| 1. ADDRECORD |\n";
cout<<"| 2. listrecord |\n";
cout<<"| 3. modifyrecord |\n";
cout<<"| 4. Stop the Application |\n";
cout<<"---------------------------------------------------------------------------------\n";
}
int32_t main(){
Trie *root=new Trie();
//intitially setting dummy node value as -1 while operating cheking if root->count is -1
while(1)
{
menu();
int key;
cin>>key;
switch(key){
case 1:{
// taking details and adding them into a trie
//each time if we try to insert we need increase count on dummny node
root->count++;
addrecord(root);
break;
//DONE
}
case 2:
{
listrecord(root);
break;
}
case 3:
{
modifyrecord(root);
break;
}
case 4:
{
exit(0);
}
default:{
cout<<"___Enter Valid Key___\n";
break;
}
}
}
}