-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneBook.cpp
More file actions
657 lines (599 loc) · 17.7 KB
/
Copy pathPhoneBook.cpp
File metadata and controls
657 lines (599 loc) · 17.7 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
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#include <iostream>
#include <string>
#include <iomanip>
#include <thread>
#include <chrono>
#include <fstream>
#include <conio.h>
using namespace std;
struct Contact
{
string name;
string phoneNumber;
string email;
string address;
string workplace;
Contact *next;
};
Contact *head = nullptr;
void Boxed(const string &message, const string &colorCode = "\033[0m")
{
int width = 80;
int padding = (width - message.length() - 4) / 2;
string border = string(width, '-');
cout << colorCode << border << "\033[0m" << endl;
cout << colorCode << "|" << string(padding, ' ') << message << string(padding, ' ') << "|\033[0m" << endl;
cout << colorCode << border << "\033[0m" << endl;
}
bool isDuplicate(const string &name)
{
Contact *current = head;
while (current != nullptr)
{
if (current->name == name)
{
Boxed("Contact with this name already exists.", "\033[31m\033[1m");
cout << "\033[1mDo you want to still save the contact? \033[0m\033[1;31m(y/n)\033[0m\033[1m:\033[0m ";
char saveChoice;
cin >> saveChoice;
if (saveChoice == 'y' || saveChoice == 'Y')
{
return false;
}
else
{
return true;
}
}
current = current->next;
}
return false;
}
bool isDuplicatePhoneNumber(const string &phoneNumber)
{
Contact *current = head;
while (current != nullptr)
{
if (current->phoneNumber == phoneNumber)
{
Boxed("Contact with this phone number already exists.", "\033[31m\033[1m");
return true;
}
current = current->next;
}
return false;
}
void addContact()
{
system("cls");
Contact *newContact = new Contact;
cout << "\n\033[1mEnter Full Name: \033[0m";
getline(cin, newContact->name);
if (isDuplicate(newContact->name))
{
delete newContact;
return;
}
cout << "\033[1mEnter Phone Number: \033[0m";
getline(cin, newContact->phoneNumber);
if (isDuplicatePhoneNumber(newContact->phoneNumber))
{
delete newContact;
return;
}
bool validEmail = false;
while (!validEmail)
{
cout << "\033[1mEnter Email: \033[0m";
getline(cin, newContact->email);
if (newContact->email.empty())
{
validEmail = true;
}
else if (newContact->email.find('@') == string::npos || newContact->email.find('.') == string::npos)
{
Boxed("Invalid email. Please include '@' and '.' in the email.", "\033[31m\033[1m");
}
else
{
validEmail = true;
}
}
cout << "\033[1mEnter Address: \033[0m";
getline(cin, newContact->address);
cout << "\033[1mEnter Workplace: \033[0m";
getline(cin, newContact->workplace);
newContact->next = head;
head = newContact;
Boxed("Contact added successfully!", "\033[32m\033[1m");
cout << endl;
system("pause");
}
int countContacts()
{
int count = 0;
Contact *current = head;
while (current != nullptr)
{
count++;
current = current->next;
}
return count;
}
void displayContacts()
{
system("cls");
cout << "\033[1;33mLoading the contacts to display ";
char loadingChars[] = {'\\', '|', '/', '-'};
for (int i = 0; i < 12; ++i)
{
cout << loadingChars[i % 4] << "\b";
cout.flush();
this_thread::sleep_for(chrono::milliseconds(400));
}
cout << "\033[0m\n";
cout << endl;
system("cls");
int contactCount = countContacts();
if (contactCount == 0)
{
Boxed("No contacts to display.", "\033[31m\033[1m");
cout << "\033[1mWould you like to add a contact? \033[0m\033[1;31m(y/n)\033[0m\033[1m:\033[0m ";
char addChoice = _getch();
cout << addChoice << endl;
if (addChoice == 'y' || addChoice == 'Y')
{
addContact();
}
else
{
Boxed("Returning to the Menu.", "\033[32m\033[1m");
cout << endl;
system("pause");
}
return;
}
Boxed("You have " + to_string(contactCount) + " contacts in your phonebook.", "\033[1;3;36m");
cout << endl;
Boxed("Phonebook Contacts:", "\033[1;94m");
cout << endl;
Contact *current = head;
while (current != nullptr)
{
cout << "\033[1mName: \033[0m\033[1;3;36m" << current->name << "\033[0m\n";
cout << "\033[1mPhone Number: \033[0m" << current->phoneNumber << "\n";
cout << "\033[1mEmail: \033[0m" << current->email << "\n";
cout << "\033[1mAddress: \033[0m" << current->address << "\n";
cout << "\033[1mWorkplace: \033[0m" << current->workplace << "\n";
cout << endl;
cout << "\033[36m----------------------------\033[0m\n";
cout << endl;
current = current->next;
}
system("pause");
}
void searchContact()
{
system("cls");
string searchQuery;
cout << "\n\033[1mWhich Contact are you searching for? (name, phone number) \033[0m";
cin.ignore();
getline(cin, searchQuery);
int contactCount = countContacts();
cout << "\033[1;33mSearching among your ( " << contactCount << " ) contacts";
for (int i = 0; i < 3; ++i)
{
cout << ".";
cout.flush();
this_thread::sleep_for(chrono::milliseconds(1000));
}
cout << "\033[0m\n";
cout << endl;
system("cls");
Contact *current = head;
bool foundAny = false;
while (current != nullptr)
{
if (current->name.find(searchQuery) != string::npos ||
current->phoneNumber.find(searchQuery) != string::npos)
{
foundAny = true;
cout << "\n\033[1mName: \033[0m\033[1;3;36m" << current->name << "\033[0m\n";
cout << "\033[1mPhone Number: \033[0m" << current->phoneNumber << "\n";
cout << "\033[1mEmail: \033[0m" << current->email << "\n";
cout << "\033[1mAddress: \033[0m" << current->address << "\n";
cout << "\033[1mWorkplace: \033[0m" << current->workplace << "\n";
cout << endl;
cout << "\033[36m----------------------------\033[0m\n";
cout << endl;
}
current = current->next;
}
if (!foundAny)
{
Boxed("No contacts found.", "\033[31m\033[1m");
cout << "\033[1mWould you like to add the contact? \033[0m\033[1;31m(y/n)\033[0m\033[1m:\033[0m ";
char addChoice = _getch();
cout << addChoice << endl;
if (addChoice == 'y' || addChoice == 'Y')
{
addContact();
}
else
{
Boxed("Returning to the Menu.", "\033[32m\033[1m");
}
}
else
{
Boxed("Contacts found!", "\033[32m\033[1m");
}
cout << endl;
system("pause");
}
void deleteContact()
{
system("cls");
string deleteQuery;
cout << "\n\033[1mEnter Name or Number to Delete: \033[0m";
cin.ignore();
getline(cin, deleteQuery);
cout << "\033[1;33mSearching the contact ";
char loadingChars[] = {'\\', '|', '/', '-'};
for (int i = 0; i < 12; ++i)
{
cout << loadingChars[i % 4] << "\b";
cout.flush();
this_thread::sleep_for(chrono::milliseconds(400));
}
cout << "\033[0m\n";
cout << endl;
system("cls");
Contact *current = head;
Contact *previous = nullptr;
bool foundAny = false;
while (current != nullptr)
{
if (current->name.find(deleteQuery) != string::npos ||
current->phoneNumber.find(deleteQuery) != string::npos)
{
cout << "\n\033[1mAre you sure you want to delete \033[0m\033[1;3;36m" << current->name << "\033[0m? \033[1;31m(y/n)\033[0m\033[1m:\033[0m ";
char deleteChoice = _getch();
cout << deleteChoice << endl;
if (deleteChoice == 'y' || deleteChoice == 'Y')
{
if (previous == nullptr)
{
head = current->next;
}
else
{
previous->next = current->next;
}
delete current;
Boxed("Contact deleted successfully!", "\033[32m\033[1m");
cout << endl;
system("pause");
}
else
{
Boxed("Contact deletion canceled.", "\033[32m\033[1m");
system("pause");
}
return;
}
previous = current;
current = current->next;
}
if (!foundAny)
{
Boxed("No contacts found.", "\033[31m\033[1m");
cout << "\033[1mWould you like to add the contact? \033[0m\033[1;31m(y/n)\033[0m\033[1m:\033[0m ";
char addChoice = _getch();
cout << addChoice << endl;
if (addChoice == 'y' || addChoice == 'Y')
{
addContact();
}
else
{
Boxed("Returning to the Menu.", "\033[32m\033[1m");
}
}
else
{
Boxed("Contacts found!", "\033[32m\033[1m");
}
cout << endl;
system("pause");
}
void editContact()
{
system("cls");
string editQuery;
cout << "\n\033[1mEnter Name or Number to Edit: \033[0m";
cin.ignore();
getline(cin, editQuery);
cout << "\033[1;33mSearching the contact ";
char loadingChars[] = {'\\', '|', '/', '-'};
for (int i = 0; i < 12; ++i)
{
cout << loadingChars[i % 4] << "\b";
cout.flush();
this_thread::sleep_for(chrono::milliseconds(400));
}
cout << "\033[0m\n";
cout << endl;
system("cls");
Contact *current = head;
bool foundAny = false;
while (current != nullptr)
{
if (current->name.find(editQuery) != string::npos ||
current->phoneNumber.find(editQuery) != string::npos)
{
cout << "\n\033[1mEditing Contact: \033[0m\033[1;3;36m" << current->name << "\033[0m\n";
cout << "\033[1mEnter New Phone Number (leave blank to keep current): \033[0m";
string newPhoneNumber;
getline(cin, newPhoneNumber);
if (!newPhoneNumber.empty())
{
current->phoneNumber = newPhoneNumber;
}
bool validEmail = false;
while (!validEmail)
{
string newEmail;
cout << "\033[1mEnter New Email (leave blank to keep current): \033[0m";
getline(cin, newEmail);
if (newEmail.empty())
{
validEmail = true;
}
else if (newEmail.find('@') == string::npos || newEmail.find('.') == string::npos)
{
Boxed("Invalid email. Please include '@' and '.' in the email.", "\033[31m\033[1m");
}
else
{
current->email = newEmail;
validEmail = true;
}
}
cout << "\033[1mEnter New Address (leave blank to keep current): \033[0m";
string newAddress;
getline(cin, newAddress);
if (!newAddress.empty())
{
current->address = newAddress;
}
cout << "\033[1mEnter New Workplace (leave blank to keep current): \033[0m";
string newWorkplace;
getline(cin, newWorkplace);
if (!newWorkplace.empty())
{
current->workplace = newWorkplace;
}
Boxed("Contact updated successfully!", "\033[32m\033[1m");
cout << endl;
system("pause");
return;
}
current = current->next;
}
if (!foundAny)
{
Boxed("No contacts found.", "\033[31m\033[1m");
cout << "\033[1mWould you like to add the contact? \033[0m\033[1;31m(y/n)\033[0m\033[1m:\033[0m ";
char addChoice = _getch();
cout << addChoice << endl;
if (addChoice == 'y' || addChoice == 'Y')
{
addContact();
}
else
{
Boxed("Returning to the Menu.", "\033[32m\033[1m");
}
}
else
{
Boxed("Contacts found!", "\033[32m\033[1m");
}
cout << endl;
system("pause");
}
void saveContacts()
{
system("cls");
cout << "\033[1;33mSaving the contacts ";
char loadingChars[] = {'\\', '|', '/', '-'};
for (int i = 0; i < 12; ++i)
{
cout << loadingChars[i % 4] << "\b";
cout.flush();
this_thread::sleep_for(chrono::milliseconds(400));
}
cout << "\033[0m\n";
cout << endl;
system("cls");
ofstream outFile("contacts.txt");
if (!outFile)
{
Boxed("Error saving contacts to file.", "\033[31m\033[1m");
return;
}
Contact *current = head;
while (current != nullptr)
{
outFile << current->name << "\n";
outFile << current->phoneNumber << "\n";
outFile << current->email << "\n";
outFile << current->address << "\n";
outFile << current->workplace << "\n";
current = current->next;
}
outFile.close();
Boxed("Contacts saved successfully!", "\033[32m\033[1m");
cout << endl;
system("pause");
}
void loadContacts(bool showMessage = true)
{
system("cls");
cout << "\033[1;33mLoading the contact ";
char loadingChars[] = {'\\', '|', '/', '-'};
for (int i = 0; i < 12; ++i)
{
cout << loadingChars[i % 4] << "\b";
cout.flush();
this_thread::sleep_for(chrono::milliseconds(400));
}
cout << "\033[0m\n";
cout << endl;
system("cls");
ifstream inFile("contacts.txt");
if (!inFile)
{
if (showMessage)
{
Boxed("No saved contacts found.", "\033[31m\033[1m");
cout << endl;
}
return;
}
head = nullptr;
string name, phoneNumber, email, address, workplace;
while (getline(inFile, name))
{
getline(inFile, phoneNumber);
getline(inFile, email);
getline(inFile, address);
getline(inFile, workplace);
Contact *newContact = new Contact{name, phoneNumber, email, address, workplace, head};
head = newContact;
}
inFile.close();
if (showMessage)
{
Boxed("Contacts loaded successfully!", "\033[32m\033[1m");
cout << endl;
system("pause");
}
}
void displayMenu(int selectedOption)
{
system("cls");
Boxed("Phonebook Menu:", "\033[1;94m");
string options[] = {
"1. Add Contact",
"2. Display Contacts",
"3. Search Contact",
"4. Delete Contact",
"5. Edit Contact",
"6. Save Contacts",
"7. Load Contacts",
"8. Exit"};
for (int i = 0; i < 8; ++i)
{
if (i == selectedOption)
{
cout << "\033[1;32m> " << options[i] << " <\033[0m\n";
}
else
{
cout << options[i] << "\n";
}
}
Boxed("Use arrow keys to navigate and press Enter to select.", "\033[1;5;31m");
}
int navigateMenu()
{
int selectedOption = 0;
while (true)
{
displayMenu(selectedOption);
int key = _getch();
if (key == 224)
{
key = _getch();
if (key == 72)
{
selectedOption = (selectedOption - 1 + 8) % 8;
}
else if (key == 80)
{
selectedOption = (selectedOption + 1) % 8;
}
}
else if (key >= '1' && key <= '8')
{
return key - '1';
}
else if (key == 13)
{
return selectedOption;
}
}
}
int main()
{
loadContacts(false);
system("cls");
cout << "\033[1;33mPreparing the Menu";
for (int i = 0; i < 3; ++i)
{
cout << ".";
cout.flush();
this_thread::sleep_for(chrono::milliseconds(1000));
}
cout << "\033[0m\n";
cout << endl;
system("cls");
while (true)
{
int choice = navigateMenu();
switch (choice)
{
case 0:
addContact();
break;
case 1:
displayContacts();
break;
case 2:
searchContact();
break;
case 3:
deleteContact();
break;
case 4:
editContact();
break;
case 5:
saveContacts();
break;
case 6:
loadContacts();
break;
case 7:
{
cout << "\n\033[1mAre you sure you want to exit?\033[0m \033[1;31m(y/n)\033[0m\033[1m:\033[0m ";
char exitChoice = _getch();
cout << exitChoice << endl;
if (exitChoice == 'y' || exitChoice == 'Y')
{
saveContacts();
cout << endl;
Boxed("Exiting Phonebook. Goodbye!", "\033[32m\033[1m");
return 0;
}
break;
}
default:
Boxed("Invalid choice. Please try again.", "\033[31m\033[1m");
break;
}
}
return 0;
}