-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
411 lines (396 loc) · 9.66 KB
/
main.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
#include <iostream>
#include <conio.h>
#include "HashTable.h"
#include "HashTable.cpp"
#include "Priority_Queue.h"
#include "Priority_Queue.cpp"
#include <string>
#include <regex>
using namespace std;
class Contact_Directory{
private:
HashTable<string, string, string> Name; //For Hashing By name
HashTable<string, string, string> Number; //For Hashing By number
Priority_Queue<string, string, string> P_Queue; //To arrange Alphabetically
public:
Contact_Directory();
~Contact_Directory();
void Menu();
void Add();
void Display();
void Search();
void Delete();
int Priority(string);
void Edit();
bool Name_pattern_checker(string); //For inputed Name
bool Phone_pattern_checker(string); //For inputed Phone
bool Email_pattern_checker(string); //For inputed Email
};
Contact_Directory::Contact_Directory(){
//Loads all of the data when program executes for the first time
Name.Load("Names.txt");
Number.Load("Numbers.txt");
P_Queue.Load("Aranged.txt");
}
Contact_Directory::~Contact_Directory(){
//Saves all the data when we exit our program
Name.Save("Names.txt");
Number.Save("Numbers.txt");
P_Queue.Save("Aranged.txt");
}
void Contact_Directory::Menu(){
bool Runing = true;
int option = 0;
while(Runing)
{
system("CLS");
cout<<"\t\t===================================\n\t\t\tCONTACTS DIRECTORY MENU\n\t\t===================================\n";
cout << "\t\t\t1-Add Contact\n";
cout << "\t\t\t2-Delete Contact\n";
cout << "\t\t\t3-Display all Contacts\n";
cout << "\t\t\t4-Search Contact\n";
cout << "\t\t\t5-Edit Contact\n";
cout << "\t\t\t6-Exit\n";
cout << "Enter Option: ";
cin >> option;
switch(option) {
case 1:{
Add();
break;
}
case 2:{
Delete();
getch();
break;
}
case 3:{
Display();
getch();
break;
}
case 4:{
Search();
break;
}
case 5:{
Edit();
getch();
break;
}
case 6:{
Runing = false;
break;
}
default:{
cerr << "Invalid Choice!";
getch();
}
}
}
}
void Contact_Directory::Add(){
bool runing;
string v, n, e;
bool option =1;
while(option==1){
runing = true;
while(runing){
cout<<"Enter Name (First Middle Last | First Last): ";
cin.ignore();
getline(cin, v);
if(Name_pattern_checker(v)){
runing = false;
}
else{
cout <<"Please enter Name in Specified format.\n";
getch();
}
}
runing = true;
while(runing){
cout<<"Enter Num (+92-xxx-xxxxxxx ): ";
cin>>n;
if(Phone_pattern_checker(n)){
runing = false;
}
else{
cout<<"Please enter Phone Number in Specified format.\n";
getch();
}
}
if(P_Queue.Search(v,n)){
cout<<"\t!!Contact with this name or Number is Already Saved in the Directory!\n";
getch();
break;
}
runing = true;
while(runing){
cout<<"Enter Email ID (example@mail.com): ";
cin>>e;
if(Email_pattern_checker(e)){
runing = false;
}
else{
cout <<"Please Enter Email in Specified format.\n";
getch();
}
}
int p = Priority(v);
Name.insert(v,n,e);
Number.insert(n,v,e);
P_Queue.Enqueue(v,n, p, e);
cout<<"\n\n";
cout<<"Want to add one more contact? (1/0):";
cin>>option;
}
}
void Contact_Directory::Display(){
cout<<"1 => Display A to Z."<<endl;
cout<<"2 => Normal Display."<<endl;
cout<<"Enter Option: ";
int choose; cin>>choose;
switch(choose){
case 1:{
P_Queue.display();
break;
}
case 2:{
Name.display();
break;
}
default:{
cout<<"Invalid Choice!"<<endl;
break;
}
}
}
void Contact_Directory::Search(){
cout<<"1 => Search with Name."<<endl;
cout<<"2 => Search with Phone Number."<<endl;
cout<<"Enter Option: ";
int choose; cin>>choose;
switch(choose){
case 1:{
string a;
while(true){
cout<<"Enter Name (First Middle Last | First Last): ";
cin.ignore();
getline(cin, a);
if(Name_pattern_checker(a))
break;
cout<<"Please Enter Name in Specified format."<<endl;
getch();
}
bool present = Name.Search(a);
if(!present){
cout<<"This Contact is not in directory. Wanna Add it? (y/n): ";
char ask;
cin>>ask;
if(ask=='y')
Add();
else
return;
}
break;
}
case 2:{
string num;
while(true){
cout<<"Enter Num (+92-xxx-xxxxxxx ): ";
cin>>num;
if(Phone_pattern_checker(num))
break;
cout<<"Please enter Phone Number in Specified format."<<endl;
getch();
}
bool present = Number.Search(num);
if(!present){
cout<<"This Contact is not in directory. Wanna Add it? (y/n): ";
char ask;
cin>>ask;
if(ask=='y')
Add();
else
return;
}
break;
}
default:{
cout<<"Invalid Choice!"<<endl;
break;
}
}
getch();
}
bool Contact_Directory::Name_pattern_checker(string str){
regex name_patrn ("\\w+\\s\\w+|\\w+\\s\\w+\\s\\w+"); //Expression for Name pattern checking
if(regex_match (str,name_patrn))
return true;
return false;
}
bool Contact_Directory::Phone_pattern_checker(string str){
regex phone_patrn ("\\+\\d{2}-\\d{3}-\\d{7}"); // Expression for Phone Number pattern checking
if(regex_match (str,phone_patrn))
return true;
return false;
}
bool Contact_Directory::Email_pattern_checker(string str){
regex email_patrn ("(\\w+\\d*)(\\.|_)?(\\w*\\d*)@(\\w+)(\\.(\\w+))+"); // Expression for Email ID pattern checking
if(regex_match (str,email_patrn))
return true;
return false;
}
void Contact_Directory::Delete(){
cout<<"1 => Delete with Name."<<endl;
cout<<"2 => Delete with Phone Number."<<endl;
cout<<"Enter Option: ";
int choose; cin>>choose;
switch(choose){
case 1:{
string a;
while(true){
cout<<"Enter Name (First Middle Last | First Last): ";
cin.ignore();
getline(cin, a);
if(Name_pattern_checker(a))
break;
cout<<"Please Enter Name in Specified format."<<endl;
getch();
}
string pn = Name.Delete(a);
string nam = Number.Delete(pn);
bool check = P_Queue.Delete(nam);
(check)? cout<<"\nContact has been deleted!\n" : cout<<"\nThis Contact Not in Conatacts Directory!\n";
cout<<"Press any key to continue...";
break;
}
case 2:{
string num;
while(true){
cout<<"Enter Num (+92-xxx-xxxxxxx ): ";
cin>>num;
if(Phone_pattern_checker(num))
break;
cout<<"Please enter Phone Number in Specified format."<<endl;
getch();
}
string nm = Number.Delete(num);
Name.Delete(nm);
bool check = P_Queue.Delete(nm);
(check)? cout<<"\nContact has been deleted!\n" : cout<<"\nThis Contact Not in Conatacts Directory!\n";
cout<<"Press any key to continue...";
break;
}
default:{
cout<<"Invalid Choice!"<<endl;
break;
}
}
}
void Contact_Directory::Edit(){
string a;
while(true){
cout<<"Enter Contact Name to Edit (First Middle Last | First Last): ";
cin.ignore();
getline(cin, a);
if(Name_pattern_checker(a))
break;
cout<<"Please Enter Name in Specified format."<<endl;
getch();
}
bool present = Name.Search(a);
if(!present){
cout<<"This Contact is not found in Directory!"<<endl;
getch();
return;
}else{
cout<<"\n\t1=> Edit Name"<<endl;
cout<<"\t2=> Edit Phone"<<endl;
cout<<"\t3=> Edit Email"<<endl;
int c;
cout<<"Enter Option: ";
cin>>c;
switch(c){
case 1:{
string v;
while(true){
cout<<"Enter Name (First Middle Last | First Last): ";
cin.ignore();
getline(cin, v);
if(Name_pattern_checker(v))
break;
cout<<"Please enter Name in Specified format."<<endl;
getch();
}
if(P_Queue.Search(v,"-1")){
cerr<<"\t!!Contact already exist with this name!."<<endl;
}
else{
string numb = Name.Edit(a,v,"-1","-1");
Number.Edit(numb, "-1",v,"-1");
bool check = P_Queue.Edit(a,v,"-1","-1");
(check)? cout<<"\nContact has been edited Successfully!"<<endl : cout<<"\nThis Contact Not in Conatacts Directory!"<<endl;
}
getch();
cout<<"Press any key to continue...";
break;
}
case 2:{
string n;
while(true){
cout<<"Enter Num (+92-xxx-xxxxxxx ): ";
cin>>n;
if(Phone_pattern_checker(n))
break;
cout<<"Please enter Phone Number in Specified format."<<endl;
getch();
}
if(P_Queue.Search("-1",n)){
cerr<<"\t!!Contact already exist with this Phone Number!."<<endl;
}
else{
string numb = Name.Edit(a,"-1",n,"-1");
Number.Edit(numb,n,"-1","-1");
bool check = P_Queue.Edit(a,"-1",n,"-1");
(check)? cout<<"\nContact has been edited Successfully!"<<endl : cout<<"\nThis Contact Not in Conatacts Directory!"<<endl;
}
getch();
cout<<"Press any key to continue...";
break;
}
case 3:{
cin.ignore();
string e;
while(true){
cout<<"Enter Email ID (example@mail.com): ";
cin>>e;
if(Email_pattern_checker(e))
break;
cout<<"Please Enter Email in Specified format."<<endl;
getch();
}
string numb = Name.Edit(a,"-1","-1",e);
Number.Edit(numb,"-1","-1",e);
bool check = P_Queue.Edit(a,"-1","-1",e);
(check)? cout<<"\nContact has been edited Successfully!"<<endl : cout<<"\nThis Contact Not in Contacts Directory!"<<endl;
getch();
cout<<"Press any key to continue...";
break;
}
default:{
cout<<"Invalid Choice!"<<endl;
break;
}
}
}
}
int Contact_Directory::Priority(string s){
char p = toupper(s[0]);
return int(p); //Converts the charachter to ascii Value
}
//Main Driver
int main() {
Contact_Directory cd;
cd.Menu();
return 0;
}