-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject.cpp
560 lines (509 loc) · 18.5 KB
/
project.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#include <iostream>
#include <iomanip>
#include <fstream>
#include <conio.h>
using namespace std;
//fuctions
//function to add a contact
bool Addcontact(string phone, string name, string address, string email, string Users_Contact[30][4]);
//function to view list of all contacts
void View_All_ContactList(string Users_Contact[30][4]);
//function to search for a contact
bool Search_Contact(string phone, string Users_Contact[30][4]);
//function to update a contact
bool Update_Contact(string option, string phone, string name, string email, string address, string Users_Contact[30][4]);
//function to delete a contact
bool Del_Contact(string phone, string Users_Contact[30][4]);
//validators
//to valid the name
bool Valid_Name(string name);
//to valid phone number
bool Valid_Phone(string phone);
//to valid email address
bool Valid_email(string email);
//file handling
void Save_Contacts(string Users_Contact[30][4]);
void Load_Contacts(string Users_Contact[30][4]);
int main()
{
///////////////////////Variables
int choose;
string phone;
string name, address, email;
string option;
///////////////////////Arrays////////////////////////
string Users_Contact[30][4];
//////////////////////////////////////////Load data///////////////////////////////////
Load_Contacts(Users_Contact);
while (choose != 0)
{
cout<<"\t\t\t\t*\t*";
cout<<"\t\t\t\t**\t**";
cout<<"\t\t\t\t***\t***";
cout<<"\t\t\t\t****\t****";
cout<<"\t\t\t\t*****\t*****";
cout<<"\t\t\t\t******\t******";
cout<<"\t\t\t\t*******\t*******";
cout<<"\t\t\t\t*******\t*******";
cout<<"\t\t\t\t******\t******";
cout<<"\t\t\t\t*****\t*****";
cout<<"\t\t\t\t****\t****";
cout<<"\t\t\t\t***\t***";
cout<<"\t\t\t\t**\t**";
cout<<"\t\t\t\t*\t*";
cout << "\n";
cout << "\n";
cout << " **** Welcome to Contact Management System ****\n";
cout << "\n";
// Menu of contact management system
cout << " MAIN MENU\n";
cout << " ======================\n";
cout << " 1 Add a new Contact\n";
cout << " 2 Search for Contact\n";
cout << " 3 List of all Contacts\n";
cout << " 4 Update a Contact\n";
cout << " 5 Delete a Contact\n";
cout << " 6 Check Valid Contact number\n";
cout << " 0 Exit\n";
cout << " ======================\n";
cout << " Enter the choice:";
cin >> choose;
system("CLS");
switch (choose)
{
/////////////////////////////////////////////////Add a new contact///////////////////////////////////////
case 1:
addcontact:
cout << "======================================================================================" << endl;
cout << " Add a new Contact" << endl;
cout << "======================================================================================" << endl;
cin.ignore();
cout << "Phone No: ";
getline(cin, phone);
cout << "Name: ";
getline(cin, name);
cout << "Address: ";
getline(cin, address);
cout << "Email address: ";
getline(cin, email);
cout << endl
<< endl;
if (Valid_Name(name) && Valid_Phone(phone) && Valid_email(email))
{
if (Addcontact(phone, name, address, email, Users_Contact))
{
cout << "New Contact is Added.."
<< endl;
}
else
{
cout << "New Contact Is Not Added Due To Some Error" << endl;
}
}
else
{
cout << "Invalid Input...Please Enter The Above Details In Following Ways."
<< endl;
cout << "Phone: 03(XXXXXXXXX)only numers Are Allowed" << endl;
cout << "Name: Only Alphabets Are Allowed(Smaller OR Cpaital) of length Max 30"
<< endl;
cout << "Address: All Type of characters are Valid " << endl;
cout << "Email Addess: alphabets/numbers@gmail/yahoo.com\n"
<< endl;
cout << "Do you wanna try again(Y/N) :";
cin >> option;
system("CLS");
if (option == "Y" || option == "y")
{
goto addcontact;
}
else if (!(option == "Y" || option == "y") || (option == "n" || option == "N"))
{
cout << "You Entered Wrong input..." << endl;
}
}
cout << "Press any to continue: ";
getch();
system("CLS");
break;
/////////////////////////////////////////Search for contact/////////////////////////////////
case 2:
cout << "=========================================================================================================" << endl;
cout << " Search a Contact" << endl;
cout << "=========================================================================================================\n"
<< endl;
cout << "Enter The Phone Number of the Contact You Want to Search: ";
cin.ignore();
getline(cin, phone);
system("CLS");
if (Search_Contact(phone, Users_Contact))
{
}
else
{
cout << "Sorry No Record Found!!!" << endl;
}
cout << "Press any to continue: ";
getch();
system("CLS");
break;
/////////////////////////////////////////List Of All contacts///////////////////////////////
case 3:
View_All_ContactList(Users_Contact);
break;
/////////////////////////////////////////Update a Contact///////////////////////////////////
case 4:
cout << "=========================================================================================================" << endl;
cout << " Update a Contact" << endl;
cout << "=========================================================================================================\n"
<< endl;
cin.ignore();
cout << "Enter The Phone Number of Contact To update: ";
getline(cin, phone);
system("CLS");
if (Update_Contact(option, phone, name, email, address, Users_Contact))
{
}
else
{
cout << "No Contact Found Relate to This Phone Number " << endl;
}
cout << "Press any key to continue..: ";
getch();
system("CLS");
break;
/////////////////////////////////////////delete a contact////////////////////////////////////
case 5:
cout << "=========================================================================================================" << endl;
cout << " Delete a Contact" << endl;
cout << "=========================================================================================================\n"
<< endl;
cin.ignore();
cout << "Enter The Phone Number of Contact to Delete: ";
getline(cin, phone);
if (Del_Contact(phone, Users_Contact))
{
cout << "Contact Is Deleted Successfully!!!"
<< endl;
}
else
{
cout << "Contact Not Deleted Because Of some Error.."
<< endl;
}
cout << "Press amy key to continue: ";
getch();
system("CLS");
break;
////////////////////////////////////////Check //////////////////////////////////////////////
case 6:
cout << "=========================================================================================================" << endl;
cout << " Check Weather Number is Valid or not" << endl;
cout << "=========================================================================================================\n"
<< endl;
cin.ignore();
cout << "Enter the Phone No: ";
getline(cin, phone);
if (Valid_Phone(phone))
{
cout << "Phone Number Is Valid..."
<< endl;
}
else
{
cout << "Phone Number Is invalid..."
<< endl;
}
cout << "Press amy key to continue: ";
getch();
system("CLS");
cout << "=========================================================================================================" << endl;
cout << " Check Weather name is Valid or not" << endl;
cout << "=========================================================================================================\n"
<< endl;
cout << "Enter the Name: ";
cin.ignore();
getline(cin, name);
if (Valid_Name(name))
{
cout << "Name Is Valid..."
<< endl;
}
else
{
cout << "Name Is invalid..."
<< endl;
}
cout << "Press amy key to continue: ";
getch();
system("CLS");
cout << "=========================================================================================================" << endl;
cout << " Check Weather email is Valid or not" << endl;
cout << "=========================================================================================================\n"
<< endl;
cout << "Enter the Email: ";
cin.ignore();
getline(cin, email);
if (Valid_email(email))
{
cout << "Email Is Valid..."
<< endl;
}
else
{
cout << "Email Is invalid..."
<< endl;
}
cout << "Press amy key to continue: ";
getch();
system("CLS");
break;
/////////////////////////////////////////Exit Case///////////////////////////////////////////
case 0:
cout<<"Program Exited Successfully...\n";
cout << "Thanks For Using the Program!!" << endl;
Save_Contacts(Users_Contact);
return 0;
default:
cout << "Wrong Input!!!!!" << endl;
}
}
return 0;
}
bool Addcontact(string phone, string name, string address, string email, string Users_Contact[30][4])
{
bool flag;
for (int i = 0; i < 100; i++)
{
if (Users_Contact[i][0] == "")
{
Users_Contact[i][0] = name;
Users_Contact[i][1] = address;
Users_Contact[i][2] = phone;
Users_Contact[i][3] = email;
flag = true;
break;
}
}
return flag;
}
bool Valid_Name(string name)
{
bool flag;
int size = name.length();
if (size <= 30)
{
for (int i = 0; i < size; i++)
{
if ((name[i] >= 'A' && name[i] <= 'Z') || (name[i] >= 'a' && name[i] <= 'z') || name[i] == ' ')
{
flag = true;
}
else
{
flag = false;
break;
}
}
}
return flag;
}
bool Valid_Phone(string phone)
{
bool flag;
int size = phone.length();
if (size == 11)
{
for (int i = 2; i < size; i++)
{
if ((phone[0] == '0' && phone[1] == '3') && (phone[i] >= '0' && phone[i] <= '9'))
{
flag = true;
}
else
{
flag = false;
break;
}
}
}
return flag;
}
bool Valid_email(string email)
{
bool flag = false;
int email_size;
email_size = email.length();
if (email[0] != '@' && email[email_size - 4] == '.' && email[email_size - 3] == 'c' && email[email_size - 2] == 'o' && email[email_size - 1] == 'm')
{
for (int i = 0; i < email_size; i++)
{
if (email[i] == '@')
{
flag = true;
}
}
}
return flag;
}
void View_All_ContactList(string Users_Contact[30][4])
{
cout << "\n\t\t================================\n\t\t\tLIST OF CONTACTS\n\t\t================================";
cout<<endl;
cout << "===========================================================================================================================================" << endl;
cout << left << setw(30) << "Name" << left << setw(30) << "Address" << left << setw(30) << "Phone Number" << left << setw(30) << "Email"
<< endl;
for (int i = 0; i < 30; i++)
{
if (Users_Contact[i][0] != "")
{
for (int j = 0; j < 4; j++)
{
cout << left << setw(30) << Users_Contact[i][j];
}
cout << endl;
}
}
cout
<< "==========================================================================================================================================="
<< endl;
cout << "Press any to continue: ";
getch();
system("CLS");
}
bool Search_Contact(string phone, string Users_Contact[30][4])
{
bool flag = false;
for (int i = 0; i < 30; i++)
{
if (phone == Users_Contact[i][2])
{
cout << "===========================================================================================================================================" << endl;
cout << " Search Result" << endl;
cout << left << setw(30) << "Name" << left << setw(30) << "Address" << left << setw(30) << "Phone Number" << left << setw(30) << "Email" << endl;
for (int j = 0; j < 4; j++)
{
cout << left << setw(30) << Users_Contact[i][j];
}
cout << endl;
flag = true;
cout << "===========================================================================================================================================" << endl;
}
}
return flag;
}
bool Update_Contact(string option, string phone, string name, string email, string address, string Users_Contact[30][4])
{
bool flag = false;
for (int i = 0; i < 30; i++)
{
if (phone == Users_Contact[i][2])
{
update:
cout << "Name: ";
getline(cin, name);
cout << "Address: ";
getline(cin, address);
cout << "Email address: ";
getline(cin, email);
cout << endl;
if (Valid_Name(name) && Valid_email(email))
{
Users_Contact[i][0] = name;
Users_Contact[i][1] = address;
Users_Contact[i][3] = email;
cout << "Contact Updated Successfully!!!"
<< endl;
flag = true;
}
else
{
cout << "Invalid Input...Please Enter The Above Details In Following Ways."
<< endl;
cout << "Name: Only Alphabets Are Allowed(Smaller OR Cpaital) of length Max 30"
<< endl;
cout << "Address: All Type of characters are Valid "
<< endl;
cout << "Email Addess: alphabets/numbers@gmail/yahoo.com\n"
<< endl;
cout << "Do you wanna try again(Y/N) :";
cin >> option;
system("CLS");
if (option == "Y" || option == "y")
{
goto update;
}
else if (!(option == "Y" || option == "y") || (option == "n" || option == "N"))
{
cout << "You Entered Wrong input..." << endl;
}
}
}
}
return flag;
}
bool Del_Contact(string phone, string Users_Contact[30][4])
{
bool flag = false;
for (int i = 0; i < 30; i++)
{
if (Users_Contact[i][2] == phone)
{
Users_Contact[i][0] = {""};
Users_Contact[i][1] = {""};
Users_Contact[i][2] = {""};
Users_Contact[i][3] = {""};
flag = true;
}
}
return flag;
}
void Save_Contacts(string Users_Contact[30][4])
{
ofstream SaveContacts("Contacts.csv");
if (SaveContacts.is_open())
{
for (int i = 0; i < 30; i++)
{
if (Users_Contact[i][0] != "")
{
for (int j = 0; j < 4; j++)
{
SaveContacts << Users_Contact[i][j] << ",";
}
SaveContacts << endl;
}
}
}
}
void Load_Contacts(string Users_Contact[30][4])
{
int i = 0;
ifstream LoadContacts("Contacts.csv");
if (!LoadContacts.is_open())
{
cout << "File failed to open" << endl;
}
else
{
string name, Address, phonenumber, email;
string line;
while (!LoadContacts.eof() && i < 30)
{
getline(LoadContacts, line);
if (line == "")
continue;
stringstream ss(line);
getline(ss, name, ',');
Users_Contact[i][0] = name;
getline(ss, Address, ',');
Users_Contact[i][1] = Address;
getline(ss, phonenumber, ',');
Users_Contact[i][2] = phonenumber;
getline(ss, email, ',');
Users_Contact[i][3] = email;
i++;
}
LoadContacts.close();
}
}