151 lines
4.1 KiB
C++
151 lines
4.1 KiB
C++
/* ************************************************************************** */
|
|
/* */
|
|
/* :::::::: */
|
|
/* PhoneBook.cpp :+: :+: */
|
|
/* +:+ */
|
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
|
/* +#+ */
|
|
/* Created: 2025/03/20 11:02:32 by whaffman #+# #+# */
|
|
/* Updated: 2025/03/31 14:58:09 by whaffman ######## odam.nl */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "PhoneBook.hpp"
|
|
#include "Contact.hpp"
|
|
#include <iostream>
|
|
#include <iomanip>
|
|
|
|
PhoneBook::PhoneBook()
|
|
{
|
|
_contactCount = 0;
|
|
}
|
|
|
|
void PhoneBook::addContact()
|
|
{
|
|
Contact contact = Contact::newContact();
|
|
|
|
if (_contactCount < MAX_CONTACTS)
|
|
{
|
|
_contacts[_contactCount] = contact;
|
|
_contactCount++;
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < MAX_CONTACTS - 1; i++)
|
|
{
|
|
_contacts[i] = _contacts[i + 1];
|
|
}
|
|
_contacts[MAX_CONTACTS - 1] = contact;
|
|
}
|
|
}
|
|
|
|
std::string PhoneBook::truncate(const std::string str) const
|
|
{
|
|
if (str.length() > 10)
|
|
return (str.substr(0, 9) + ".");
|
|
return (str);
|
|
}
|
|
void PhoneBook::printAllContacts() const
|
|
{
|
|
std::cout << std::setw(10) << "Index" << "|";
|
|
std::cout << std::setw(10) << "First name" << "|";
|
|
std::cout << std::setw(10) << "Last name" << "|";
|
|
std::cout << std::setw(10) << "Nickname" << std::endl;
|
|
|
|
for (int i = 0; i < _contactCount; i++)
|
|
{
|
|
std::cout << std::setw(10) << i << "|";
|
|
std::cout << std::setw(10) << truncate(_contacts[i].getFirstName()) << "|";
|
|
std::cout << std::setw(10) << truncate(_contacts[i].getLastName()) << "|";
|
|
std::cout << std::setw(10) << truncate(_contacts[i].getNickname()) << std::endl;
|
|
}
|
|
}
|
|
|
|
void PhoneBook::printContact(const int i) const
|
|
{
|
|
if (i < 0 || i >= _contactCount)
|
|
{
|
|
std::cout << "Invalid index" << std::endl;
|
|
return;
|
|
}
|
|
|
|
std::cout << std::setw(16) << "First name: " << _contacts[i].getFirstName() << std::endl;
|
|
std::cout << std::setw(16) << "Last name: " << _contacts[i].getLastName() << std::endl;
|
|
std::cout << std::setw(16) << "Nickname: " << _contacts[i].getNickname() << std::endl;
|
|
std::cout << std::setw(16) << "Phone number: " << _contacts[i].getPhoneNumber() << std::endl;
|
|
std::cout << std::setw(16) << "Darkest secret: " << _contacts[i].getDarkestSecret() << std::endl;
|
|
|
|
return;
|
|
}
|
|
|
|
void PhoneBook::searchContact() const
|
|
{
|
|
std::string command;
|
|
|
|
if (_contactCount == 0)
|
|
{
|
|
std::cout << "The PhoneBook is empty." << std::endl;
|
|
return;
|
|
}
|
|
|
|
std::cout << "---------------------------------------------" << std::endl;
|
|
printAllContacts();
|
|
std::cout << "---------------------------------------------" << std::endl;
|
|
|
|
int index = -1;
|
|
|
|
while (true)
|
|
{
|
|
std::cout << "Enter the index of the contact you want to see: ";
|
|
std::getline(std::cin, command);
|
|
std::cout << std::endl;
|
|
if (command == "EXIT")
|
|
break;
|
|
if (command.empty())
|
|
{
|
|
std::cout << "Invalid index" << std::endl;
|
|
continue;
|
|
}
|
|
try
|
|
{
|
|
index = std::stoi(command); //0dsfsdf is also valid....
|
|
}
|
|
catch (const std::invalid_argument &e)
|
|
{
|
|
std::cout << "Invalid index" << std::endl;
|
|
continue;
|
|
}
|
|
catch (const std::out_of_range &e)
|
|
{
|
|
std::cout << "Index out of range" << std::endl;
|
|
continue;
|
|
}
|
|
if (index >= 0 && index < _contactCount)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
std::cout << "---------------------------------------------" << std::endl;
|
|
printContact(index);
|
|
std::cout << "---------------------------------------------" << std::endl;
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
void PhoneBook::run()
|
|
{
|
|
std::string command;
|
|
|
|
while (true)
|
|
{
|
|
std::cout << "Enter a command: ";
|
|
getline(std::cin, command);
|
|
if (command == "EXIT")
|
|
break;
|
|
else if (command == "ADD")
|
|
addContact();
|
|
else if (command == "SEARCH")
|
|
searchContact();
|
|
else
|
|
std::cout << "Invalid command" << std::endl;
|
|
}
|
|
} |