/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* PhoneBook.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/04/15 10:37:20 by apommier #+# #+# */ /* Updated: 2022/07/21 20:05:15 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "PhoneBook.hpp" PhoneBook::PhoneBook(void) { this->_NbrContact = -1; while (this->_NbrContact < 8) this->_Contact[this->_NbrContact++] = 0; this->_NbrContact = 0; } PhoneBook::~PhoneBook(void) { } void PhoneBook::StoreContact(Contact *NewContact) { if (_Contact[this->_NbrContact]) delete _Contact[this->_NbrContact] ; _Contact[this->_NbrContact] = NewContact; if (this->_NbrContact == 8) this->_NbrContact = 0; else this->_NbrContact++; } void PhoneBook::PrintContact(int index) { if (!this->_Contact[index]) { std::cout << "There is no contact at this index" << std::endl; return ; } std::cout << std::setw(10) << "First name: " << this->_Contact[index]->getOneInfo(0) << std::endl; std::cout << std::setw(10) << "Last name: " << this->_Contact[index]->getOneInfo(1) << std::endl; std::cout << std::setw(10) << "Nickname: " << this->_Contact[index]->getOneInfo(2) << std::endl; std::cout << std::setw(10) << "Phone number: " << this->_Contact[index]->getOneInfo(3) << std::endl; std::cout << std::setw(10) << "Darkest secret: " << this->_Contact[index]->getOneInfo(4) << std::endl; } void PhoneBook::PrintIndex() { int index = 0; std::string nbr; while (index < 8 && this->_Contact[index]) { std::cout << std::setw(10) << index + 1 << "|"; std::cout << std::setw(10) << this->_Contact[index]->GetInfo(0) << "|"; std::cout << std::setw(10) << this->_Contact[index]->GetInfo(1) << "|"; std::cout << std::setw(10) << this->_Contact[index]->GetInfo(2) << std::endl; index++; } if (!index) std::cout << "There is no contact\n"; if (index) { while (1) { std::cout << "Enter index of contact: "; std::cin >> nbr; if (nbr.size() != 1 || !isdigit(nbr[0])) std::cout << "Bad entry" << std::endl; else { index = nbr[0] - '0'; if (!index || index > 8) std::cout << "Bad entry" << std::endl; else { std::cout << index << "index \n"; this->PrintContact(index - 1); return ; } } } } } void PhoneBook::deleteContact() { for (int i = 0; this->_Contact[i] && i < 8; i++) delete this->_Contact[i]; }