Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<iomanip>
- #include<conio.h>
- #include<string>
- #include<fstream>
- using namespace std;
- class file_not_found : public exception
- {
- public:
- file_not_found(char const* const _Message) : exception(_Message) {}
- };
- class User
- {
- string login;
- string password;
- string* message;
- int length;
- public:
- User(string login = "", string password = "")
- {
- setUserLogin(login);
- setUserPassword(password);
- message = nullptr;
- length = 0;
- }
- ~User()
- {
- if (message != nullptr)
- {
- delete[] message;
- }
- }
- User(const User& user) // copy constructor
- {
- this->login = user.login;
- this->password = user.password;
- if (user.message != nullptr)
- {
- this->length = user.length;
- this->message = new string[user.length];
- for (int i = 0; i < user.length; ++i)
- {
- this->message[i] = user.message[i];
- }
- }
- }
- User& operator=(const User& user) // Copy assignement
- {
- this->login = user.login;
- this->password = user.password;
- if (user.message != nullptr)
- {
- this->length = user.length;
- this->message = new string[user.length];
- for (int i = 0; i < user.length; ++i)
- {
- this->message[i] = user.message[i];
- }
- }
- return *this;
- }
- User(User&& user) // Move constructor
- {
- this->login = user.login;
- this->password = user.password;
- if (user.length != 0)
- {
- this->length = user.length;
- }
- user.message = nullptr;
- }
- User& operator=(User&& user) // Move assignement
- {
- this->login = user.login;
- this->password = user.password;
- if (user.length != 0)
- {
- this->length = user.length;
- }
- user.message = nullptr;
- return *this;
- }
- void setUserLogin(string login) { this->login = login; }
- void setUserPassword(string password) { this->password = password; }
- void setUserMessage(string message)
- {
- int len = message.size();
- if (len > 0)
- {
- string* temp = new string[length + 1];
- for (int i = 0; i < length; i++)
- {
- temp[i] = message[i];
- }
- delete[] this->message;
- this->message = temp;
- this->message[length] = message;
- length++;
- }
- }
- int getUserLength() const { return length; }
- string getUserLogin() const { return login; }
- string getUserPassword() const { return password; }
- string* getUserMessage() const { return message; }
- };
- class UserManager
- {
- User* registration = nullptr;
- unsigned int size = 0;
- unsigned int position = 0;
- string login;
- string password;
- void getVariantInfo()
- {
- if (size != 0)
- {
- int ndx = 0;
- for (int i = 0; i < size; i++)
- {
- if (this->login == registration[i].getUserLogin() &&
- this->password == registration[i].getUserPassword())
- {
- continue;
- ndx -= 1;
- }
- else
- {
- cout << i + ndx << ". " << registration[i].getUserLogin() << "\n";
- }
- }
- }
- else
- {
- throw exception("Error! There is no persons in the system!\n");
- }
- }
- void getUser(int index)
- {
- if (index >= 0 and index < size)
- {
- cout << registration[index].getUserLogin() << "\n\n";
- }
- else
- {
- throw exception("Error! Incorrect index!\n");
- }
- }
- public:
- ~UserManager()
- {
- if (registration != nullptr)
- {
- delete[] registration;
- }
- }
- UserManager()
- {
- }
- int getSize() const { return size; }
- string getUserManagerLogin() const { return login; }
- string getUserManagerPassword() const { return password; }
- UserManager(const UserManager& userManager) // copy constructor
- {
- this->size = userManager.size;
- this->position = userManager.position;
- this->registration = new User[userManager.size];
- for (int i = 0; i < userManager.size; ++i)
- {
- registration[i] = userManager.registration[i];
- }
- }
- UserManager& operator=(const UserManager& userManager) // Copy assignement
- {
- this->size = userManager.size;
- this->position = userManager.position;
- this->registration = new User[userManager.size];
- for (int i = 0; i < userManager.size; ++i)
- {
- registration[i] = userManager.registration[i];
- }
- return *this;
- }
- UserManager(UserManager&& userManager) // Move constructor
- {
- this->size = userManager.size;
- this->position = userManager.position;
- userManager.registration = nullptr;
- }
- UserManager& operator=(UserManager&& userManager) // Move assignement
- {
- this->size = userManager.size;
- this->position = userManager.position;
- userManager.registration = nullptr;
- return *this;
- }
- User* getRegistration() { return registration; }
- void setRegistration(string login = "", string password = "")
- {
- User* temp = new User[size + 1];
- for (int i = 0; i < size; ++i)
- {
- temp[i] = registration[i];
- }
- delete[] registration;
- registration = temp;
- registration[size].setUserLogin(login);
- registration[size].setUserPassword(password);
- size++;
- }
- void authenticate(string login = "", string password = "")
- {
- bool search = false;
- for (int i = 0; i < size; i++)
- {
- if (login == registration[i].getUserLogin() && password == registration[i].getUserPassword())
- {
- this->login = login;
- this->password = password;
- position = i;
- getVariantInfo();
- search = true;
- }
- }
- if (search == false)
- {
- throw exception("Error! There is no such person in the system!");
- }
- }
- void sendMessage(int& index)
- {
- ofstream fout("text.txt");
- if (fout.is_open())
- {
- string message;
- getline(cin, message);
- fout << message;
- if (index >= 0 && index < size)
- {
- registration[index].setUserMessage(message);
- }
- else
- {
- throw exception("Error! Incorrect index!");
- }
- fout.close();
- }
- else
- {
- throw file_not_found("Error! File not found!");
- }
- }
- void readMessage()
- {
- ifstream fin("text.txt");
- if (fin.is_open())
- {
- string temp;
- for (int i = 0; i < registration[position].getUserLength(); i++)
- {
- temp = registration[position].getUserMessage()[i];
- getline(fin, temp);
- fin >> temp;
- cout << temp << endl;
- fin.close();
- }
- }
- else
- {
- throw file_not_found("Error! File not found!");
- }
- }
- };
- int main()
- {
- UserManager user;
- string login, password;
- char key, choose;
- int index = 0, count = 0;
- while (true)
- {
- system("cls");
- cout << "1. Registration\n";
- cout << "2. Authenticate\n\n";
- key = _getch();
- system("cls");
- switch (key)
- {
- case '1':
- cout << "Registration!\n\n";
- cout << "Enter login: ";
- getline(cin, login);
- cout << "Enter password: ";
- getline(cin, password);
- user.setRegistration(login, password);
- break;
- case '2':
- try
- {
- cout << "Authenticate!\n\n";
- cout << "Enter login: ";
- getline(cin, login);
- cout << "Enter password: ";
- getline(cin, password);
- system("cls");
- cout << "Users:\n";
- user.authenticate(login, password);
- cout << "\n\nAction:\n";
- cout << "1. Send message(enter user's number)\n";
- cout << "2. Read messages\n";
- cout << "3. Exit\n\n";
- choose = _getch();
- system("cls");
- switch (choose)
- {
- case '1':
- cout << "Enter user's number: ";
- cin >> index;
- cin.ignore();
- index -= 1;
- for (int i = 0; i < user.getSize(); i++)
- {
- if (user.getRegistration[i].getUserLogin() == user.getUserManagerLogin() &&
- user.getRegistration[i].getUserPassword() == user.getUserManagerPassword())
- {
- index = i + 1;
- }
- }
- cout << "Enter message:\n\t";
- user.sendMessage(index);
- break;
- case '2':
- cout << "Text: ";
- user.readMessage();
- break;
- case '3':
- break;
- default:
- cout << "Error! Incorrectly number!" << endl;
- break;
- }
- }
- catch (const std::exception& ex)
- {
- cout << ex.what() << endl;
- }
- break;
- default:
- cout << "Error! Incorrectly number!" << endl;
- break;
- }
- system("pause");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment