Guest User

Untitled

a guest
Jul 26th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.92 KB | None | 0 0
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<conio.h>
  4. #include<string>
  5. #include<fstream>
  6. using namespace std;
  7.  
  8. class file_not_found : public exception
  9. {
  10. public:
  11.     file_not_found(char const* const _Message) : exception(_Message) {}
  12. };
  13.  
  14. class User
  15. {
  16.     string login;
  17.     string password;
  18.     string* message;
  19.     int length;
  20. public:
  21.  
  22.     User(string login = "", string password = "")
  23.     {
  24.         setUserLogin(login);
  25.         setUserPassword(password);
  26.         message = nullptr;
  27.         length = 0;
  28.     }
  29.  
  30.     ~User()
  31.     {
  32.         if (message != nullptr)
  33.         {
  34.             delete[] message;
  35.         }
  36.     }
  37.  
  38.     User(const User& user) // copy constructor
  39.     {
  40.         this->login = user.login;
  41.         this->password = user.password;
  42.         if (user.message != nullptr)
  43.         {
  44.             this->length = user.length;
  45.             this->message = new string[user.length];
  46.             for (int i = 0; i < user.length; ++i)
  47.             {
  48.                 this->message[i] = user.message[i];
  49.             }
  50.         }
  51.     }
  52.  
  53.     User& operator=(const User& user) // Copy assignement
  54.     {
  55.         this->login = user.login;
  56.         this->password = user.password;
  57.         if (user.message != nullptr)
  58.         {
  59.             this->length = user.length;
  60.             this->message = new string[user.length];
  61.             for (int i = 0; i < user.length; ++i)
  62.             {
  63.                 this->message[i] = user.message[i];
  64.             }
  65.         }
  66.         return *this;
  67.     }
  68.  
  69.     User(User&& user) // Move constructor
  70.     {
  71.         this->login = user.login;
  72.         this->password = user.password;
  73.         if (user.length != 0)
  74.         {
  75.             this->length = user.length;
  76.         }
  77.         user.message = nullptr;
  78.     }
  79.  
  80.     User& operator=(User&& user) // Move assignement
  81.     {
  82.         this->login = user.login;
  83.         this->password = user.password;
  84.         if (user.length != 0)
  85.         {
  86.             this->length = user.length;
  87.         }
  88.         user.message = nullptr;
  89.         return *this;
  90.     }
  91.  
  92.     void setUserLogin(string login) { this->login = login; }
  93.     void setUserPassword(string password) { this->password = password; }
  94.     void setUserMessage(string message)
  95.     {
  96.         int len = message.size();
  97.         if (len > 0)
  98.         {
  99.             string* temp = new string[length + 1];
  100.             for (int i = 0; i < length; i++)
  101.             {
  102.                 temp[i] = message[i];
  103.             }
  104.             delete[] this->message;
  105.             this->message = temp;
  106.             this->message[length] = message;
  107.             length++;
  108.         }
  109.     }
  110.     int getUserLength() const { return length; }
  111.     string getUserLogin() const { return login; }
  112.     string getUserPassword() const { return password; }
  113.     string* getUserMessage() const { return message; }
  114. };
  115.  
  116. class UserManager
  117. {
  118.     User* registration = nullptr;
  119.     unsigned int size = 0;
  120.     unsigned int position = 0;
  121.     string login;
  122.     string password;
  123.  
  124.     void getVariantInfo()
  125.     {
  126.         if (size != 0)
  127.         {
  128.             int ndx = 0;
  129.             for (int i = 0; i < size; i++)
  130.             {
  131.                 if (this->login == registration[i].getUserLogin() &&
  132.                     this->password == registration[i].getUserPassword())
  133.                 {
  134.                     continue;
  135.                     ndx -= 1;
  136.                 }
  137.                 else
  138.                 {
  139.                     cout << i + ndx << ". " << registration[i].getUserLogin() << "\n";
  140.                 }
  141.             }
  142.         }
  143.         else
  144.         {
  145.             throw exception("Error! There is no persons in the system!\n");
  146.         }
  147.     }
  148.  
  149.     void getUser(int index)
  150.     {
  151.         if (index >= 0 and index < size)
  152.         {
  153.             cout << registration[index].getUserLogin() << "\n\n";
  154.         }
  155.         else
  156.         {
  157.             throw exception("Error! Incorrect index!\n");
  158.         }
  159.     }
  160.  
  161. public:
  162.  
  163.     ~UserManager()
  164.     {
  165.         if (registration != nullptr)
  166.         {
  167.             delete[] registration;
  168.         }
  169.     }
  170.  
  171.     UserManager()
  172.     {
  173.  
  174.     }
  175.  
  176.     int getSize() const { return size; }
  177.  
  178.     string getUserManagerLogin() const { return login; }
  179.  
  180.     string getUserManagerPassword() const { return password; }
  181.  
  182.     UserManager(const UserManager& userManager) // copy constructor
  183.     {
  184.         this->size = userManager.size;
  185.         this->position = userManager.position;
  186.         this->registration = new User[userManager.size];
  187.         for (int i = 0; i < userManager.size; ++i)
  188.         {
  189.             registration[i] = userManager.registration[i];
  190.         }
  191.     }
  192.  
  193.     UserManager& operator=(const UserManager& userManager) // Copy assignement
  194.     {
  195.         this->size = userManager.size;
  196.         this->position = userManager.position;
  197.         this->registration = new User[userManager.size];
  198.         for (int i = 0; i < userManager.size; ++i)
  199.         {
  200.             registration[i] = userManager.registration[i];
  201.         }
  202.         return *this;
  203.     }
  204.  
  205.     UserManager(UserManager&& userManager) // Move constructor
  206.     {
  207.         this->size = userManager.size;
  208.         this->position = userManager.position;
  209.         userManager.registration = nullptr;
  210.     }
  211.  
  212.     UserManager& operator=(UserManager&& userManager) // Move assignement
  213.     {
  214.         this->size = userManager.size;
  215.         this->position = userManager.position;
  216.         userManager.registration = nullptr;
  217.         return *this;
  218.     }
  219.  
  220.     User* getRegistration() { return registration; }
  221.  
  222.     void setRegistration(string login = "", string password = "")
  223.     {
  224.         User* temp = new User[size + 1];
  225.         for (int i = 0; i < size; ++i)
  226.         {
  227.             temp[i] = registration[i];
  228.         }
  229.         delete[] registration;
  230.         registration = temp;
  231.         registration[size].setUserLogin(login);
  232.         registration[size].setUserPassword(password);
  233.         size++;
  234.     }
  235.  
  236.     void authenticate(string login = "", string password = "")
  237.     {
  238.         bool search = false;
  239.         for (int i = 0; i < size; i++)
  240.         {
  241.             if (login == registration[i].getUserLogin() && password == registration[i].getUserPassword())
  242.             {
  243.                 this->login = login;
  244.                 this->password = password;
  245.                 position = i;
  246.                 getVariantInfo();
  247.                 search = true;
  248.             }
  249.         }
  250.         if (search == false)
  251.         {
  252.             throw exception("Error! There is no such person in the system!");
  253.         }
  254.     }
  255.  
  256.     void sendMessage(int& index)
  257.     {
  258.         ofstream fout("text.txt");
  259.         if (fout.is_open())
  260.         {
  261.             string message;
  262.             getline(cin, message);
  263.             fout << message;
  264.             if (index >= 0 && index < size)
  265.             {
  266.                 registration[index].setUserMessage(message);
  267.             }
  268.             else
  269.             {
  270.                 throw exception("Error! Incorrect index!");
  271.             }
  272.             fout.close();
  273.         }
  274.         else
  275.         {
  276.             throw file_not_found("Error! File not found!");
  277.         }
  278.     }
  279.  
  280.     void readMessage()
  281.     {
  282.         ifstream fin("text.txt");
  283.         if (fin.is_open())
  284.         {
  285.             string temp;
  286.             for (int i = 0; i < registration[position].getUserLength(); i++)
  287.             {
  288.                 temp = registration[position].getUserMessage()[i];
  289.                 getline(fin, temp);
  290.                 fin >> temp;
  291.                 cout << temp << endl;
  292.                 fin.close();
  293.             }
  294.         }
  295.         else
  296.         {
  297.             throw file_not_found("Error! File not found!");
  298.         }
  299.     }
  300.  
  301. };
  302.  
  303.  
  304. int main()
  305. {
  306.     UserManager user;
  307.     string login, password;
  308.     char key, choose;
  309.     int index = 0, count = 0;
  310.  
  311.     while (true)
  312.     {
  313.         system("cls");
  314.         cout << "1. Registration\n";
  315.         cout << "2. Authenticate\n\n";
  316.  
  317.         key = _getch();
  318.  
  319.         system("cls");
  320.         switch (key)
  321.         {
  322.         case '1':
  323.             cout << "Registration!\n\n";
  324.             cout << "Enter login: ";
  325.             getline(cin, login);
  326.             cout << "Enter password: ";
  327.             getline(cin, password);
  328.             user.setRegistration(login, password);
  329.             break;
  330.         case '2':
  331.             try
  332.             {
  333.                 cout << "Authenticate!\n\n";
  334.                 cout << "Enter login: ";
  335.                 getline(cin, login);
  336.                 cout << "Enter password: ";
  337.                 getline(cin, password);
  338.                 system("cls");
  339.                 cout << "Users:\n";
  340.                 user.authenticate(login, password);
  341.                 cout << "\n\nAction:\n";
  342.                 cout << "1. Send message(enter user's number)\n";
  343.                 cout << "2. Read messages\n";
  344.                 cout << "3. Exit\n\n";
  345.  
  346.                 choose = _getch();
  347.                 system("cls");
  348.                 switch (choose)
  349.                 {
  350.                 case '1':
  351.                     cout << "Enter user's number: ";
  352.                     cin >> index;
  353.                     cin.ignore();
  354.                     index -= 1;
  355.                     for (int i = 0; i < user.getSize(); i++)
  356.                     {
  357.                         if (user.getRegistration[i].getUserLogin() == user.getUserManagerLogin() &&
  358.                             user.getRegistration[i].getUserPassword() == user.getUserManagerPassword())
  359.                         {
  360.                             index = i + 1;
  361.                         }
  362.                     }
  363.                     cout << "Enter message:\n\t";
  364.                     user.sendMessage(index);
  365.                     break;
  366.                 case '2':
  367.                     cout << "Text: ";
  368.                     user.readMessage();
  369.                     break;
  370.                 case '3':
  371.                     break;
  372.                 default:
  373.                     cout << "Error! Incorrectly number!" << endl;
  374.                     break;
  375.                 }
  376.  
  377.             }
  378.             catch (const std::exception& ex)
  379.             {
  380.                 cout << ex.what() << endl;
  381.             }
  382.             break;
  383.         default:
  384.             cout << "Error! Incorrectly number!" << endl;
  385.             break;
  386.         }
  387.  
  388.  
  389.  
  390.  
  391.         system("pause");
  392.     }
  393.  
  394.  
  395.  
  396.  
  397.  
  398.     return 0;
  399. }
Advertisement
Add Comment
Please, Sign In to add comment