Advertisement
plarmi

hwcpp_1_2

Jun 14th, 2023
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cstring>
  5.  
  6. class Contact {
  7. private:
  8.     char* name;
  9.     std::string homePhone;
  10.     std::string workPhone;
  11.     std::string mobilePhone;
  12.     std::string additionalInfo;
  13.  
  14. public:
  15.     Contact(const char* fullName, const std::string& home, const std::string& work, const std::string& mobile, const std::string& additional) :
  16.             homePhone(home), workPhone(work), mobilePhone(mobile), additionalInfo(additional) {
  17.         name = new char[strlen(fullName) + 1];
  18.         strcpy(name, fullName);
  19.     }
  20.  
  21.     ~Contact() {
  22.         delete[] name;
  23.     }
  24.  
  25.     const char* getName() const {
  26.         return name;
  27.     }
  28.  
  29.     const std::string& getHomePhone() const {
  30.         return homePhone;
  31.     }
  32.  
  33.     const std::string& getWorkPhone() const {
  34.         return workPhone;
  35.     }
  36.  
  37.     const std::string& getMobilePhone() const {
  38.         return mobilePhone;
  39.     }
  40.  
  41.     const std::string& getAdditionalInfo() const {
  42.         return additionalInfo;
  43.     }
  44. };
  45.  
  46. class PhoneBook {
  47. private:
  48.     std::vector<Contact*> contacts;
  49.  
  50. public:
  51.     ~PhoneBook() {
  52.         for (Contact* contact : contacts) {
  53.             delete contact;
  54.         }
  55.     }
  56.  
  57.     void addContact(Contact* contact) {
  58.         contacts.push_back(contact);
  59.     }
  60.  
  61.     void removeContact(const char* fullName) {
  62.         for (auto it = contacts.begin(); it != contacts.end(); ++it) {
  63.             if (strcmp((*it)->getName(), fullName) == 0) {
  64.                 delete *it;
  65.                 contacts.erase(it);
  66.                 break;
  67.             }
  68.         }
  69.     }
  70.  
  71.     const Contact* findContact(const char* fullName) const {
  72.         for (const Contact* contact : contacts) {
  73.             if (strcmp(contact->getName(), fullName) == 0) {
  74.                 return contact;
  75.             }
  76.         }
  77.         return nullptr;
  78.     }
  79.  
  80.     void displayAllContacts() const {
  81.         for (const Contact* contact : contacts) {
  82.             std::cout << "Name: " << contact->getName() << std::endl;
  83.             std::cout << "Home Phone: " << contact->getHomePhone() << std::endl;
  84.             std::cout << "Work Phone: " << contact->getWorkPhone() << std::endl;
  85.             std::cout << "Mobile Phone: " << contact->getMobilePhone() << std::endl;
  86.             std::cout << "Additional Info: " << contact->getAdditionalInfo() << std::endl;
  87.             std::cout << std::endl;
  88.         }
  89.     }
  90.  
  91.     void saveToFile(const std::string& filename) const {
  92.         std::ofstream file(filename);
  93.         if (file.is_open()) {
  94.             for (const Contact* contact : contacts) {
  95.                 file << contact->getName() << std::endl;
  96.                 file << contact->getHomePhone() << std::endl;
  97.                 file << contact->getWorkPhone() << std::endl;
  98.                 file << contact->getMobilePhone() << std::endl;
  99.                 file << contact->getAdditionalInfo() << std::endl;
  100.                 file << std::endl;
  101.             }
  102.             file.close();
  103.             std::cout << "Phone book saved to file: " << filename << std::endl;
  104.         }
  105.         else {
  106.             std::cout << "Unable to open file: " << filename << std::endl;
  107.         }
  108.     }
  109.  
  110.     void loadFromFile(const std::string& filename) {
  111.         std::ifstream file(filename);
  112.         if (file.is_open()) {
  113.             // Clear the existing contacts
  114.             for (Contact* contact : contacts) {
  115.                 delete contact;
  116.             }
  117.             contacts.clear();
  118.  
  119.             // Read the contacts from file
  120.             std::string fullName;
  121.             std::string homePhone;
  122.             std::string workPhone;
  123.             std::string mobilePhone;
  124.             std::string additionalInfo;
  125.             while (std::getline(file, fullName)) {
  126.                 std::getline(file, homePhone);
  127.                 std::getline(file, workPhone);
  128.                 std::getline(file, mobilePhone);
  129.                 std::getline(file, additionalInfo);
  130.  
  131.                 Contact* contact = new Contact(fullName.c_str(), homePhone, workPhone, mobilePhone, additionalInfo);
  132.                 contacts.push_back(contact);
  133.             }
  134.  
  135.             file.close();
  136.             std::cout << "Phone book loaded from file: " << filename << std::endl;
  137.         }
  138.         else {
  139.             std::cout << "Unable to open file: " << filename << std::endl;
  140.         }
  141.     }
  142. };
  143.  
  144. int main() {
  145.     PhoneBook phoneBook;
  146.  
  147.     // Добавление абонентов
  148.     Contact* contact1 = new Contact("John Doe", "123456", "789012", "345678", "Friend");
  149.     phoneBook.addContact(contact1);
  150.  
  151.     Contact* contact2 = new Contact("Jane Smith", "987654", "210987", "654321", "Colleague");
  152.     phoneBook.addContact(contact2);
  153.  
  154.     // Вывод всех абонентов
  155.     phoneBook.displayAllContacts();
  156.  
  157.     // Поиск абонента по ФИО
  158.     const char* searchName = "John Doe";
  159.     const Contact* foundContact = phoneBook.findContact(searchName);
  160.     if (foundContact != nullptr) {
  161.         std::cout << "Contact found: " << foundContact->getName() << std::endl;
  162.         std::cout << "Home Phone: " << foundContact->getHomePhone() << std::endl;
  163.         std::cout << "Work Phone: " << foundContact->getWorkPhone() << std::endl;
  164.         std::cout << "Mobile Phone: " << foundContact->getMobilePhone() << std::endl;
  165.         std::cout << "Additional Info: " << foundContact->getAdditionalInfo() << std::endl;
  166.     }
  167.     else {
  168.         std::cout << "Contact not found: " << searchName << std::endl;
  169.     }
  170.  
  171.     // Удаление абонента
  172.     phoneBook.removeContact("Jane Smith");
  173.  
  174.     // Сохранение в файл
  175.     phoneBook.saveToFile("phonebook.txt");
  176.  
  177.     // Загрузка из файла
  178.     phoneBook.loadFromFile("phonebook.txt");
  179.  
  180.     return 0;
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement