-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phone_Book.c
295 lines (267 loc) · 10.4 KB
/
Phone_Book.c
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
/***************************************************/
/* Author : Mahmoud Sayed *
* Date : 18 April 2023 *
* Description : Phone book program. *
* To save information about friends,*
* families, and people. *
* Email : Thrtr619@gmail.com *
* *
***************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Linked_List_config.h"
#include "Linked_List.h"
#include "Phone_Book.h"
/* ========== Important declarations ========== */
list_t *ContactList = NULL;
/* ========== Functions declarations ========== */
void ContactDisplayFormat(lnode_t* node) {
printf("\nName: %s %s\nPhone number: %s\nAddress: %s\nE-mail: %s\n",
node->dataPtr->FirstName, node->dataPtr->LastName, node->dataPtr->PhoneNumber, node->dataPtr->Address,
node->dataPtr->Email);
}
void Phone_Book_Init(void) {
ContactList = (list_t *) malloc(sizeof(list_t));
Linked_List_Init(ContactList);
}
void Phone_Book_SaveData(void) {
FILE *ContactFile;
lnode_t *ptr = ContactList->first;
int counter = 0;
ContactFile = fopen("Phone_Book.txt","w");
while(ptr != NULL) {
fseek(ContactFile,(long)(counter * sizeof(DATA)),SEEK_SET);
fwrite(ptr->dataPtr, sizeof(DATA),1,ContactFile);
counter++;
ptr = ptr->NextNode;
}
fclose(ContactFile);
}
int Phone_Book_LoadData(void) {
FILE *ContactFile;
ContactFile = fopen("Phone_Book.txt","r");
if(ContactFile == NULL) return 0;
DATA *DataLoaded;
while(!feof(ContactFile)) {
DataLoaded = (DATA *) malloc(sizeof(DATA));
int res = (int) fread(DataLoaded,sizeof(DATA),1,ContactFile);
if(res != 0) {
Linked_List_AddNode(ContactList,DataLoaded);
}
}
fclose(ContactFile);
return 1;
}
void Phone_Book_Menu(void) {
char select;
_Bool QUIT = 0;
printf("===============================================================\n");
printf("############### Phone Book ##############\n");
printf("===============================================================\n");
printf("############# Please Choose from the following ############\n");
printf("===============================================================\n\n");
printf("\t(+)\ta) Add a contact \t\t\t(+)\n");
printf("\t(+)\td) Delete a contact\t\t\t(+)\n");
printf("\t(+)\tl) List a contact's information\t\t(+)\n");
printf("\t(+)\tL) List all contacts' information\t(+)\n");
printf("\t(+)\te) Edit a contact's information\t\t(+)\n");
printf("\t(+)\tq) Quit program\t\t\t\t(+)\n");
do {
char Continue;
printf("\nType Here: ");
scanf("%c", &select);
switch (select) {
case 'L':
Linked_List_ListNodes(ContactList,ContactDisplayFormat);
break;
case 'a':
do {
Phone_Book_AddContact();
printf("The contact has added successfully.\n");
printf("Do you want to add another contact? (y - yes, n - no)\n");
do {
printf(">");
fflush(stdin);
scanf("%c", &Continue);
if ((Continue != 'y') && (Continue != 'n')) {
printf("Please enter a valid answer.\n");
}
} while((Continue != 'y') && (Continue != 'n'));
} while(Continue == 'y');
break;
case 'd':
do {
Phone_Book_DeleteContact();
printf("Do you want to delete another contact? (y - yes, n - no)\n");
do {
printf(">");
fflush(stdin);
scanf("%c", &Continue);
if ((Continue != 'y') && (Continue != 'n')) {
printf("Please enter a valid answer.\n");
}
} while((Continue != 'y') && (Continue != 'n'));
} while(Continue == 'y');
break;
case 'l':
do {
Phone_Book_ListContactData();
printf("Do you want to list another contact? (y - yes, n - no)\n");
do {
printf(">");
fflush(stdin);
scanf("%c", &Continue);
if ((Continue != 'y') && (Continue != 'n')) {
printf("Please enter a valid answer.\n");
}
} while((Continue != 'y') && (Continue != 'n'));
} while(Continue == 'y');
break;
case 'e':
do {
Phone_Book_EditContact();
printf("Do you want to edit another contact? (y - yes, n - no)\n");
do {
printf(">");
fflush(stdin);
scanf("%c", &Continue);
if ((Continue != 'y') && (Continue != 'n')) {
printf("Please enter a valid answer.\n");
}
} while((Continue != 'y') && (Continue != 'n'));
} while(Continue == 'y');
break;
case 'q':
printf("Are you sure you want to quit? (y - yes, n - no)\n");
do {
printf(">");
fflush(stdin);
scanf("%c", &Continue);
if ((Continue != 'y') && (Continue != 'n')) {
printf("Please enter a valid answer.\n");
}
} while((Continue != 'y') && (Continue != 'n'));
if(Continue == 'y') QUIT = 1;
else QUIT = 0;
break;
default:
printf("Please enter one of the shown settings (a, d, l, e, or q lowercase)\n");
}
fflush(stdin);
} while(!QUIT);
}
void Phone_Book_AddContact(void) {
char firstName[21];
char lastName[21];
char email[31];
char address[31];
char phoneNumber[13];
printf("Please enter the first and last name:");
scanf("%20s %20s",firstName,lastName);
fflush(stdin);
printf("Please enter the Phone number:");
/* This step is done to check if the phone number entered is already registered in the system or not */
int Abort = 0;
lnode_t *ptr = ContactList->first;
while(!Abort) {
fflush(stdin);
scanf("%s",phoneNumber);
Abort = 1;
while (ptr != NULL) {
if ((strcmp(ptr->dataPtr->PhoneNumber, phoneNumber)) == 0) {
printf("You've entered that phone number before, try another phone number.\n>>");
Abort = 0;
break;
}
ptr = ptr->NextNode;
}
}
/* End of checking step */
printf("Please enter the address:");
fflush(stdin);
scanf("%s",address);
printf("Please enter the E-mail: ");
/* This step is done to check if the E-mail entered is already registered in the system or not */
Abort = 0;
ptr = ContactList->first;
while(!Abort) {
fflush(stdin);
scanf("%s",email);
Abort = 1;
while (ptr != NULL) {
if ((strcmp(ptr->dataPtr->Email, email)) == 0) {
printf("You've entered that E-mail before, try another E-mail.\n>>");
Abort = 0;
break;
}
ptr = ptr->NextNode;
}
}
/* End of checking step */
DATA *NewAccount;
NewAccount = (DATA *) malloc(sizeof(DATA));
strcpy(NewAccount->FirstName, firstName);
strcpy(NewAccount->LastName, lastName);
strcpy(NewAccount->Address, address);
strcpy(NewAccount->Email, email);
strcpy(NewAccount->PhoneNumber, phoneNumber);
Linked_List_AddNode(ContactList,NewAccount);
}
void Phone_Book_DeleteContact(void) {
char firstName[21], lastName[21];
int result;
printf("Please enter the name number of the contact:");
fflush(stdin);
scanf("%20s %20s",firstName, lastName);
result = Linked_List_DeleteNode(ContactList, firstName, lastName);
if(result)
printf("The contact has deleted successfully.\n");
else
printf("The contact is not included.\n");
}
void Phone_Book_EditContact(void) {
char firstNameSearch[21], lastNameSearch[21];
lnode_t *Contact;
printf("Please enter the name number of the contact:");
fflush(stdin);
scanf("%20s %20s",firstNameSearch, lastNameSearch);
Contact = Linked_List_FindNode(ContactList, firstNameSearch, lastNameSearch);
if (Contact != NULL) {
char firstName[21] = "0";
char lastName[21] = "0";
char email[11] = "0";
char address[31] = "0";
char phoneNumber[12] = "0";
printf("Enter the new contact's info you want to edit in the following order: first name, last name, email,"
" address, and phone number.\nIf one of the mentioned you don't want to alter,"
"enter 0 instead.");
scanf("%20s %20s %10s %30s %11s", firstName, lastName, email, address, phoneNumber);
if(strcmp(firstName, "0") != 0) {
strcpy(Contact->dataPtr->FirstName , firstName);
}
if(strcmp(lastName, "0") != 0) {
strcpy(Contact->dataPtr->LastName , lastName);
}
if(strcmp(address, "0") != 0) {
strcpy(Contact->dataPtr->Address , address);
}
if(strcmp(email, "0") != 0) {
strcpy(Contact->dataPtr->Email , email);
}
if(strcmp(phoneNumber, "0") != 0) {
strcpy(Contact->dataPtr->PhoneNumber , phoneNumber);
}
}
else printf("The contact is not included.\n");
}
void Phone_Book_ListContactData(void) {
char firstNameSearch[21], lastNameSearch[21];
lnode_t *Contact;
printf("Please enter the name number of the contact:");
fflush(stdin);
scanf("%20s %20s",firstNameSearch, lastNameSearch);
Contact = Linked_List_FindNode(ContactList,firstNameSearch, lastNameSearch);
if (Contact != NULL) ContactDisplayFormat(Contact);
else printf("The contact is not included.\n");
}