LW3.2 Redefine input/output streams operators.

This commit is contained in:
Kolan Sh 2021-03-28 21:12:20 +03:00
parent 650bd227d4
commit ca3125dbd4
3 changed files with 55 additions and 18 deletions

View File

@ -8,7 +8,7 @@ void CBookItems::CreateNewItem(string sFirstName, string sLastName, string sPhon
void CBookItems::CreateNewItem(string sFirstName, string sLastName, string sPhoneNumber1, string sPhoneNumber2)
{
_BookItem* spaceItem = new _BookItem;
BookItem* spaceItem = new BookItem;
spaceItem->_sFirstName = sFirstName;
spaceItem->_sLastName = sLastName;
spaceItem->_sPhoneNumber1 = sPhoneNumber1;
@ -33,7 +33,7 @@ int CBookItems::SetNewValueToItem(string sFirstName, string sLastName, string sP
{
if (nNumOfItem == i)
{
_BookItem spaceItem;
BookItem spaceItem;
spaceItem._sFirstName = sFirstName;
spaceItem._sLastName = sLastName;
spaceItem._sPhoneNumber1 = sPhoneNumber1;
@ -72,6 +72,13 @@ int CBookItems::GetItem(int nNumOfItem, string& sFirstName, string& sLastName, s
}
}
const CBookItems::BookItem& CBookItems::GetItem(int nNumOfItem)
{
if (nNumOfItem < 0 || nNumOfItem > _aItems.size()) return CBookItems::BookItem();
return _aItems[nNumOfItem];
}
int CBookItems::GetSize()
{
return _aItems.size();
@ -301,3 +308,18 @@ int CBookItems::LoadItems(string sNameOfFile)
}
std::ostream& operator<<(std::ostream& s, const CBookItems::BookItem& item)
{
s << item._sFirstName << " " << item._sLastName << ": " << item._sPhoneNumber1;
if (item._sPhoneNumber2 != "")
s << ", " << item._sPhoneNumber2;
return s;
}
std::istream& operator>>(std::istream& s, CBookItems::BookItem& item)
{
s >> item._sFirstName >> item._sLastName >> item._sPhoneNumber1 >> item._sPhoneNumber2;
return s;
}

View File

@ -19,16 +19,33 @@ using namespace std;
class CBookItems
{
private:
struct _BookItem
public:
struct BookItem
{
private:
string _sFirstName;
string _sLastName;
string _sPhoneNumber1;
string _sPhoneNumber2;
public:
BookItem()
{
}
~BookItem()
{
}
//===============================================================================
/**
*Ďĺđĺîďđĺäĺë˙ĺň îďĺđŕňîđű âűâîäŕ/âűâîäŕ
*/
friend std::ostream& operator<<(std::ostream& s, const BookItem& item);
friend std::istream& operator>>(std::istream& s, BookItem& item);
friend CBookItems;
};
vector<_BookItem> _aItems;
vector<BookItem> _aItems;
public:
@ -68,6 +85,12 @@ public:
*/
int GetItem(int nNumOfItem, string& sFirstName, string& sLastName, string& sPhoneNumber1, string& sPhoneNumber2);
//===============================================================================
/**
*Âîçâđŕůŕĺň ýëĺěĺíň ňĺëĺôîííîăî ńďđŕâî÷íčęŕ
*/
const BookItem& GetItem(int nNumOfItem);
//===============================================================================
/**
*Âîçâðàùàåò êîëè÷åñòâî ýëåìåíòîâ â òåëåôîííîì ñïðàâî÷íèêå

View File

@ -21,15 +21,7 @@ void CConsoleUI::PrintPhoneBook(CBookItems& CPhoneBook)
if (CPhoneBook.GetSize() >= 1)
{
for (int i = 0; i < CPhoneBook.GetSize(); i++)
{
string sFName = " ";
string sLName = " ";
string sNum1 = " ";
string sNum2 = " ";
CPhoneBook.GetItem(i, sFName, sLName, sNum1, sNum2);
cout << 1 + i << ". " << sFName << " " << sLName << ": " << sNum1 << ", " << sNum2 << endl;
}
cout << 1 + i << ". " << CPhoneBook.GetItem(i) << endl;
}
else
{