// // phonebook.cpp // #include "phonebook.h" #include #include #include #include using namespace std; PhoneBook::PhoneBook() : entries{0} { } bool PhoneBook::empty() const { return entries <= 0; } int PhoneBook::index_of(string target) const { int i = 0; while ( i < entries && book[i]->name() != target ) ++i; if ( i >= entries ) return NOT_FOUND; else return i; } Entry *PhoneBook::lookup(string target) const { int loc = index_of(target); if ( loc == NOT_FOUND ) return nullptr; else return book[loc]; } void PhoneBook::add(Entry *new_entry) { if ( lookup(new_entry->name()) == nullptr ) { book[entries] = new_entry; ++entries; } } void PhoneBook::remove(string name) { int loc = index_of(name); if ( loc != NOT_FOUND ) { for(int i = loc; i < entries - 1; ++i) book[i] = book[i + 1]; --entries; } } void PhoneBook::list() const { size_t maxlen = 0; for(int i = 0; i < entries; ++i) maxlen = max(maxlen, book[i]->name().length()); for(int i = 0; i < entries; ++i) { cout << setw(maxlen + 1) << setiosflags(ios::left) << book[i]->name(); cout << resetiosflags(ios::left); cout << book[i]->number() << endl; } }