-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontactBook.cpp
More file actions
272 lines (240 loc) · 6.02 KB
/
contactBook.cpp
File metadata and controls
272 lines (240 loc) · 6.02 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
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<iomanip>
using namespace std;
//colour code for terminal
void Red () {
cout<<"\033[1;31m";
}
void Yellow () {
cout<<"\033[1;33m";
}
void Blue () {
cout<<"\033[1;34m";
}
void Green () {
cout<<"\033[0;32m";
}
void Purple () {
cout<<"\033[0;35m";
}
void White () {
cout<<"\033[0;37m";
}
void Cyan () {
cout<<"\033[0;36m";
}
void ResetTextColor () {
cout<<"\033[0m";
}
class Contact {
public:
string name;
string phoneNo;
string emailId;
void printContact(){
Purple();
cout<<"____________________________"<<endl;
cout<<"Name :"<<name<<endl;
cout<<"Phone Number :"<<phoneNo<<endl;
cout<<"Email Id :"<<emailId<<endl;
ResetTextColor();
}
};
class contactBook{
private:
vector<Contact> listofcontacts;
protected:
void storeContactInFile();
void readContactFromFile();
public:
void mainMenu();
void addNewContact();
void viewAllContact();
void searchContactByName();
void deleteContactByName();
void updateContact();
};
void contactBook::storeContactInFile(){
ofstream fout("Contact.txt");
if(fout.is_open()){
for(const auto& Contact : listofcontacts){
fout<<Contact.name<<"\n";
fout<<Contact.phoneNo<<"\n";
fout<<Contact.emailId<<"\n";
}
fout.close();
}
else{
Red();
cout<<"File Not Found!"<<endl;
ResetTextColor();
}
}
void contactBook::readContactFromFile(){
ifstream fin("Contact.txt");
if(fin.is_open()){
Contact contact;
listofcontacts.clear();
while(getline(fin,contact.name)){
getline(fin,contact.phoneNo);
getline(fin,contact.emailId);
listofcontacts.push_back(contact);
}
fin.close();
}
else{
Red();
cout<<"File Not Found!"<<endl;
ResetTextColor();
}
}
void contactBook::mainMenu(){
int choice;
readContactFromFile();
cout<<"Welcome to Contact Book ☎︎"<<endl;
do{
Green();
cout<<"_____________________________"<<endl;
cout<<"1. Add New Contact"<<endl;
cout<<"2. View All Contacts"<<endl;
cout<<"3. Search Contact By Name"<<endl;
cout<<"4. Delete Contact By Name"<<endl;
cout<<"5. Update Contact"<<endl;
cout<<"6. Exit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
ResetTextColor();
cin.ignore();
system("cls");
switch(choice){
case 1:
addNewContact();
break;
case 2:
viewAllContact();
break;
case 3:
searchContactByName();
break;
case 4:
deleteContactByName();
break;
case 5:
updateContact();
break;
case 6:
cout<<"Exiting..."<<endl;
storeContactInFile();
break;
default:
Red();
cout<<"Invalid Choice, please try again."<<endl;
ResetTextColor();
}
}while(choice!=6);
}
void contactBook::addNewContact(){
Contact newcontact;
Blue();
cout<<"__________________________"<<endl;
cout<<"Enter Name : ";
getline(cin,newcontact.name);
cout<<"Enter Phone Number : ";
getline(cin,newcontact.phoneNo);
cout<<"Enter EmailId : ";
getline(cin,newcontact.emailId);
ResetTextColor();
listofcontacts.push_back(newcontact);
Green();
cout<<"Contact added successfully!"<<endl;
ResetTextColor();
}
void contactBook::viewAllContact(){
if(listofcontacts.empty()){
Red();
cout<<"No Contacts available! "<<endl;
ResetTextColor();
}
else{
int count = 1;
for(auto& contact : listofcontacts){
Yellow();
cout<<endl;
cout<<"Contact No : "<<setfill('0')<<setw(2)<<count++<<endl;
contact.printContact();
}
}
}
void contactBook :: searchContactByName(){
string name;
cout<<"_____________________________"<<endl;
cout<<"Enter the name to search : ";
getline(cin,name);
bool found = false;
for(auto& Contact : listofcontacts){
if(Contact.name == name){
Contact.printContact();
found = true;
break;
}
}
if(!found){
Red();
cout<<"Contact not found!"<<endl;
ResetTextColor();
}
}
void contactBook :: deleteContactByName(){
string name;
cout<<"_______________________________"<<endl;
cout<<"Enter the name to delete : ";
getline(cin,name);
bool found = false;
for(auto it = listofcontacts.begin();it != listofcontacts.end();++it){
if(it->name == name){
listofcontacts.erase(it);
found = true;
Green();
cout<<"Contact Deleted successfully!"<<endl;
ResetTextColor();
break;
}
}
if(!found){
Red();
cout<<"Contact Not Found!"<<endl;
ResetTextColor();
}
}
void contactBook :: updateContact(){
string name;
cout<<"________________________________"<<endl;
cout<<"Enter the name to update : ";
getline(cin,name);
bool found = false;
for(auto& contact : listofcontacts){
if(contact.name == name){
cout<<"Updating Contact... "<<endl;
cout<<"Enter new PhoneNumber : ";
getline(cin,contact.phoneNo);
cout<<"Enter New EmailID : ";
getline(cin,contact.emailId);
found = true;
cout<<"Contact Updated Successfully!"<<endl;
break;
}
}
if(!found){
Red();
cout<<"Contact Not Found !"<<endl;
ResetTextColor();
}
}
int main(){
contactBook cb;
cb.mainMenu();
return 0;
}