-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContact_management_source_code.cpp
502 lines (470 loc) · 12.7 KB
/
Contact_management_source_code.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//|****************************************************************************************************|
// Header files | |
//|****************************************************************************************************|
#include<iostream>
#include<conio.h>
#include<Windows.h>
#include<string.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
//*********************************************************************************************************|
// Class declration |
//*********************************************************************************************************|
class contact
{
private:
string fname,lname,phone_num;
public:
bool check_name(string);
void display();
void design();
void addcontact();
void deletecontact();
void editcontact();
void helpcontact();
void searchcontact();
void showcontact();
void deleteallcontact();
bool check_digit(string);
bool check_number(string);
void modify();
}c;
//DESIGN OF THE MAIN FUNCTION
void contact::design()
{
system("color 1F");
for(int i=0;i<=20;i++)
{
system("cls");
Sleep(200);
cout<<endl;
cout<<"\n****************************************************************************************************";
cout<<"\n* \t\t|============================================================================| *";
cout<<"\n* \t\t|============================================================================| *";
cout<<"\n* \t\t|****************************************************************************| *";
cout<<"\n* \t\t| ***CONTACT MANAGEMENT*** | *";
cout<<"\n* \t\t|****************************************************************************| *";
cout<<"\n* \t\t|============================================================================| *";
cout<<"\n* \t\t============================================================================| *";
cout<<"\n****************************************************************************************************";
cout<<endl;
Sleep(200);
}
}
//This function add contact in the file:
void contact::addcontact()
{
system("cls");
system("color 2F");
string tempname;
cout<<"\t\t\n *******************************ADD CONTACT PAGE******************************************";
x:
cout<<"\t\t\n Enter your first name:";
cin>>fname;
tempname=fname;
if(check_name(fname)==true)
{
cout<<"\n This name is already present try to use another name:";
goto x;
}
fname=tempname;
cout<<"\t\t\n Enter your last name:";
cin>>lname;
cout<<"\t\t\n Enter your phone number:";
cin>>phone_num;
if(check_digit(phone_num)==true)
{
if(check_number(phone_num)==true)
{
ofstream fout;
fout.open("phone.dat",ios::app|ios::out|ios::binary);
fout.write((char*)&c,sizeof(c));
cout<<" \n CONTACT SAVED SUCCESSFULLY.................";
fout.close();
getch();
}
else
{
cout<<"\n You can't type alphabet or special number in a phone no.";
getch();
}
}
else
{
cout<<"\n\t Phone number must contain 10 digits";
cout<<"\n Press any key to continue:";
getch();
}
}
//Check digits function:
bool contact::check_digit(string x)
{
if(x.length()==10)
return true;
else
return false;
}
//check number function:
bool contact::check_number(string x)
{
bool check=true;
for(int i=0;i< x.length();i++)
{
if(!(int(x[i])>= 48 && int(x[i]) <= 57))
{
check=false;
break;
}
}
if(check==true)
return true;
if(check==false)
return false;
}
//this function checks the name
bool contact::check_name(string x)
{
bool check=false;
ifstream fin;
fin.open("phone.dat",ios::in|ios::binary);
if(fin)
{
while(fin.read(reinterpret_cast<char *> (&c), sizeof(contact)))
{
if(x==fname)
{
check=true;
break;
}
}
}
if(check==true)
return true;
if(check==false)
return false;
}
//this function delete contact fron the file:
void contact::deletecontact()
{
system("cls");
system("color 1F");
string name;
int flag=0;
cout<<"\n*****************DELETE SECTION********************";
cout<<"\n Enter name you want to delete this contact details:";
cin>>name;
ifstream phone;
ofstream tout;
phone.open("phone.dat",ios::in|ios::binary);
tout.open("temp.dat",ios::app|ios::out|ios::binary);
if(phone)
{
while (phone.read(reinterpret_cast<char *> (&c), sizeof(contact)))
{
if(name==fname)
{
cout<<"\n This contact details now deleted:";
Sleep(200);
display();
flag++;
getch();
}
else
{
tout.write((char*)&c,sizeof(c));
}
}
phone.close();
tout.close();
remove("phone.dat");
rename("temp.dat","phone.dat");
if(flag==0)
{
cout<<"\n That name not present in that file";
cout<<"\n Press any key to continue:";
getch();
}
}
else
{
cout<<"\n File doesn't exist";
cout<<"\n Press any key to continue:";
getch();
}
}
//this function search contact from the file:
void contact::searchcontact()
{
system("cls");
system("color 2F");
string name;
int flag=0;
cout<<"\n ******************Search section**************************";
cout<<"\n Enter Name you want to search:";
cin>>name;
ifstream phone;
phone.open("phone.dat",ios::in);
if(phone)
{
while(phone.read(reinterpret_cast<char *> (&c), sizeof(contact)))
{
if(name==fname)
{
flag++;
display();
}
}
phone.close();
cout<<"\n Press any key to continue:";
getch();
if(flag==0)
{
system("cls");
cout<<"\n That contact details is not present in file";
cout<<"\n Press any key to continue:";
getch();
}
}
else
{
cout<<"File doesn't exist:";
cout<<"\n Press any key to continue:";
getch();
}
}
//this function show contact from the file:
void contact::showcontact()
{
system("cls");
system("color 1F");
cout<<" \n\t**************************All CONTACT DETAILS***********************************";
ifstream phone;
phone.open("phone.dat",ios::in|ios::binary);
if(phone)
{
while(phone.read(reinterpret_cast<char *> (&c), sizeof(contact)))
{
display();
}
phone.close();
cout<<"\n Press any key to continue>..........";
getch();
}
else
{
cout<<"\n File doesn't Exist";
cout<<"\n Press any key to continue.....";
getch();
}
}
void contact::display()
{
cout<<"\t \n*************************************************************";
cout<<"\n First Name:"<<fname;
cout<<endl;
cout<<"\n Last Name:"<<lname;
cout<<endl;
cout<<"\n Phone number:"<<phone_num;
cout<<"\t \n*************************************************************";
}
//this function delete all contact from the file:
void contact::deleteallcontact()
{
system("cls");
system("color 4F");
char ch;
cout<<"\n Are you sure you want to delete all the contact(y/n):";
cin>>ch;
if(ch=='y' | ch=='Y')
{
ifstream phone;
phone.open("phone.dat",ios::in|ios::binary);
if(phone)
{
phone.close();
remove("phone.dat");
cout<<"\n ALL DATA DELETED SUCCESSFULLY";
cout<<"\n Press any key to continue.....";
getch();
}
else
{
cout<<"\n file doesn't exist";
cout<<"\n Press any key to continue.. ";
getch();
}
}
}
//This is help section:
void contact::helpcontact()
{
system("cls");
system("color 1F");
cout<<" \n\t\t\tTHIS IS HELPS SECTION:";
cout<<" \n\t\t\t********************************************************************************";
cout<<endl;
cout<<" \nInstructions";
cout<<" \n 1.if You want to add a new contact name should be unique";
cout<<endl;
cout<<" \n 2.Number should be 10 digits";
cout<<endl;
cout<<" \n 3.You want to search or delete the contact this action perform using name";
cout<<endl;
cout<<" \n\t\t\t*****************************************************************************";
cout<<"\n Press any key to continue:..................................";
getch();
}
//This function edit the contact the details:
void contact::editcontact()
{
system("cls");
system("color 4F");
string name;
bool flag=false;
cout<<" \n\t\t\t*************************************Edit Section*********************************************";
cout<<" \n\t\t\tWhich Contact details you want to modify give name:";
cin>>name;
fstream file;
file.open("phone.dat",ios::in|ios::out|ios::binary);
if(file)
{
while(file.read((char*)&c,sizeof(c)))
{
if(name==fname)
{
flag=true;
c.modify();
int pos=(-1)*static_cast<int>(sizeof(c));
file.seekp(pos,ios::cur);
file.write((char*)&c,sizeof(c));
cout<<"\n your details are modified:.............";
Sleep(300);
getch();
}
}
file.close();
if(flag==false)
{
cout<<"\n this name is not present in that file";
cout<<"\n Press any key to continue:";
getch();
}
}
else
{
cout<<"\n File doesn't exist";
cout<<"\n Press any key to continue:";
getch();
}
}
//Modify details section:
void contact::modify()
{
string tempname;
x:
cout<<"\n Enter your new name:";
cin>>fname;
tempname=fname;
if(check_name(fname)==true)
{
cout<<"\n This name is already present try to use another name:";
goto x;
}
fname=tempname;
cout<<"\n Enter your last name:";
cin>>lname;
y:
cout<<"\n Enter your 10-digit mobile number:";
cin>>phone_num;
if(check_digit(phone_num)==true)
{
if(check_number(phone_num)==true)
{
}
else
{
cout<<"\n the mobile number must contain number only";
getch();
goto y;
}
}
else
{
cout<<"\n The mobile number should be 10 digits";
getch();
goto y;
}
}
//****************************************************************************************************|
// MAIN FUNCTION DECLRATION |
//****************************************************************************************************|
int main()
{
system("color 1F");
cout<<"\nPress Any key to start:";
getch();
system("cls"); //this is program loading function:
cout<<"\n\n\n.................................... Program being Loading .......................";
cout<<"\n\n";
for(int i=0;i<=50;i++)
{
Sleep(80);
cout<<char(219);
}
system("cls");
c.design();
int n;
do
{
system("cls");
cout<<endl;
cout<<" \t\t\t\t\t\n**********************WELCOME TO CONTACT MANAGEMENT SYSTEM*************************";
cout<<endl;
cout<<" \t\t\t\t\t\n=====================================================================================";
cout<<endl;
cout<<" \t\t\t\t\t\n1>ADD CONTACT";
cout<<endl;
cout<<" \t\t\t\t\t\n2>DELETE CONTACT";
cout<<endl;
cout<<" \t\t\t\t\t\n3>SEARCH CONTACT";
cout<<endl;
cout<<" \t\t\t\t\t\n4>EDIT CONTACT";
cout<<endl;
cout<<" \t\t\t\t\t\n5>HELP CONTACT";
cout<<endl;
cout<<" \t\t\t\t\t\n6>SHOW CONTACT";
cout<<endl;
cout<<" \t\t\t\t\t\n7>DELETE ALL CONTACT";
cout<<endl;
cout<<" \t\t\t\t\t\n8>EXIT";
cout<<endl;
cout<<" \t\t\t\t\t\n=====================================================================================";
cout<<" \t\t\t\t\t\nENTER YOUR CHOICE:";
cin>>n;
switch(n)
{
case 1: c.addcontact();
break;
case 2: c.deletecontact();
break;
case 3: c.searchcontact();
break;
case 4: c.editcontact();
break;
case 5: c.helpcontact();
break;
case 6: c.showcontact();
break;
case 7: c.deleteallcontact();
break;
case 8: cout<<"\n Thankx for using this program:)";
getch();
exit(0);
default:
cout<<"\n WRONG INPUT";
}
}
while(1);
return 0;
}