-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
347 lines (311 loc) · 8.08 KB
/
Copy pathmain.cpp
File metadata and controls
347 lines (311 loc) · 8.08 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
344
345
346
347
/*
* @Author: MaximilianEdison (MaxianEdison)
* @Date: 2023-06-15 06:39:51
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <limits>
#ifdef _WIN32
#define CLEAR_COMMAND "cls"
#else
#define CLEAR_COMMAND "clear"
#endif
/**
* @brief Contact class.
*
*/
class Contact
{
private:
std::string name;
std::string email;
std::string phone;
public:
Contact(const std::string &name, const std::string &email, const std::string &phone)
: name(name), email(email), phone(phone) {}
const std::string &getName() const
{
return name;
}
const std::string &getEmail() const
{
return email;
}
const std::string &getPhone() const
{
return phone;
}
void display() const
{
std::cout << name << ", " << email << ", " << phone << std::endl;
}
static Contact createFromUserInput()
{
std::string name, email, phone;
while (true)
{
std::cout << "Enter name: ";
std::getline(std::cin, name);
if (!name.empty())
{
break;
}
std::cout << "Name can't be empty. Please try again." << std::endl;
}
while (true)
{
std::cout << "Enter email: ";
std::getline(std::cin, email);
if (ifValidEmail(email))
{
break;
}
std::cout << "Invalid email format. Please try again." << std::endl;
}
while (true)
{
std::cout << "Enter phone number: ";
std::getline(std::cin, phone);
if (isValidPhoneNumber(phone))
{
break;
}
std::cout << "Invalid email format. Please try again." << std::endl;
}
return Contact(name, email, phone);
}
static bool ifValidEmail(const std::string &email)
{
// TODO: write email validator.
return !email.empty();
}
static bool isValidPhoneNumber(const std::string &email)
{
// TODO: write phone validator.
return !email.empty();
}
};
/**
* @brief Clear terminal screen based on OS (Windows or Unix).
*
*/
void clearScreen()
{
std::system(CLEAR_COMMAND);
}
/**
* @brief Display menu for user.
*
*/
void displayMenu()
{
std::cout << "Contact List Manager" << std::endl;
std::cout << "\nSelect and option:" << std::endl;
std::cout << "1. Add new contact" << std::endl;
std::cout << "2. List all contact" << std::endl;
std::cout << "3. Search for a contact" << std::endl;
std::cout << "4. Update a contact" << std::endl;
std::cout << "5. Delete a contact" << std::endl;
std::cout << "6. Quit" << std::endl;
}
/**
* @brief Add contact to contacts.csv file.
*
* @param contacts
*/
void addContact(std::vector<Contact> &contacts)
{
clearScreen();
std::cout << "Add a new Contact" << std::endl;
Contact contact = Contact::createFromUserInput();
contacts.push_back(contact);
std::cout << "\nContact added successfully!" << std::endl;
}
/**
* @brief List all contacts.
*
* @param contacts
*/
void listContacts(const std::vector<Contact> &contacts)
{
clearScreen();
std::cout << "List of all contacts" << std::endl;
for (const Contact &contact : contacts)
{
contact.display();
}
}
/**
* @brief Search for contacts in contacts.csv file.
*
* @param contacts
*/
void searchContact(const std::vector<Contact> &contacts)
{
clearScreen();
std::cout << "Search for contact" << std::endl;
std::string searhTerm;
std::cout << "Enter search term: ";
std::getline(std::cin, searhTerm);
bool found = false;
std::cout << "\nSearch results:" << std::endl;
for (const Contact &contact : contacts)
{
if (contact.getName().find(searhTerm) != std::string::npos ||
contact.getEmail().find(searhTerm) != std::string::npos ||
contact.getPhone().find(searhTerm) != std::string::npos)
{
contact.display();
found = true;
}
}
if (!found)
std::cout << "No matching contacts found." << std::endl;
}
/**
* @brief Update info for one contact.
*
* @param contacts
*/
void updateContact(std::vector<Contact> &contacts)
{
clearScreen();
std::cout << "Update contact" << std::endl;
std::string searchTerm;
std::cout << "Enter the name of the contact to update: ";
std::getline(std::cin, searchTerm);
bool found = false;
for (Contact &contact : contacts)
{
if (contact.getName() == searchTerm)
{
contact = Contact::createFromUserInput();
std::cout << "\nContact updated successfully!" << std::endl;
found = true;
break;
}
}
if (!found)
std::cout << "Contact not found." << std::endl;
}
/**
* @brief Delete contact from contacts.csv file.
*
* @param contacts
*/
void deleteContact(std::vector<Contact> &contacts)
{
clearScreen();
std::cout << "Delete a contact" << std::endl;
std::string searchTerm;
std::cout << "Enter the name of the contact to delete: ";
std::getline(std::cin, searchTerm);
bool found = false;
for (auto it = contacts.begin(); it != contacts.end(); ++it)
{
if (it->getName() == searchTerm)
{
contacts.erase(it);
std::cout << "\nContact deleted successfully!" << std::endl;
found = true;
break;
}
}
if (!found)
std::cout << "Contact not found." << std::endl;
}
/**
* @brief When program exit save contacts to disk.
*
* @param contacts
* @param filename
*/
void saveContacts(const std::vector<Contact> &contacts, const std::string &filename)
{
std::ofstream file(filename);
if (!file)
{
std::cerr << "Error opening file for writing." << std::endl;
return;
}
for (const Contact &contact : contacts)
{
file << contact.getName() << "," << contact.getEmail() << "," << contact.getPhone() << std::endl;
}
file.close();
}
/**
* @brief When programm run load contacts to memory.
*
* @param filename
* @return std::vector<Contact>
*/
std::vector<Contact> loadContacts(const std::string &filename)
{
std::vector<Contact> contacts;
std::ifstream file(filename);
if (!file)
{
std::cerr << "Error opening file for reading." << std::endl;
return contacts;
}
std::string line;
while (std::getline(file, line))
{
size_t pos1 = line.find(",");
size_t pos2 = line.rfind(",");
if (pos1 != std::string::npos && pos2 != std::string::npos)
{
std::string name = line.substr(0, pos1);
std::string email = line.substr(pos1 + 1, pos2 - pos1 - 1);
std::string phone = line.substr(pos2 + 1);
contacts.push_back(Contact(name, email, phone));
}
}
file.close();
return contacts;
}
int main()
{
const std::string filename = "contacts.csv";
std::vector<Contact> contacts = loadContacts(filename);
int choice;
while (true)
{
clearScreen();
displayMenu();
std::cout << "\nEnter your choice: ";
std::cin >> choice;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
switch (choice)
{
case 1:
addContact(contacts);
break;
case 2:
listContacts(contacts);
break;
case 3:
searchContact(contacts);
break;
case 4:
updateContact(contacts);
break;
case 5:
deleteContact(contacts);
break;
case 6:
saveContacts(contacts, filename);
clearScreen();
std::cout << "Goodbye!" << std::endl;
return 0;
default:
std::cout << "Invalid choice. Please try again." << std::endl;
break;
}
std::cout << "\nPress Enter to continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}