Advertisement
Savonarolla

Untitled

Oct 30th, 2020
1,849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. /* Создать класс «Телефонный справочник», хранящий информацию о
  10. контактах со следующими полями:
  11. − id;
  12. − имя;
  13. − фамилия;
  14. − адрес;
  15. − номер телефона.
  16. Реализовать следующие возможности:
  17. − добавление записи;
  18. − удаление записи;
  19. − сохранение в файл(перезапись);
  20. − загрузка из файла. */
  21.  
  22.  
  23. class Phone
  24.     {
  25.         string id;
  26.         string firstName;
  27.         string lastName;
  28.         string address;
  29.         string cellPhone;
  30.  
  31.     public:
  32.  
  33.         Phone(){};
  34.  
  35.         Phone (string iD_, string FirstName, string LastName, string Address, string CellPhone)
  36.             {
  37.                 id = iD_;
  38.                 firstName = FirstName;
  39.                 lastName = LastName;
  40.                 address = Address;
  41.                 cellPhone = CellPhone;
  42.             }
  43.         ~Phone ()
  44.             {
  45.                 delete this;
  46.             }
  47.  
  48.         string GetID ()
  49.             {
  50.                 return id;
  51.             }
  52.  
  53.         string GetFirstName()
  54.             {
  55.                 return firstName;
  56.             }
  57.  
  58.         string GetLastName()
  59.             {
  60.                 return lastName;
  61.             }
  62.         string GetAddress()
  63.             {
  64.                 return address;
  65.             }
  66.         string GetPhone()
  67.             {
  68.                 return cellPhone;
  69.             }
  70.  
  71.         void setID (string ID)
  72.             {
  73.                 id = ID;
  74.             }
  75.  
  76.         void setFirstName(string FirstName)
  77.             {
  78.                 firstName = FirstName;
  79.             }
  80.  
  81.         void setLastName(string LastName)
  82.             {
  83.                 lastName = LastName;
  84.             }
  85.         void setAddress(string Address)
  86.             {
  87.                 address = Address;
  88.             }
  89.         void setPhone(string Phone)
  90.             {
  91.                 cellPhone = Phone;
  92.             }
  93.  
  94.         void showPhone()
  95.             {
  96.                 cout << GetID() << "\n" << GetFirstName() << "\n" << GetLastName() << "\n" << GetAddress()  << "\n" << GetPhone() << endl;
  97.             }
  98.     };
  99.  
  100. void addPhone (vector <Phone*> &book)
  101.     {
  102.         string newID, newFirstName, newLastName, newAddress, newPhoneNumber;
  103.         // getline здесь для практики. Так, было бы более логично использовать обычный cin.
  104.         // Но адресс скорее всего будет вмещать в себе много разных элементов. Поэтому я решил сделать вот так.
  105.  
  106.         cout << "ID: ";
  107.         getline(cin,newID);
  108.         cout << "FirstName: ";
  109.         getline(cin,newFirstName);
  110.         cout << "LastName: ";
  111.         getline(cin,newLastName);
  112.         cout << "Address: ";
  113.         getline(cin,newAddress);
  114.         cout << "PhoneNumber: ";
  115.         getline(cin,newPhoneNumber);
  116.    //     cin >> newFirstName >> newLastName >> newAddress >> newPhoneNumber;
  117.         Phone* newPhone = new Phone(newID,newFirstName, newLastName, newAddress, newPhoneNumber);
  118.         // Phone newPhone(newFirstName, newLastName, newAddress, newPhoneNumber);
  119.  
  120.         book.push_back(newPhone);
  121.         // book.push_back({newFirstName, newLastName, newAddress, newPhoneNumber});
  122.     }
  123.  
  124.  
  125. void deletePhone (vector <Phone*> &book, int pos)
  126.     {
  127.         book.erase(book.begin()+pos);
  128.     }
  129.  
  130. void saveFile (vector <Phone*> &book,string path)
  131.     {
  132.         ofstream ofs;
  133.         ofs.open(path);
  134.  
  135.         if (!ofs.is_open())
  136.             {
  137.                 cerr << "Can't open the file " << endl;
  138.             }
  139.         else if (ofs.is_open())
  140.             {
  141.                 for (int i = 0; i < book.size(); i++)
  142.                     {
  143.                         string temp;
  144.                         temp = book[i]->GetID();
  145.                         ofs << temp << "\n";
  146.                         temp = book[i]->GetFirstName();
  147.                         ofs << temp << "\n";
  148.                         temp = book[i]->GetLastName();
  149.                         ofs << temp << "\n";
  150.                         temp = book[i]->GetAddress();
  151.                         ofs << temp << "\n";
  152.                         temp = book[i]->GetPhone();
  153.                         ofs << temp << "\n";
  154.                     }
  155.             }
  156.         ofs.close();
  157.     }
  158.  
  159. void resetArr (bool array[], int pos)
  160.     {
  161.         for (int i = 0; i < 5; i++)
  162.             {
  163.                 if (i == pos)
  164.                     {
  165.                         array[pos] = true;
  166.                     }
  167.                 else
  168.                     {
  169.                         array[i] = false;
  170.                     }
  171.             }
  172.  
  173.     }
  174. void loadFile (vector <Phone*> &book, string path)
  175.     {
  176.         ifstream ifs;
  177.         ifs.open(path);
  178.  
  179.         string newString, newID,newFirstName, newLastName, newAddress, newPhoneNumber;
  180.         bool arr[5];
  181.         arr[0] = true;
  182.  
  183.         if (!ifs.is_open())
  184.             {
  185.                 cerr << "Can't open the file " << endl;
  186.             }
  187.         else if (ifs.is_open())
  188.             {
  189.                 book.clear();
  190.                 while (getline(ifs, newString))
  191.                     {
  192.                         if (arr[0])
  193.                             {
  194.                                 newID = newString;
  195.                                 resetArr(arr, 1);
  196.                                 newString.clear();
  197.                                 continue;
  198.                             }
  199.                         if (arr[1])
  200.                             {
  201.                                 newFirstName = newString;
  202.                                 resetArr(arr, 2);
  203.                                 newString.clear();
  204.                                 continue;
  205.                             }
  206.                         if (arr[2])
  207.                             {
  208.                                 newLastName = newString;
  209.                                 resetArr(arr, 3);
  210.                                 newString.clear();
  211.                                 continue;
  212.                             }
  213.                         if (arr[3])
  214.                             {
  215.                                 newAddress = newString;
  216.                                 resetArr(arr, 4);
  217.                                 newString.clear();
  218.                                 continue;
  219.                             }
  220.                         if (arr[4])
  221.                             {
  222.                                 newPhoneNumber = newString;
  223.                                 resetArr(arr, 0);
  224.                                 Phone* newPhone = new Phone(newID,newFirstName, newLastName, newAddress, newPhoneNumber);
  225.                                 book.push_back(newPhone);
  226.                                 newString.clear();
  227.                                 continue;
  228.                             }
  229.                     }
  230.  
  231.  
  232.  
  233.                 /*while (getline(ifs, test))
  234.                     {
  235.                       counter++;
  236.                     }
  237.                 for (int i = 0, j = 0; i < counter; i++)
  238.                     {
  239.                         getline(ifs, newString);
  240.                         if (j == 0)
  241.                             {
  242.                                newID = newString;
  243.                                j++;
  244.                             }
  245.                         if (j == 1)
  246.                             {
  247.                                 newFirstName = newString;
  248.                                 j++;
  249.                             }
  250.                         if (j == 2)
  251.                             {
  252.                                 newLastName = newString;
  253.                                 j++;
  254.                             }
  255.                         if (j == 3)
  256.                             {
  257.                                 newAddress = newString;
  258.                                 j++;
  259.                             }
  260.                         if (j == 4)
  261.                             {
  262.                                 newPhoneNumber = newString;
  263.                                 j++;
  264.                                 Phone* newPhone = new Phone(newID,newFirstName, newLastName, newAddress, newPhoneNumber);
  265.                                 book.push_back(newPhone);
  266.                             }
  267.                     }*/
  268.                 /*bool arr [5];
  269.                 while(ifs.get(ch))
  270.                     {
  271.                         if (ch != '\n' && !arr[0])
  272.                             {
  273.                                 newID += ch;
  274.                                 arr[0] = true;
  275.                             }
  276.                         while (ch != '\n' && !arr[1])
  277.                             {
  278.                                 newFirstName += ch;
  279.                                 arr[1] = true;
  280.                             }
  281.                         while (ch != '\n' && !arr[2])
  282.                             {
  283.                                 newLastName += ch;
  284.                                 arr[2] = true;
  285.                             }
  286.                         while (ch != '\n' && !arr[3])
  287.                             {
  288.                                 newAddress += ch;
  289.                                 arr[3] = true;
  290.                             }
  291.                         while (ch != '\n' && !arr[4])
  292.                             {
  293.                                 newPhoneNumber += ch;
  294.                                 arr[4] = true;
  295.                             }
  296.                     }*/
  297.  
  298.  
  299.  
  300.             }
  301.         ifs.close();
  302.     }
  303.  
  304.  
  305. int main()
  306.     {
  307.     string path = "test.txt";
  308.  
  309.     vector<Phone*> newBook;
  310.     addPhone(newBook);
  311.     // cout << newBook.size() << endl;
  312.     newBook[0]->showPhone();
  313.     // cout << newBook[0] << endl;
  314.  
  315.     /*deletePhone(newBook,0);
  316.     cout << newBook.size() << endl;*/
  317.     saveFile(newBook,path);
  318.  
  319.     addPhone(newBook);
  320.     newBook[1]->showPhone();
  321.     saveFile(newBook,path);
  322.  
  323.     vector<Phone*> testBook;
  324.  
  325.     loadFile(testBook,path);
  326.  
  327.     cout << endl;
  328.  
  329.     testBook[0]->showPhone();
  330.  
  331.     cout << endl;
  332.     testBook[1]->showPhone();
  333.     deletePhone(testBook,0);
  334.     cout << "Size: " << testBook.size() << endl;
  335.    
  336.     }
  337.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement