#include <iostream>
#include <string>
#include <vector>
using std::string;
using std::vector;
using std::cout;
using std::endl;
using std::cin;
/**
* Класс пользователя
* Хрнанит:
* Имя пользователя
* Id пользователя
*/
class User {
public:
// Конструктор
User(int id, string const &name) {
this->id = id;
this->name = name;
}
int getId() const {
return id;
}
string getName() const {
return name;
}
private:
int id;
string name;
};
/**
* Класс базы пользователей
* Операции:
* Добавление нового пользователя
* Удаление пользователя (По имени или по Id)
* Поиск (По имени или по id)
*/
class DataBase {
public:
/**
* Возвращаем результат добавления
* true - добавили
* false - не добавили
*/
bool add(User const &user) {
// Проверяем на уникальность id
for (auto it = user_list.begin(); it != user_list.end(); ++it) {
if (it->getId() == user.getId()) {
return false;
}
}
user_list.push_back(user);
return true;
}
/**
* Возвращаем результат удаления
* true - удалено
* false - не удалено
*/
bool remove(int id) {
// auto === vector<User>::iterator
for (auto it = user_list.begin(); it != user_list.end(); ++it) {
if (it->getId() == id) {
user_list.erase(it);
return true;
}
}
return false;
}
bool remove(string const &name) {
for (auto it = user_list.begin(); it != user_list.end(); ++it) {
if (it->getName() == name) {
user_list.erase(it);
return true;
}
}
return false;
}
/**
* Возвращаем результат:
* true - Нашлось
* false - Не нашлось
* Второй параметр - результат.
* Т.е. он будет содержать найденного пользователя
*/
bool find(int id, User &result) const {
for (auto it = user_list.begin(); it != user_list.end(); ++it) {
if (it->getId() == id) {
result = *it;
return true;
}
}
return false;
}
bool find(string const &name, User &result) const {
for (auto it = user_list.begin(); it != user_list.end(); ++it) {
if (it->getName() == name) {
result = *it;
return true;
}
}
return false;
}
/**
* Возвращаем всех пользователей
*/
vector<User> getAll() const {
return user_list;
}
private:
vector<User> user_list;
};
int main() {
setlocale(LC_ALL, "Russian");
DataBase db;
int action = 0;
while (action != 7) {
cout << "1 - добавить пользователя" << endl;
cout << "2 - найти пользователя по id" << endl;
cout << "3 - найти пользователя по имени" << endl;
cout << "4 - удалить пользователя по id" << endl;
cout << "5 - удалить пользователя по имени" << endl;
cout << "6 - вывести всех пользователей" << endl;
cout << "7 - выйти" << endl;
cin >> action;
system("cls"); // Отчищаем экран
int id;
string name;
User user(0, "");
if (action == 1 || action == 2 || action == 4) {
cout << "Введите id пользователя" << endl;
cin >> id;
}
if (action == 1 || action == 3 || action == 5) {
cout << "Введите имя пользователя" << endl;
cin >> name;
}
switch (action) {
// Добавляем пользователя
case 1:
if (db.add(User(id, name))) {
cout << "Пользователь добавлен" << endl;
} else {
cout << "Пользователь с таким id уже существует" << endl;
}
break;
// Поиск пользователя по id
case 2:
if (db.find(id, user)) {
cout << "Имя пользователя с id == " << id << ": " << user.getName() << endl;
} else {
cout << "Пользователь с таким id не найден" << endl;
}
break;
// Поиск пользователя по имени
case 3:
if (db.find(name, user)) {
cout << "Id пользователя с именем " << name << ": " << user.getId() << endl;
} else {
cout << "Пользователь с таким именем не найден" << endl;
}
break;
// Удаление пользователя по id
case 4:
if (db.remove(id)) {
cout << "Пользователь удален" << endl;
} else {
cout << "Пользователя с таким id не существует" << endl;
}
break;
// Удаление пользователя по имени
case 5:
if (db.remove(name)) {
cout << "Пользователь удален" << endl;
} else {
cout << "Пользователя с таким именем не существует" << endl;
}
break;
// Вывод всех пользователей
case 6:
{
cout << "Все пользователи:" << endl;
vector<User> users = db.getAll();
for (auto user : users) {
cout << "Id:" << user.getId() << " Name:" << user.getName() << endl;
}
}
}
}
system("pause");
return 0;
}
Comments
0 B
|👍
/👎