С++
Завдання
Створіть додаток «Телефонна книга». Необхідно зберігати дані про абонента (ПІБ, домашній телефон, робочий
телефон, мобільний телефон, додаткова інформація про
контакт) всередині відповідного класу. Наповніть клас
змінними-членами, функціями-членами, конструкторами,
inline-функціями-членами, використовуйте ініціалізатор,
реалізуйте деструктор. Обов’язково необхідно виділяти
динамічно пам’ять під ПІБ. Надайте користувачеві можливість додавати нових абонентів, видаляти абонентів,
шукати абонентів за ПІБ, показувати всіх абонентів, зберігати інформацію в файл і завантажувати з файлу.
Ответы
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class Abonent {
public:
Abonent(const string& name, const string& homePhone, const string& workPhone, const string& mobilePhone, const string& additionalInfo) :
m_name(new string(name)),
m_homePhone(homePhone),
m_workPhone(workPhone),
m_mobilePhone(mobilePhone),
m_additionalInfo(additionalInfo)
{}
Abonent(const Abonent& other) :
m_name(new string(*other.m_name)),
m_homePhone(other.m_homePhone),
m_workPhone(other.m_workPhone),
m_mobilePhone(other.m_mobilePhone),
m_additionalInfo(other.m_additionalInfo)
{}
Abonent& operator=(const Abonent& other) {
if (this != &other) {
delete m_name;
m_name = new string(*other.m_name);
m_homePhone = other.m_homePhone;
m_workPhone = other.m_workPhone;
m_mobilePhone = other.m_mobilePhone;
m_additionalInfo = other.m_additionalInfo;
}
return *this;
}
~Abonent() {
delete m_name;
}
const string&getName() const {
return *m_name;
}
const string& getHomePhone() const {
return m_homePhone;
}
const string& getWorkPhone() const {
return m_workPhone;
}
const string& getMobilePhone() const {
return m_mobilePhone;
}
const string& getAdditionalInfo() const {
return m_additionalInfo;
}
private:
string* m_name;
string m_homePhone;
string m_workPhone;
string m_mobilePhone;
string m_additionalInfo;
};
class PhoneBook {
public:
PhoneBook() {}
PhoneBook(const PhoneBook& other) :
m_abonents(other.m_abonents)
{}
PhoneBook& operator=(const PhoneBook& other) {
if (this != &other) {
m_abonents = other.m_abonents;
}
return *this;
}
~PhoneBook() {}
void addAbonent(const string& name, const string& homePhone, const string& workPhone, const string& mobilePhone, const string& additionalInfo) {
Abonent abonent(name, homePhone, workPhone, mobilePhone, additionalInfo);
m_abonents.push_back(abonent);
}
void removeAbonent(const string& name) {
for (auto it = m_abonents.begin(); it != m_abonents.end(); ++it) {
if (it->getName() == name) {
m_abonents.erase(it);
return;
}
}
}
Abonent* findAbonent(const string& name) {
for (auto& abonent : m_abonents) {
if (abonent.getName() == name) {
return &abonent;
}
}
return nullptr;
}
void showAllAbonents() const {
for (const auto& abonent : m_abonents) {
cout << "Name: " << abonent.getName() << endl;
cout << "Home Phone: " << abonent.getHomePhone() << endl;
cout << "Work Phone: " << abonent.getWorkPhone() << endl;
cout << "Mobile Phone: " << abonent.getMobilePhone() << endl;
cout << "Additional Info: " << abonent.getAdditionalInfo() << endl << endl;
}
}
void saveToFile(const string& filename) const {
ofstream outFile(filename);
if (outFile.is_open()) {
for (const auto& abonent : m_abonents) {
outFile << abonent.getName() << endl;
outFile << abonent.getHomePhone() << endl;
outFile << abonent.getWorkPhone() << endl;
outFile << abonent.getMobilePhone() << endl;
outFile << abonent.getAdditionalInfo() << endl;
}
outFile.close();
}
}
void loadFromFile(const string& filename) {
ifstream inFile(filename);
if (inFile.is_open()) {
string name;
string homePhone;
string workPhone;
string mobilePhone;
string additionalInfo;
while (getline(inFile, name)) {
getline(inFile, homePhone);
getline(inFile, workPhone);
getline(inFile, mobilePhone);
getline(inFile, additionalInfo);
Abonent abonent(name, homePhone, workPhone, mobilePhone, additionalInfo);
m_abonents.push_back(abonent);
}
inFile.close();
}
}
private:
vector<Abonent> m_abonents;
};
int main() {
PhoneBook phoneBook;
phoneBook.addAbonent("John Doe", "111-111-1111", "222-222-2222", "333-333-3333", "Some additional info");
phoneBook.addAbonent("Jane Smith", "444-444-4444", "555-555-5555", "666-666-6666", "Some more additional info");
phoneBook.showAllAbonents();
phoneBook.removeAbonent("John Doe");
Abonent* abonent = phoneBook.findAbonent("Jane Smith");
if (abonent != nullptr) {
cout << "Found abonent:" << endl;
cout << "Name: " << abonent->getName() << endl;
cout <<"Home Phone: " << abonent->getHomePhone() << endl;
cout << "Work Phone: " << abonent->getWorkPhone() << endl;
cout << "Mobile Phone: " << abonent->getMobilePhone() << endl;
cout << "Additional Info: " << abonent->getAdditionalInfo() << endl << endl;
} else {
cout << "Abonent not found." << endl;
}
phoneBook.saveToFile("phonebook.txt");
PhoneBook loadedPhoneBook;
loadedPhoneBook.loadFromFile("phonebook.txt");
loadedPhoneBook.showAllAbonents();
return 0;
}