Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <cstring>
- class Contact {
- private:
- char* name;
- std::string homePhone;
- std::string workPhone;
- std::string mobilePhone;
- std::string additionalInfo;
- public:
- Contact(const char* fullName, const std::string& home, const std::string& work, const std::string& mobile, const std::string& additional) :
- homePhone(home), workPhone(work), mobilePhone(mobile), additionalInfo(additional) {
- name = new char[strlen(fullName) + 1];
- strcpy(name, fullName);
- }
- ~Contact() {
- delete[] name;
- }
- const char* getName() const {
- return name;
- }
- const std::string& getHomePhone() const {
- return homePhone;
- }
- const std::string& getWorkPhone() const {
- return workPhone;
- }
- const std::string& getMobilePhone() const {
- return mobilePhone;
- }
- const std::string& getAdditionalInfo() const {
- return additionalInfo;
- }
- };
- class PhoneBook {
- private:
- std::vector<Contact*> contacts;
- public:
- ~PhoneBook() {
- for (Contact* contact : contacts) {
- delete contact;
- }
- }
- void addContact(Contact* contact) {
- contacts.push_back(contact);
- }
- void removeContact(const char* fullName) {
- for (auto it = contacts.begin(); it != contacts.end(); ++it) {
- if (strcmp((*it)->getName(), fullName) == 0) {
- delete *it;
- contacts.erase(it);
- break;
- }
- }
- }
- const Contact* findContact(const char* fullName) const {
- for (const Contact* contact : contacts) {
- if (strcmp(contact->getName(), fullName) == 0) {
- return contact;
- }
- }
- return nullptr;
- }
- void displayAllContacts() const {
- for (const Contact* contact : contacts) {
- std::cout << "Name: " << contact->getName() << std::endl;
- std::cout << "Home Phone: " << contact->getHomePhone() << std::endl;
- std::cout << "Work Phone: " << contact->getWorkPhone() << std::endl;
- std::cout << "Mobile Phone: " << contact->getMobilePhone() << std::endl;
- std::cout << "Additional Info: " << contact->getAdditionalInfo() << std::endl;
- std::cout << std::endl;
- }
- }
- void saveToFile(const std::string& filename) const {
- std::ofstream file(filename);
- if (file.is_open()) {
- for (const Contact* contact : contacts) {
- file << contact->getName() << std::endl;
- file << contact->getHomePhone() << std::endl;
- file << contact->getWorkPhone() << std::endl;
- file << contact->getMobilePhone() << std::endl;
- file << contact->getAdditionalInfo() << std::endl;
- file << std::endl;
- }
- file.close();
- std::cout << "Phone book saved to file: " << filename << std::endl;
- }
- else {
- std::cout << "Unable to open file: " << filename << std::endl;
- }
- }
- void loadFromFile(const std::string& filename) {
- std::ifstream file(filename);
- if (file.is_open()) {
- // Clear the existing contacts
- for (Contact* contact : contacts) {
- delete contact;
- }
- contacts.clear();
- // Read the contacts from file
- std::string fullName;
- std::string homePhone;
- std::string workPhone;
- std::string mobilePhone;
- std::string additionalInfo;
- while (std::getline(file, fullName)) {
- std::getline(file, homePhone);
- std::getline(file, workPhone);
- std::getline(file, mobilePhone);
- std::getline(file, additionalInfo);
- Contact* contact = new Contact(fullName.c_str(), homePhone, workPhone, mobilePhone, additionalInfo);
- contacts.push_back(contact);
- }
- file.close();
- std::cout << "Phone book loaded from file: " << filename << std::endl;
- }
- else {
- std::cout << "Unable to open file: " << filename << std::endl;
- }
- }
- };
- int main() {
- PhoneBook phoneBook;
- // Добавление абонентов
- Contact* contact1 = new Contact("John Doe", "123456", "789012", "345678", "Friend");
- phoneBook.addContact(contact1);
- Contact* contact2 = new Contact("Jane Smith", "987654", "210987", "654321", "Colleague");
- phoneBook.addContact(contact2);
- // Вывод всех абонентов
- phoneBook.displayAllContacts();
- // Поиск абонента по ФИО
- const char* searchName = "John Doe";
- const Contact* foundContact = phoneBook.findContact(searchName);
- if (foundContact != nullptr) {
- std::cout << "Contact found: " << foundContact->getName() << std::endl;
- std::cout << "Home Phone: " << foundContact->getHomePhone() << std::endl;
- std::cout << "Work Phone: " << foundContact->getWorkPhone() << std::endl;
- std::cout << "Mobile Phone: " << foundContact->getMobilePhone() << std::endl;
- std::cout << "Additional Info: " << foundContact->getAdditionalInfo() << std::endl;
- }
- else {
- std::cout << "Contact not found: " << searchName << std::endl;
- }
- // Удаление абонента
- phoneBook.removeContact("Jane Smith");
- // Сохранение в файл
- phoneBook.saveToFile("phonebook.txt");
- // Загрузка из файла
- phoneBook.loadFromFile("phonebook.txt");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement