42_CPP00/ex01/src/PhoneBook.cpp
2023-05-22 17:34:19 +00:00

70 lines
1.1 KiB
C++

#include "../include/PhoneBook.hpp"
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
PhoneBook::PhoneBook()
{
this->len = 0;
}
PhoneBook::~PhoneBook()
{
for (size_t i = 0; i < this->len; i++)
delete this->contacts[i];
}
void PhoneBook::add_contact(Contact *new_contact)
{
size_t i;
std::cout << std::endl << this->len << std::endl;
if (this->len < 8)
{
this->contacts[this->len] = new_contact;
this->len++;
}
else
{
delete this->contacts[0];
for (i = this->len - 1; i > 0; i--) {
this->contacts[i] = this->contacts[i - 1];
}
this->contacts[0] = new_contact;
}
}
void PhoneBook::display_contacts()
{
size_t i;
std::cout
<< " index|"
<< "first name|"
<< "last name |"
<< "nickname |"
<< "phone num.|"
<< "darkest s."
<< std::endl;
for (i = 0; i < this->len; i++)
{
std::cout
<< std::setw(10) << i
<< "|"
<< this->contacts[i]->to_string_partial()
<< std::endl;
}
}
Contact* PhoneBook::search(int index)
{
if ((size_t) index > this->len || index < 0)
return (NULL);
return (this->contacts[index]);
}