LW3: Redefine add/remove methods. Add second phone (phone2) to _BookItem contact.

This commit is contained in:
Kolan Sh 2021-03-28 12:08:19 +03:00
parent 6f29575f97
commit 650bd227d4
4 changed files with 116 additions and 58 deletions

View File

@ -1,19 +1,30 @@
#include "CBookItems.h" #include "CBookItems.h"
void CBookItems::CreateNewItem(string sFirstName, string sLastName, string sPhoneNumber) void CBookItems::CreateNewItem(string sFirstName, string sLastName, string sPhoneNumber1)
{
CreateNewItem(sFirstName, sLastName, sPhoneNumber1, "");
}
void CBookItems::CreateNewItem(string sFirstName, string sLastName, string sPhoneNumber1, string sPhoneNumber2)
{ {
_BookItem* spaceItem = new _BookItem; _BookItem* spaceItem = new _BookItem;
spaceItem->_sFirstName = sFirstName; spaceItem->_sFirstName = sFirstName;
spaceItem->_sLastName = sLastName; spaceItem->_sLastName = sLastName;
spaceItem->_sPhoneNumber = sPhoneNumber; spaceItem->_sPhoneNumber1 = sPhoneNumber1;
spaceItem->_sPhoneNumber2 = sPhoneNumber2;
_aItems.push_back(*spaceItem); _aItems.push_back(*spaceItem);
//delete spaceItem; //delete spaceItem;
} }
int CBookItems::SetNewValueToItem(string sFirstName, string sLastName, string sPhoneNumber, int nNumOfItem) int CBookItems::SetNewValueToItem(string sFirstName, string sLastName, string sPhoneNumber1, int nNumOfItem)
{
SetNewValueToItem(sFirstName, sLastName, sPhoneNumber1, "", nNumOfItem);
}
int CBookItems::SetNewValueToItem(string sFirstName, string sLastName, string sPhoneNumber1, string sPhoneNumber2, int nNumOfItem)
{ {
nNumOfItem--; nNumOfItem--;
if (!(nNumOfItem > _aItems.size())) if (!(nNumOfItem > _aItems.size()))
@ -25,7 +36,8 @@ int CBookItems::SetNewValueToItem(string sFirstName, string sLastName, string sP
_BookItem spaceItem; _BookItem spaceItem;
spaceItem._sFirstName = sFirstName; spaceItem._sFirstName = sFirstName;
spaceItem._sLastName = sLastName; spaceItem._sLastName = sLastName;
spaceItem._sPhoneNumber = sPhoneNumber; spaceItem._sPhoneNumber1 = sPhoneNumber1;
spaceItem._sPhoneNumber2 = sPhoneNumber2;
_aItems.at(nNumOfItem) = spaceItem; _aItems.at(nNumOfItem) = spaceItem;
return 0; return 0;
@ -38,13 +50,20 @@ int CBookItems::SetNewValueToItem(string sFirstName, string sLastName, string sP
} }
} }
int CBookItems::GetItem(int nNumOfItem, string& sFirstName, string& sLastName, string& sPhoneNumber) int CBookItems::GetItem(int nNumOfItem, string& sFirstName, string& sLastName, string& sPhoneNumber1)
{
string sPhoneNumber2;
GetItem(nNumOfItem, sFirstName, sLastName, sPhoneNumber1, sPhoneNumber2);
}
int CBookItems::GetItem(int nNumOfItem, string& sFirstName, string& sLastName, string& sPhoneNumber1, string& sPhoneNumber2)
{ {
if (!(nNumOfItem > _aItems.size())) if (!(nNumOfItem > _aItems.size()))
{ {
sFirstName = _aItems.at(nNumOfItem)._sFirstName; sFirstName = _aItems.at(nNumOfItem)._sFirstName;
sLastName = _aItems.at(nNumOfItem)._sLastName; sLastName = _aItems.at(nNumOfItem)._sLastName;
sPhoneNumber = _aItems.at(nNumOfItem)._sPhoneNumber; sPhoneNumber1 = _aItems.at(nNumOfItem)._sPhoneNumber1;
sPhoneNumber2 = _aItems.at(nNumOfItem)._sPhoneNumber2;
return 0; return 0;
} }
else else
@ -71,19 +90,23 @@ int CBookItems::SortItems(int nSortingType)
{ {
string sFirstName = ""; string sFirstName = "";
string sLastName = ""; string sLastName = "";
string sPhoneNumber = ""; string sPhoneNumber1 = "";
string sPhoneNumber2 = "";
sFirstName = _aItems.at(i)._sFirstName; sFirstName = _aItems.at(i)._sFirstName;
sLastName = _aItems.at(i)._sLastName; sLastName = _aItems.at(i)._sLastName;
sPhoneNumber = _aItems.at(i)._sPhoneNumber; sPhoneNumber1 = _aItems.at(i)._sPhoneNumber1;
sPhoneNumber2 = _aItems.at(i)._sPhoneNumber2;
_aItems.at(i)._sFirstName = _aItems.at(j)._sFirstName; _aItems.at(i)._sFirstName = _aItems.at(j)._sFirstName;
_aItems.at(i)._sLastName = _aItems.at(j)._sLastName; _aItems.at(i)._sLastName = _aItems.at(j)._sLastName;
_aItems.at(i)._sPhoneNumber = _aItems.at(j)._sPhoneNumber; _aItems.at(i)._sPhoneNumber1 = _aItems.at(j)._sPhoneNumber1;
_aItems.at(i)._sPhoneNumber2 = _aItems.at(j)._sPhoneNumber2;
_aItems.at(j)._sFirstName = sFirstName; _aItems.at(j)._sFirstName = sFirstName;
_aItems.at(j)._sLastName = sLastName; _aItems.at(j)._sLastName = sLastName;
_aItems.at(j)._sPhoneNumber = sPhoneNumber; _aItems.at(j)._sPhoneNumber1 = sPhoneNumber1;
_aItems.at(j)._sPhoneNumber2 = sPhoneNumber2;
} }
} }
} }
@ -100,19 +123,23 @@ int CBookItems::SortItems(int nSortingType)
{ {
string sFirstName = ""; string sFirstName = "";
string sLastName = ""; string sLastName = "";
string sPhoneNumber = ""; string sPhoneNumber1 = "";
string sPhoneNumber2 = "";
sFirstName = _aItems.at(i)._sFirstName; sFirstName = _aItems.at(i)._sFirstName;
sLastName = _aItems.at(i)._sLastName; sLastName = _aItems.at(i)._sLastName;
sPhoneNumber = _aItems.at(i)._sPhoneNumber; sPhoneNumber1 = _aItems.at(i)._sPhoneNumber1;
sPhoneNumber2 = _aItems.at(i)._sPhoneNumber2;
_aItems.at(i)._sFirstName = _aItems.at(j)._sFirstName; _aItems.at(i)._sFirstName = _aItems.at(j)._sFirstName;
_aItems.at(i)._sLastName = _aItems.at(j)._sLastName; _aItems.at(i)._sLastName = _aItems.at(j)._sLastName;
_aItems.at(i)._sPhoneNumber = _aItems.at(j)._sPhoneNumber; _aItems.at(i)._sPhoneNumber1 = _aItems.at(j)._sPhoneNumber1;
_aItems.at(i)._sPhoneNumber2 = _aItems.at(j)._sPhoneNumber2;
_aItems.at(j)._sFirstName = sFirstName; _aItems.at(j)._sFirstName = sFirstName;
_aItems.at(j)._sLastName = sLastName; _aItems.at(j)._sLastName = sLastName;
_aItems.at(j)._sPhoneNumber = sPhoneNumber; _aItems.at(j)._sPhoneNumber1 = sPhoneNumber1;
_aItems.at(j)._sPhoneNumber2 = sPhoneNumber2;
} }
} }
} }
@ -178,20 +205,16 @@ int CBookItems::FindItem(string sDesiredValue, int nSortingType, vector<int>& nN
{ {
for (int i = 0; i < _aItems.size(); i++) for (int i = 0; i < _aItems.size(); i++)
{ {
for (int j = 0; j < sDesiredValue.size() & j < _aItems.at(i)._sPhoneNumber.size(); j++) string key, phone1, phone2;
{ key = sDesiredValue;
if (sDesiredValue[j] == _aItems.at(i)._sPhoneNumber[j]) phone1 = _aItems.at(i)._sPhoneNumber1;
{ phone2 = _aItems.at(i)._sPhoneNumber2;
bCheckRightValue = true;
}
else
{
bCheckRightValue = false;
}
}
if (bCheckRightValue == true) if (phone1 != "" && key.find(phone1) != std::string::npos
{ || phone2 != "" && key.find(phone2) != std::string::npos
|| phone1 != "" && phone1.find(key) != std::string::npos
|| phone2 != "" && phone2.find(key) != std::string::npos
) {
nNumOfDesirvedItems.push_back(i); nNumOfDesirvedItems.push_back(i);
} }
} }
@ -218,7 +241,8 @@ int CBookItems::SaveItems(string sNameOfFile)
{ {
for (int i = 0; i < _aItems.size(); i++) for (int i = 0; i < _aItems.size(); i++)
{ {
fSave << _aItems.at(i)._sFirstName << " " << _aItems.at(i)._sLastName << " " << _aItems.at(i)._sPhoneNumber << " " << endl; fSave << _aItems.at(i)._sFirstName << " " << _aItems.at(i)._sLastName << " "
<< _aItems.at(i)._sPhoneNumber1 << " " << _aItems.at(i)._sPhoneNumber2 << " " << endl;
} }
fSave.close(); fSave.close();
return 0; return 0;
@ -238,7 +262,7 @@ int CBookItems::LoadItems(string sNameOfFile)
} }
else else
{ {
string sFirstName = "", sLastName = "", sPhoneNum = ""; string sFirstName = "", sLastName = "", sPhoneNum1 = "", sPhoneNum2 = "";
string sItemData = ""; string sItemData = "";
while (getline(fLoad, sItemData)) while (getline(fLoad, sItemData))
{ {
@ -255,16 +279,21 @@ int CBookItems::LoadItems(string sNameOfFile)
sLastName += sItemData[j]; sLastName += sItemData[j];
j++, k++; j++, k++;
} }
k = 0;
j++; j++;
while (j < sItemData.size()) while (j < sItemData.size() && sItemData[j] != '\r' && sItemData[j] != ' ')
{ {
sPhoneNum += sItemData[j]; sPhoneNum1 += sItemData[j];
j++;
}
j++;
while (j < sItemData.size() && sItemData[j] != '\r' && sItemData[j] != ' ')
{
sPhoneNum2 += sItemData[j];
j++, k++; j++, k++;
} }
CreateNewItem(sFirstName, sLastName, sPhoneNum); CreateNewItem(sFirstName, sLastName, sPhoneNum1, sPhoneNum2);
sFirstName = "", sLastName = "", sPhoneNum = "", sItemData = ""; sFirstName = "", sLastName = "", sPhoneNum1 = "", sPhoneNum2 = "", sItemData = "";
} }
fLoad.close(); fLoad.close();
return 0; return 0;

View File

@ -24,7 +24,8 @@ private:
{ {
string _sFirstName; string _sFirstName;
string _sLastName; string _sLastName;
string _sPhoneNumber; string _sPhoneNumber1;
string _sPhoneNumber2;
}; };
vector<_BookItem> _aItems; vector<_BookItem> _aItems;
@ -35,19 +36,37 @@ public:
/** /**
*Создаёт элемент телефонного справочника и задаёт его значение *Создаёт элемент телефонного справочника и задаёт его значение
*/ */
void CreateNewItem(string sFirstName, string sLastName, string sPhoneNumber); void CreateNewItem(string sFirstName, string sLastName, string sPhoneNumber1);
//===============================================================================
/**
*Ñîçäà¸ò ýëåìåíò òåëåôîííîãî ñïðàâî÷íèêà è çàäà¸ò åãî çíà÷åíèå (2 òåëåôîíà)
*/
void CreateNewItem(string sFirstName, string sLastName, string sPhoneNumber1, string sPhoneNumber2);
//=============================================================================== //===============================================================================
/** /**
*Меняет значение заданного элемента телефонного справочника *Меняет значение заданного элемента телефонного справочника
*/ */
int SetNewValueToItem(string sFirstName, string sLastName, string sPhoneNumber, int nNumOfItem); int SetNewValueToItem(string sFirstName, string sLastName, string sPhoneNumber1, int nNumOfItem);
//===============================================================================
/**
*Ìåíÿåò çíà÷åíèå çàäàííîãî ýëåìåíòà òåëåôîííîãî ñïðàâî÷íèêà (2 òåëåôîíà)
*/
int SetNewValueToItem(string sFirstName, string sLastName, string sPhoneNumber1, string sPhoneNumber2, int nNumOfItem);
//=============================================================================== //===============================================================================
/** /**
*Возвращает данные элемента телефонного справочника *Возвращает данные элемента телефонного справочника
*/ */
int GetItem(int nNumOfItem, string& sFirstName, string& sLastName, string& sPhoneNumber); int GetItem(int nNumOfItem, string& sFirstName, string& sLastName, string& sPhoneNumber1);
//===============================================================================
/**
*Âîçâðàùàåò äàííûå ýëåìåíòà òåëåôîííîãî ñïðàâî÷íèêà (2 òåëåôîíà)
*/
int GetItem(int nNumOfItem, string& sFirstName, string& sLastName, string& sPhoneNumber1, string& sPhoneNumber2);
//=============================================================================== //===============================================================================
/** /**

View File

@ -24,10 +24,11 @@ void CConsoleUI::PrintPhoneBook(CBookItems& CPhoneBook)
{ {
string sFName = " "; string sFName = " ";
string sLName = " "; string sLName = " ";
string sNum = " "; string sNum1 = " ";
string sNum2 = " ";
CPhoneBook.GetItem(i, sFName, sLName, sNum); CPhoneBook.GetItem(i, sFName, sLName, sNum1, sNum2);
cout << 1 + i << ". " << sFName << " " << sLName << ": " << sNum << endl; cout << 1 + i << ". " << sFName << " " << sLName << ": " << sNum1 << ", " << sNum2 << endl;
} }
} }
else else
@ -42,27 +43,31 @@ void CConsoleUI::ChangeSomeItem(CBookItems& CPhoneBook)
int nNumOfElement = 0; int nNumOfElement = 0;
string sFName; string sFName;
string sLName; string sLName;
string sNum; string sNum1;
string sNum2;
cout << "Ââåäèòå íîìåð ýëåìåíòà: "; cout << "Ââåäèòå íîìåð ýëåìåíòà: ";
cin >> nNumOfElement; cin >> nNumOfElement;
cout << "Ââåäèòå èìÿ, ôàìèëèþ è íîìåð àáîíåíòà:" << endl; cout << "Ââåäèòå èìÿ, ôàìèëèþ è íîìåðà àáîíåíòà:" << endl;
cout << "Èìÿ: "; cout << "Èìÿ: ";
cin >> sFName; cin >> sFName;
cout << "Ôàìèëèÿ: "; cout << "Ôàìèëèÿ: ";
cin >> sLName; cin >> sLName;
cout << "Íîìåð: "; cout << "Íîìåð1: ";
cin >> sNum; cin >> sNum1;
CPhoneBook.SetNewValueToItem(sFName, sLName, sNum, nNumOfElement); cout << "Íîìåð2: ";
cin >> sNum2;
CPhoneBook.SetNewValueToItem(sFName, sLName, sNum1, sNum2, nNumOfElement);
} }
void CConsoleUI::SortItems(CBookItems& CPhoneBook) void CConsoleUI::SortItems(CBookItems& CPhoneBook)
{ {
int nSortingType = 0; int nSortingType = 0;
cout << "Ââåäèòå íîìåð òèïà ñîðòèðîâêè:" << endl; cout << "Ââåäèòå íîìåðà òèïà ñîðòèðîâêè:" << endl;
cout << "1. Ïî Èìåíè" << endl; cout << "1. Ïî Èìåíè" << endl;
cout << "2. Ïî Ôàìèëèè" << endl; cout << "2. Ïî Ôàìèëèè" << endl;
cout << "3. Ïî Íîìåðó" << endl; cout << "3. Ïî Íîìåðó1" << endl;
cout << "4. Ïî Íîìåðó2" << endl;
cin >> nSortingType; cin >> nSortingType;
nSortingType--; nSortingType--;
int error = CPhoneBook.SortItems(nSortingType); int error = CPhoneBook.SortItems(nSortingType);
@ -77,7 +82,7 @@ void CConsoleUI::FindItem(CBookItems& CPhoneBook)
string sValueNeed = ""; string sValueNeed = "";
vector<int> nNumOfDesirvedItems; vector<int> nNumOfDesirvedItems;
int nSortingType = 0; int nSortingType = 0;
cout << "Ââåäèòå íîìåð òèïà ïîèñêà:" << endl; cout << "Ââåäèòå íîìåðà òèïà ïîèñêà:" << endl;
cout << "1. Ïî Èìåíè" << endl; cout << "1. Ïî Èìåíè" << endl;
cout << "2. Ïî Ôàìèëèè" << endl; cout << "2. Ïî Ôàìèëèè" << endl;
cout << "3. Ïî Íîìåðó" << endl; cout << "3. Ïî Íîìåðó" << endl;
@ -91,10 +96,11 @@ void CConsoleUI::FindItem(CBookItems& CPhoneBook)
{ {
string sFName = " "; string sFName = " ";
string sLName = " "; string sLName = " ";
string sNum = " "; string sNum1 = " ";
string sNum2 = " ";
CPhoneBook.GetItem(nNumOfDesirvedItems.at(i), sFName, sLName, sNum); CPhoneBook.GetItem(nNumOfDesirvedItems.at(i), sFName, sLName, sNum1, sNum2);
cout << 1 + i << ". " << sFName << " " << sLName << ": " << sNum << endl; cout << 1 + i << ". " << sFName << " " << sLName << ": " << sNum1 << ", " << sNum2 << endl;
} }
} }
@ -102,17 +108,20 @@ void CConsoleUI::CreateNewItem(CBookItems& CPhoneBook)
{ {
string sFName; string sFName;
string sLName; string sLName;
string sNum; string sNum1;
string sNum2;
cout << "Ââåäèòå èìÿ, ôàìèëèþ è íîìåð àáîíåíòà:" << endl; cout << "Ââåäèòå èìÿ, ôàìèëèþ è íîìåðà àáîíåíòà:" << endl;
cout << "Èìÿ: "; cout << "Èìÿ: ";
cin >> sFName; cin >> sFName;
cout << "Ôàìèëèÿ: "; cout << "Ôàìèëèÿ: ";
cin >> sLName; cin >> sLName;
cout << "Íîìåð: "; cout << "Íîìåð1: ";
cin >> sNum; cin >> sNum1;
cout << "Íîìåð2: ";
cin >> sNum2;
CPhoneBook.CreateNewItem(sFName, sLName, sNum); CPhoneBook.CreateNewItem(sFName, sLName, sNum1, sNum2);
} }
void CConsoleUI::Help() void CConsoleUI::Help()

View File

@ -1,3 +1,4 @@
Anton Pavkin +79374237500 Anton Pavkin +79374237500
Anton Pavkin +79374237500 Anton Pavkin +79374237500
Luap Pupa pupazalupu Vasya Pupkin +79628393472 +79328937344
Oleg Kuvaev +79052983329 +7962923822