-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathPhoneBook
284 lines (261 loc) · 7.12 KB
/
PhoneBook
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
#include <iostream>
#include <string.h>
#include <cctype>
using namespace std;
class Node {
public:
char name[30];
char number[20];
Node* nextLink;
Node* prevLink;
Node(char n[], char num[])
{
strcpy(name,n);
strcpy(number, num);
nextLink = NULL;
prevLink = NULL;
}
friend class contactList;
};
class contactList {
Node* head, * temp, * ptr;
public:
void insert();
void sort();
void deletecontact(char n[30]);
void searchbyname(char p[30]);
void searchbynumber(char num[20]);
void takeInput();
void display();
void update(char a[30]);
};
void contactList::takeInput() {
char name[30];
char number[20];
char c;
do {
system("cls");
cout << "\nEnter Name: ";
cin.sync();
cin.getline(name,30);
cout << "\nEnter Phone Number: ";
cin >> number;
while (strlen(number) != 10) {
cout << "Enter Valid Phone Number";
cin >> number;
}
temp = new Node(name, number);
if (head == NULL)
{
head = temp;
}
else
{
ptr = head;
while (ptr->nextLink != NULL)
{
ptr = ptr->nextLink;
}
ptr->nextLink = temp;
temp->prevLink = ptr;
}
cout << "\nDo you wish to continue?\n";
cin >> c;
} while (c == 'y' || c == 'Y');
}
void contactList::display() {
system("cls");
ptr = head;
while (ptr != NULL)
{
cout << "Name: " << ptr->name << endl;
cout << "Phone Number: " << ptr->number << endl;
ptr = ptr->nextLink;
}
}
void contactList::insert() {
takeInput();
}
void contactList::sort() {
Node* a, * b;
int temp;
char n[30];
char no[20];
for (a = head; a->nextLink != NULL; a = a->nextLink) {
for (b = a->nextLink; b!=NULL; b = b->nextLink) {
temp = strcmp(a->name, b->name);
if (temp > 0) {
//Swapping the names
strcpy(n, a->name);
strcpy(a->name, b->name);
strcpy(b->name, n);
strcpy(no, a->number);
strcpy(a->number, b->number);
strcpy(b->number, no);
}
}
}
}
void contactList::deletecontact(char n[30]) {
int c = 0;
ptr = head;
while (ptr != NULL) {
if (strcmp(n, ptr->name) == 0) {
c = 1;
break;
}
else
c = 2;
ptr = ptr->nextLink;
}
if (c == 1 && ptr != head && ptr->nextLink != NULL) {
ptr->prevLink->nextLink = ptr->nextLink;
ptr->nextLink->prevLink = ptr->prevLink;
delete(ptr);
cout << "Contact Deleted Successfully!\n";
}
if (ptr == head) {
head = head->nextLink;
head->prevLink = NULL;
delete(ptr);
cout << "Contact Deleted Successfully!\n";
}
if (ptr->nextLink == NULL) {
ptr->prevLink->nextLink = NULL;
ptr->prevLink = NULL;
delete(ptr);
cout << "Contact Deleted Successfully!\n";
}
if(c==2)
cout << "Name Not Found!\n";
}
void contactList::searchbyname(char p[30]) {
system("cls");
ptr = head;
while (ptr != NULL) {
if (strcmp(p, ptr->name) == 0) {
cout << "Contact Found.\n";
cout << "Name: " << ptr->name << endl;
cout << "Phone Number: " << ptr->number << endl;
}
ptr = ptr->nextLink;
}
}
void contactList::searchbynumber(char num[20]) {
system("cls");
ptr = head;
while (ptr != NULL) {
if (strcmp(num, ptr->number) == 0) {
cout << "Contact Found.\n";
cout << "Name: " << ptr->name << endl;
cout << "Phone Number: " << ptr->number << endl;
}
ptr = ptr->nextLink;
}
}
void contactList::update(char a[30]) {
system("cls");
char choice;
int n;
ptr = head;
while (ptr != NULL) {
if (strcmp(a, ptr->name) == 0) {
do {
cout << "Which type of information you want to Update?\n";
cout << "1. Name\t2. Phone Number\n";
cin >> n;
switch (n) {
case 1:
cout << "Enter New Name: ";
cin >> ptr->name;
break;
case 2:
cout << "Enter New Phone Number: ";
cin >> ptr->number;
while (strlen(ptr->number) != 10) {
cout << "Enter a Valid Phone Number!\n";
cin >> ptr->number;
}
break;
}
cout << "Do you wish to continue?\n";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
}
ptr=ptr->nextLink;
}
}
int main()
{
contactList c; //Creating object of contactList Class
int choice,ch;
char a;
char name[30];
char number[20];
cout << "\t\t**********\n\t\tPHONE BOOK\n\t\t**********\n\n";
cout << "Developed by Nitin Nishad\n*************************\n";
cout << "\n\nEnter Username\n";
cin.getline(name, 30);
cout << "\n\nHello " << name<<", Let's get started.\n\n";
system("cls");
do {
cout << "\t\t**********\n\t\tPHONE BOOK\n\t\t**********\n\n";
cout << "\n1. Add New Contact.\n2. Update Existing Contact.\n3. Display All Contacts.\n4. Search Contact\n5. Delete a Contact.\n6. Exit.\n\n";
cin >> choice;
switch (choice)
{
case 1:
system("cls");
c.insert();
c.sort();
break;
case 2:
system("cls");
cout << "Enter Name: ";
cin.sync();
cin.getline(name, 30);
c.update(name);
c.sort();
break;
case 3:
system("cls");
c.sort();
c.display();
break;
case 4:
system("cls");
do {
system("cls");
cout << "1. Search by Name.\n2. Search by Phone Number.\n";
cin >> ch;
switch (ch) {
case 1:
cout << "Enter Name: ";
cin.sync();
cin.getline(name, 30);
c.searchbyname(name);
break;
case 2:
cout << "Enter Phone Number: ";
cin >> number;
c.searchbynumber(number);
break;
}
cout << "\nDo you wish to Continue?\n";
cin >> a;
} while (a == 'y' || a == 'Y');
break;
case 5:
system("cls");
cout << "Enter Name to delete contact: ";
cin.sync();
cin.getline(name, 30);
c.deletecontact(name);
cout << "\nDo you wish to Continue?\n";
break;
case 6:
exit(0);
break;
}
} while (choice != 6);
}