You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.7 KiB
55 lines
1.7 KiB
#include "ContactManager.h"
|
|
#include "Contact.h"
|
|
|
|
ContactManager::ContactManager() {}
|
|
|
|
void ContactManager::addContact(const std::string& name, const std::string& phone, const std::string& email) {
|
|
contacts.push_back(Contact(name, phone, email));
|
|
}
|
|
|
|
void ContactManager::removeContact(const std::string& name) {
|
|
contacts.erase(std::remove_if(contacts.begin(), contacts.end(), [&name](const Contact& contact) {
|
|
return contact.name == name;
|
|
}), contacts.end());
|
|
}
|
|
|
|
void ContactManager::displayContacts() {
|
|
for (const auto& contact : contacts) {
|
|
std::cout << contact.name << ", " << contact.phone << ", " << contact.email << std::endl;
|
|
}
|
|
}
|
|
|
|
void ContactManager::saveToFile(const std::string& filename) {
|
|
std::ofstream file(filename);
|
|
if (file.is_open()) {
|
|
for (const auto& contact : contacts) {
|
|
file << contact.name << ", " << contact.phone << ", " << contact.email << std::endl;
|
|
}
|
|
file.close();
|
|
} else {
|
|
std::cerr << "Unable to open file." << std::endl;
|
|
}
|
|
}
|
|
|
|
void ContactManager::loadFromFile(const std::string& filename) {
|
|
contacts.clear();
|
|
std::ifstream file(filename);
|
|
if (file.is_open()) {
|
|
std::string line;
|
|
while (std::getline(file, line)) {
|
|
size_t pos = line.find(", ");
|
|
std::string name = line.substr(0, pos);
|
|
line.erase(0, pos + 2);
|
|
|
|
pos = line.find(", ");
|
|
std::string phone = line.substr(0, pos);
|
|
std::string email = line.substr(pos + 2);
|
|
|
|
addContact(name, phone, email);
|
|
}
|
|
file.close();
|
|
} else {
|
|
std::cerr << "Unable to open file." << std::endl;
|
|
}
|
|
}
|