ONKurs/phoebook/CConsoleUI.cpp

216 lines
5.0 KiB
C++

#include "CConsoleUI.h"
void CConsoleUI::StartUsingUI(CBookItems& CPhoneBook)
{
LoadPhoneBook(CPhoneBook);
bExit = false;
_aUserCommandList.push_back("help");
_aUserCommandList.push_back("SHW_PHNBOOK");
_aUserCommandList.push_back("CNG_ITEM");
_aUserCommandList.push_back("SRT_ITEMS");
_aUserCommandList.push_back("FND_ITEM");
_aUserCommandList.push_back("CRT_ITEM");
_aUserCommandList.push_back("SAVE_ITEMS");
_aUserCommandList.push_back("EXIT");
_aUserCommandList.push_back("LOAD_ITEMS");
}
void CConsoleUI::PrintPhoneBook(CBookItems& CPhoneBook)
{
if (CPhoneBook.GetSize() >= 1)
{
for (int i = 0; i < CPhoneBook.GetSize(); i++)
cout << 1 + i << ". " << CPhoneBook.GetItem(i) << endl;
}
else
{
cout << "Âàøà òåëåôîííàÿ êíèãà ïóñòà" << endl;
}
}
void CConsoleUI::ChangeSomeItem(CBookItems& CPhoneBook)
{
int nNumOfElement = 0;
string sFName;
string sLName;
string sNum1;
string sNum2;
cout << "Ââåäèòå íîìåð ýëåìåíòà: ";
cin >> nNumOfElement;
cout << "Ââåäèòå èìÿ, ôàìèëèþ è íîìåðà àáîíåíòà:" << endl;
cout << "Èìÿ: ";
cin >> sFName;
cout << "Ôàìèëèÿ: ";
cin >> sLName;
cout << "Íîìåð1: ";
cin >> sNum1;
cout << "Íîìåð2: ";
cin >> sNum2;
CPhoneBook.SetNewValueToItem(sFName, sLName, sNum1, sNum2, nNumOfElement);
}
void CConsoleUI::SortItems(CBookItems& CPhoneBook)
{
int nSortingType = 0;
cout << "Ââåäèòå íîìåðà òèïà ñîðòèðîâêè:" << endl;
cout << "1. Ïî Èìåíè" << endl;
cout << "2. Ïî Ôàìèëèè" << endl;
cout << "3. Ïî Íîìåðó1" << endl;
cout << "4. Ïî Íîìåðó2" << endl;
cin >> nSortingType;
nSortingType--;
int error = CPhoneBook.SortItems(nSortingType);
if (error == 1)
{
cout << "Îøèáêà: êîìàíäà ââåäåíà íåâåðíî!" << endl;
}
}
void CConsoleUI::FindItem(CBookItems& CPhoneBook)
{
string sValueNeed = "";
vector<int> nNumOfDesirvedItems;
int nSortingType = 0;
cout << "Ââåäèòå íîìåðà òèïà ïîèñêà:" << endl;
cout << "1. Ïî Èìåíè" << endl;
cout << "2. Ïî Ôàìèëèè" << endl;
cout << "3. Ïî Íîìåðó" << endl;
cin >> nSortingType;
cout << "Ââåäèòå èìÿ/ôàìèëèþ/òåëåôîí àáîíåíòà: ";
cin >> sValueNeed;
CPhoneBook.FindItem(sValueNeed, nSortingType, nNumOfDesirvedItems);
for (int i = 0; i < nNumOfDesirvedItems.size(); i++)
{
cout << 1 + i << ". " << CPhoneBook.GetItem(nNumOfDesirvedItems.at(i)).to_string() << endl;
}
}
void CConsoleUI::CreateNewItem(CBookItems& CPhoneBook)
{
string sFName;
string sLName;
string sNum1;
string sNum2;
cout << "Ââåäèòå èìÿ, ôàìèëèþ è íîìåðà àáîíåíòà:" << endl;
cout << "Èìÿ: ";
cin >> sFName;
cout << "Ôàìèëèÿ: ";
cin >> sLName;
cout << "Íîìåð1: ";
cin >> sNum1;
cout << "Íîìåð2: ";
cin >> sNum2;
CPhoneBook.CreateNewItem(sFName, sLName, sNum1, sNum2);
}
void CConsoleUI::Help()
{
cout << "Ïåðå÷èñëåííûå íèæå êîìàíäû" << endl << "Ïîìîãàþò âçàèìîäåéñòâîâàòü ïîëüçîâàòåëþ ñî ñïðàâî÷íèêîì: " << endl;
cout << "EXIT- çàêðûòü ïðîãðàììó" << endl;
cout << "SHW_PHNBOOK- ïîêàçàòü ñïðàâî÷íèê" << endl;
cout << "CNG_ITEM- èçìåíèòü äàííûå àáîíåíòà" << endl;
cout << "SRT_ITEMS- ñîðòèðîâêà àáîíåíòîâ" << endl;
cout << "FND_ITEM- ïîèñê àáîíåíòîâ" << endl;
cout << "CRT_ITEM- ñîçäàíèå íîâîãî àîíåíòà" << endl;
cout << "SAVE_ITEMS- ñîõðàíåíèå äàííûõ ïîëüçîâàòåëåé" << endl;
cout << "LOAD_ITEMS- çàãðóçêà äàííûõ ïîëüçîâàòåëåé" << endl;
}
void CConsoleUI::MakeSomeAcion(CBookItems& CPhoneBook)
{
cout << "Ââåäèòå êîììàíäó äëÿ âçàèìîäåéñòâèÿ ñ òåëåôîííûì ñïðàâî÷íèêîì" << endl << "(Ââåäèòå 'help' äëÿ îòîáðàæåíèÿ êîìàíä): ";
cin >> _sUserCommand;
system("cls");
for (int i = 0; i < _aUserCommandList.size(); i++)
{
if (_sUserCommand == _aUserCommandList.at(i))
{
switch (i)
{
case 0:
Help();
break;
case 1:
PrintPhoneBook(CPhoneBook);
break;
case 2:
ChangeSomeItem(CPhoneBook);
PrintPhoneBook(CPhoneBook);
break;
case 3:
SortItems(CPhoneBook);
PrintPhoneBook(CPhoneBook);
break;
case 4:
FindItem(CPhoneBook);
break;
case 5:
CreateNewItem(CPhoneBook);
PrintPhoneBook(CPhoneBook);
break;
case 6:
SavePhoneBook(CPhoneBook);
PrintPhoneBook(CPhoneBook);
break;
case 7:
CloseProgramm();
break;
case 8:
LoadPhoneBook(CPhoneBook);
PrintPhoneBook(CPhoneBook);
break;
default:
cout << "Íåâåðíî ââåäåíà êîìàíäà" << endl;
break;
}
}
}
}
void CConsoleUI::SavePhoneBook(CBookItems& CPhoneBook)
{
if (CPhoneBook.SaveItems("myFile.txt") == 1)
{
cout << "Îøèáêà îòêðûòèÿ ôàéëà" << endl;
}
else
{
cout << "Ôàéë óñïåøíî îòêðûò" << endl;
}
}
void CConsoleUI::LoadPhoneBook(CBookItems& CPhoneBook)
{
if (CPhoneBook.LoadItems("myFile.txt") == 1)
{
cout << "Îøèáêà äîñòóïà ê ôàéëó" << endl;
}
else
{
cout << "Äàííûå óñïåøíî çàãðóæåíû" << endl;
}
}
void CConsoleUI::CloseProgramm()
{
bExit = true;
}
void CConsoleUI::SetConsoleUI(CBookItems& CPhoneBook)
{
StartUsingUI(CPhoneBook);
setlocale(LC_ALL, "Rus");
while (!bExit)
{
MakeSomeAcion(CPhoneBook);
}
system("cls");
}