-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_contact_manager.cpp
More file actions
343 lines (305 loc) · 9.54 KB
/
simple_contact_manager.cpp
File metadata and controls
343 lines (305 loc) · 9.54 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
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct Node{
string Name;
string Age;
string Id;
string phone_number;
string email_add;
Node *next;
Node(string name="", string number="",string email="", string age=0 , string id=""):Name(name), Age(age), Id(id), phone_number(number),email_add(email), next(nullptr){};
};
bool are_equal(string var, string var2){
return var.compare(var2)==0;
}
class linked_list{
private:
Node *head = nullptr, *tail = nullptr;
size_t length=0;
public:
void add_node(string name,string number, string email, string age, string id){
Node *node = new Node(name,number, email, age, id);
if (!head){
head = tail = node;
}
else{
tail->next = node;
tail = node;
}
length++;
}
void add_to_firstpos(string name, string number, string email, string age, string id){
Node *node = new Node(name,number, email, age, id);
node->next = head;
head = node;
length++;
}
void add_to_lastpos(string name,string number, string email, string age, string id){
Node *node = new Node(name,number, email, age, id);
if(!head)
head = tail = node;
else{
tail->next = node;
tail = node;
}
length++;
}
void add_at_pos (string name,string number, string email, string age, string id,int pos){
Node *node = new Node(name,number, email, age, id);
if(pos < 1 || pos > return_length()+1){
cout<<"Can not be added to this postion, out of limit !!!"<<endl;
return;
}
if(pos == 1){
add_to_firstpos(name,number,email, age, id);
return;
}
if(pos == return_length()+1){
add_to_lastpos(name, number, email, age, id);
return;
}
else{
Node *temp = head;
for(int i = 1; i < pos - 1; ++i){
temp = temp->next;
}
node->next = temp->next;
temp->next = node;
}
length++;
}
void remove_first(){
if(!head){
cout<<"\a No items exist to be cleared"<<endl;
return;
}
Node *del = head;
head = head->next;
delete del;
if(!head)
tail = nullptr;
length--;
cout<<"Item has been removed :)"<<endl;
}
void remove_last(){
if(!head){
cout<<"\a No items exist to be cleared"<<endl;
return;
}
if(head==tail && head != nullptr){
delete head;
head = tail = nullptr;
length = 0;
}else{
Node *temp = head;
while(temp->next != tail)
temp = temp->next;
delete tail;
tail = temp;
tail->next = nullptr;
}
length--;
}
void remove(string name){
if (!head) {
cout << "\aNo items exist to be cleared" << endl;
return;
}
if (are_equal(head->Name, name)) {
remove_first();
return;
}
Node *mov = head;
while (mov->next && !are_equal(mov->next->Name, name)) {
mov = mov->next;
}
if (mov->next) {
Node *del = mov->next;
mov->next = del->next;
if (del == tail) {
tail = mov;
}
delete del;
length--;
cout << "Item has been found and removed from the list." << endl;
}else {
cout << "\aItem with this name is not existing; can't be removed!" << endl;
}
}
void clear_list(){
if(!head){
cout<<"\a No items exist to be cleared"<<endl;
return;
}
Node *temp = head;
while(temp){
Node *curr = temp->next;
delete temp;
temp = curr;
}
head = tail = nullptr;
length = 0;
cout<<"List has been cleared"<<endl;
}
void search_for(string wanted){
if(!head){
cout<<"\a No items exist to search for!"<<endl;
return;
}
Node *mov = head;
while(mov && mov->Name != wanted){
mov = mov->next;
}
if (mov){
cout<<"Item Exits :), and here is its data ----> \n";
cout<<"Item name is: "<< mov->Name<<endl;
cout<<"Item number : "<< mov->phone_number<<"\t\t\t Item email is : "<<mov->email_add<<endl;
cout<<"Item age : " << mov->Age << "\t\t\t Item id : "<<mov->Id<<endl;
}
else
cout<<"\aItem with this name is not existing; can't be removed !"<<endl;
}
int return_length(){
return length;
}
void display_list(){
if(!head)
cout<<"\aNo items here to display !!!!"<<endl;
Node *temp = head;
while (temp){
cout<<"Name : "<<temp->Name<<endl;
cout<<"number is : "<<temp->phone_number<<"\t\t\t\t email is:"<<temp->email_add<<endl;
cout<<"Age : "<<temp->Age << "\t\t\t\t" << "Id : "<<temp->Id <<endl;
temp = temp->next;
}
}
void save_to_file(const string &filename = "contacts.txt") {
ofstream out(filename, ios::app);
if (!out) {
cout << "Error opening file to save!" << endl;
return;
}
Node *temp = head;
while (temp) {
out << temp->Name << "," <<temp->phone_number << "," <<temp->email_add << "," << temp->Age << "," << temp->Id <<"\n";
temp = temp->next;
}
out.close();
cout << "Contacts saved to file." << endl;
}
void load_from_file(const string& filename = "contacts.txt") {
ifstream file(filename);
if (!file) {
cout << "File not found. Starting with empty contact list." << endl;
return;
}
string line, name, number, email, age, id;
while (getline(file, line)) {
if (line.rfind("Name: ", 0) == 0)
name = line.substr(6);
else if (line.rfind("Phone: ", 0) == 0)
number = line.substr(7);
else if (line.rfind("Email: ", 0) == 0)
email = line.substr(7);
else if (line.rfind("Age: ", 0) == 0)
age = line.substr(5);
else if (line.rfind("Id: ", 0) == 0)
id = line.substr(4);
else if (line == "---") {
add_to_lastpos(name, number, email, age, id);
}
}
file.close();
}
~linked_list(){
clear_list();
}
};
int main(){
int choice, pos, insertion;
linked_list ls;
// ls.load_from_file();
string name, number, email, age, id;
while(true){
cout<<"Hello what do you want to do ??"<<endl;
cout<<"1. Add a new user \n";
cout<<"2. Search for a user\n";
cout<<"3. Remove a user \n";
cout<<"4. Display all users\n";
cout<<"5. Clear whole list \n";
cout<<"6. Exit \n";
cout<<"Enter you choice.....";
cin>>choice;
switch (choice){
case 1:
cin.ignore();
cout<<"Enter name: ";
getline(cin,name);
cin.ignore();
cout<<"Enter phone number : ";
cin>>number;
cin.ignore();
cout<<"Enter Email address : ";
cin>>email;
cin.ignore();
cout<<"Enter Age : ";
cin>>age;
cin.ignore();
cout<<"Enter ID : ";
cin>>id;
cout<<"Where to add it ?? (1.first/2.last/3.specific/any) : ";
cin>>pos;
switch (pos)
{
case 1:
ls.add_to_firstpos(name,number,email,age, id);
ls.save_to_file();
break;
case 2:
ls.add_to_lastpos(name, number, email, age, id);
ls.save_to_file();
break;
case 3:
cout<<"Enter Position to insert at: ";
cin>>insertion;
ls.add_at_pos(name,number, email,age, id,insertion);
ls.save_to_file();
break;
default:
ls.add_node(name,number,email, age, id);
ls.save_to_file();
break;
}
break;
case 2:
cin.ignore();
cout<<"Enter name to search for: ";
getline(cin, name);
ls.search_for(name);
break;
case 3:
cin.ignore();
cout<<"Enter name to delete: ";
getline(cin, name);
ls.remove(name);
ls.save_to_file();
break;
case 4:
ls.display_list();
break;
case 5:
ls.clear_list();
ls.save_to_file();
break;
case 6:
exit(0);
break;
default:
cout<<"\aDid not get a valid input !!!\n";
break;
}
}
return 0;
}