-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
390 lines (307 loc) · 10.9 KB
/
main.cpp
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
#include <regex>
#include <string>
#include <iostream>
#include <fstream>
#include <utility>
#include <vector>
using namespace std;
class Contact {
private:
// Private attribute
string firstName, familyName, phoneNumber = "---", emailAddress = "---";
static bool isEmailValid(const string &email) {
// define a regular expression
const regex pattern(R"((\w+)(\.|_)?(\w*)@(\w+)(\.(\w+))+)");
// try to match the string with the regular expression
return regex_match(email, pattern);
}
public:
Contact() = default;
Contact(string firstName, string familyName, string phoneNumber, string emailAddress) {
setFirstName(move(firstName));
setFamilyName(move(familyName));
setPhoneNumber(move(phoneNumber));
setEmailAddress(move(emailAddress));
}
void setFirstName(string firstName) {
this->firstName = firstName;
}
void setFamilyName(string familyName) {
this->familyName = familyName;
}
void setPhoneNumber(string phoneNumber) {
if (phoneNumber == "---") return;
for (char const &c: phoneNumber) {
if (isdigit(c) == 0) {
cout << "Phone number is invalid !" << endl;
return;
}
}
if (phoneNumber.find("+98") != string::npos)
phoneNumber = phoneNumber.replace(0, 3, "0");
if (phoneNumber.length() != 11) {
cout << "Phone number is invalid !" << endl;
return;
}
this->phoneNumber = phoneNumber;
}
void setEmailAddress(string emailAddress) {
if (emailAddress == "---") return;
if (isEmailValid(emailAddress))
this->emailAddress = emailAddress;
else
cout << "Email address is invalid !" << endl;
}
const char *getFirstName() {
return firstName.c_str();
}
const char *getFamilyName() {
return familyName.c_str();
}
const char *getPhoneNumber() {
return phoneNumber.c_str();
}
const char *getEmailAddress() {
return emailAddress.c_str();
}
string toString(bool header = false) {
char buffer[500];
sprintf(buffer, (char *) "%-15s | %-15s | %-15s | %-15s", getFirstName(),
getFamilyName(), getPhoneNumber(), getEmailAddress());
string str = buffer;
if (header) {
char buffer2[500];
sprintf(buffer2, (char *) "%-15s | %-15s | %-15s | %-15s", "First Name",
"Family Name", "Phone Number", "Email Address");
string head = buffer2;
str = head + "\n" + str;
}
return str;
}
};
class Main {
private:
vector<Contact> contacts;
string fileName = "contacts.save";
void addContact() {
Contact contact;
string firstName, familyName, phoneNumber, emailAddress;
cout << "Enter the first name:" << endl;
getline(cin, firstName);
contact.setFirstName(firstName);
cout << "Enter the family name:" << endl;
getline(cin, familyName);
contact.setFamilyName(familyName);
cout << "Enter the phone number:" << endl;
getline(cin, phoneNumber);
contact.setPhoneNumber(phoneNumber);
cout << "Enter the email address:" << endl;
getline(cin, emailAddress);
contact.setEmailAddress(emailAddress);
contacts.push_back(contact);
cout << "\nContact added successfully !\n" << endl;
}
void removeContact() {
if (contacts.size() > 0) {
string index_;
Contact contact;
cout << "Choose the contact you want to delete by entering its number:\n";
showContacts();
cout << ">" << endl;
getline(cin, index_);
try {
int index = stoi(index_);
contact = contacts[index - 1];
contacts.erase(contacts.begin() + index - 1);
cout << contact.getFirstName() << " " << contact.getFamilyName() << " was deleted successfully !\n"
<< endl;
} catch (const std::exception &e) {
cout << "Wrong command !" << endl << endl;
}
} else
cout << "There is not any contact !" << endl << endl;
}
void searchContact() {
if (contacts.size() > 0) {
vector<Contact> find;
string toFind;
cout << "Enter any detail to find any match in contacts:" << endl;
getline(cin, toFind);
for (int i = 0; i < contacts.size(); i++)
if (contacts[i].toString(false).find(toFind) != string::npos)
find.push_back(contacts[i]);
if (find.size() > 0) {
cout << "Matching contacts:\n" << endl;
printf((char *) "-N- | %-15s | %-15s | %-15s | %-15s", "First Name",
"Family Name", "Phone Number", "Email Address");
cout << endl;
for (int i = 0; i < find.size(); i++)
if (find[i].toString(false).find(toFind) != string::npos)
cout << i + 1 << " | " << find[i].toString(false) << endl;
cout << endl;
} else
cout << "No matching contacts where found !" << endl << endl;
} else
cout << "There is not any contact !" << endl << endl;
}
void showContacts(bool header = false) {
if (contacts.size() > 0) {
if (header) {
printf((char *) "-N- | %-15s | %-15s | %-15s | %-15s", "First Name",
"Family Name", "Phone Number", "Email Address");
cout << endl;
}
for (int i = 0; i < contacts.size(); i++) {
printf((char *) "%-4d | %-15s | %-15s | %-15s | %-15s", i + 1, contacts[i].getFirstName(),
contacts[i].getFamilyName(), contacts[i].getPhoneNumber(), contacts[i].getEmailAddress());
cout << endl;
}
cout << endl;
} else
cout << "There is not any contact !" << endl << endl;
}
void save() {
if (contacts.size() > 0) {
ofstream file(fileName);
char buffer[500];
sprintf(buffer, (char *) "%-15s | %-15s | %-15s | %-15s", "First Name",
"Family Name", "Phone Number", "Email Address");
file << buffer << endl;
for (int i = 0; i < contacts.size(); i++) {
file << contacts[i].toString(false) << endl;
}
file.close();
cout << "Contacts were saved !" << endl << endl;
} else
cout << "There is not any contact !" << endl << endl;
}
void load() {
ifstream file(fileName);
int i = 0;
string line;
while (getline(file, line)) {
if (i == 0) {
i++;
continue;
}
vector<string> splits = split(" " + removeSpaces(line) + " ", '|');
Contact newCon = *new Contact(splits[0], splits[1],
splits[2], splits[3]);
contacts.push_back(newCon);
}
file.close();
}
string removeSpaces(string str) {
// n is length of the original string
int n = str.length();
// i points to next position to be filled in
// output string/ j points to next character
// in the original string
int i = 0, j = -1;
// flag that sets to true is space is found
bool spaceFound = false;
// Handles leading spaces
while (++j < n && str[j] == ' ');
// read all characters of original string
while (j < n) {
// if current characters is non-space
if (str[j] != ' ') {
// remove preceding spaces before dot,
// comma & question mark
if ((str[j] == '.' || str[j] == ',' ||
str[j] == '?') && i - 1 >= 0 &&
str[i - 1] == ' ')
str[i - 1] = str[j++];
else
// copy current character at index i
// and increment both i and j
str[i++] = str[j++];
// set space flag to false when any
// non-space character is found
spaceFound = false;
}
// if current character is a space
else if (str[j++] == ' ') {
// If space is encountered for the first
// time after a word, put one space in the
// output and set space flag to true
if (!spaceFound) {
str[i++] = ' ';
spaceFound = true;
}
}
}
// Remove trailing spaces
if (i <= 1)
str.erase(str.begin() + i, str.end());
else
str.erase(str.begin() + i - 1, str.end());
return str;
}
vector<string> split(string str, char char_) {
string temp = "";
vector<string> splits;
for (int i = 0; i < (int) str.size(); i++) {
if (str[i] != char_)
temp += str[i];
else {
splits.push_back(temp.substr(1, temp.length() - 2));
temp = "";
}
}
splits.push_back(temp.substr(1, temp.length() - 2));
return splits;
}
public:
Main() {
load();
cout << "**/ Welcome to PhoneBook \\**" << endl << endl;
cout << ">> Choose the commands bellow:" << endl;
int input;
while (true) {
input = menu();
switch (input) {
case 1:
addContact();
continue;
case 2:
removeContact();
continue;
case 3:
searchContact();
continue;
case 4:
showContacts(true);
continue;
case 5:
save();
continue;
case 6:
break;
default:
cout << "Wrong command !" << endl << endl;
continue;
}
break;
}
cout << "\nHave a good day !" << endl;
}
int menu() {
cout << "1. Add Contact\n2. Remove Contact\n3. Search Contact\n4. Show Contact\n5. Save To File\n6. Exit\n>"
<< endl;
string input_;
getline(cin, input_);
try {
int input = stoi(input_);
if (1 <= input && input <= 6)
return input;
} catch (const std::exception &e) {
return 0;
}
return 0;
}
};
int main() {
Main m;
return 0;
}