109 lines
3.6 KiB
C++
109 lines
3.6 KiB
C++
#include "PhoneBook.hpp"
|
|
|
|
PhoneBook::PhoneBook() {
|
|
_currentIndex = 0;
|
|
_totalContacts = 0;
|
|
}
|
|
|
|
PhoneBook::~PhoneBook() {}
|
|
void PhoneBook::addContact() {
|
|
Contact tempContact;
|
|
std::string input;
|
|
|
|
while (true) {
|
|
std::cout << "Enter first name: ";
|
|
if (!std::getline(std::cin, input)) exit(1);
|
|
if (!input.empty()) break;
|
|
std::cout << "Field cannot be empty!" << std::endl;
|
|
}
|
|
tempContact.setFirstName(input);
|
|
while (true) {
|
|
std::cout << "Enter last name: ";
|
|
if (!std::getline(std::cin, input)) exit(1);
|
|
if (!input.empty()) break;
|
|
std::cout << "Field cannot be empty!" << std::endl;
|
|
}
|
|
tempContact.setLastName(input);
|
|
while (true) {
|
|
std::cout << "Enter nickname: ";
|
|
if (!std::getline(std::cin, input)) exit(1);
|
|
if (!input.empty()) break;
|
|
std::cout << "Field cannot be empty!" << std::endl;
|
|
}
|
|
tempContact.setNickname(input);
|
|
while (true) {
|
|
std::cout << "Enter phone number: ";
|
|
if (!std::getline(std::cin, input)) exit(1);
|
|
if (!input.empty()) break;
|
|
std::cout << "Field cannot be empty!" << std::endl;
|
|
}
|
|
tempContact.setPhoneNumber(input);
|
|
while (true) {
|
|
std::cout << "Enter Darkest Secret: ";
|
|
if (!std::getline(std::cin, input)) exit(1);
|
|
if (!input.empty()) break;
|
|
std::cout << "Field cannot be empty!" << std::endl;
|
|
}
|
|
tempContact.setDarkestSecret(input);
|
|
|
|
_contacts[_currentIndex] = tempContact;
|
|
_currentIndex = (_currentIndex + 1) % 8;
|
|
if (_totalContacts < 8) _totalContacts++;
|
|
|
|
std::cout << "Contact added successfully!" << std::endl;
|
|
}
|
|
|
|
void PhoneBook::searchContacts() const {
|
|
std::string input;
|
|
int index;
|
|
|
|
if (_totalContacts == 0) {
|
|
std::cout << "Phone book is empty!" << std::endl;
|
|
return;
|
|
}
|
|
std::cout << std::setw(10) << "Index" << "|"
|
|
<< std::setw(10) << "First Name" << "|"
|
|
<< std::setw(10) << "Last Name" << "|"
|
|
<< std::setw(10) << "Nickname" << std::endl;
|
|
std::cout << "----------------------------------------" << std::endl;
|
|
int i = 0;
|
|
while (i < _totalContacts) {
|
|
std::cout << std::setw(10) << i << "|"
|
|
<< std::setw(10) << truncateString(_contacts[i].getFirstName()) << "|"
|
|
<< std::setw(10) << truncateString(_contacts[i].getLastName()) << "|"
|
|
<< std::setw(10) << truncateString(_contacts[i].getNickname()) << std::endl;
|
|
i++;
|
|
}
|
|
while (true) {
|
|
std::cout << "Enter the index of the contact to display: ";
|
|
if (!std::getline(std::cin, input)) {
|
|
exit(1);
|
|
}
|
|
if (input.length() == 1 && input[0] >= '0' && input[0] <= '7') {
|
|
index = input[0] - '0';
|
|
if (index < _totalContacts) {
|
|
break;
|
|
}
|
|
}
|
|
std::cout << "Invalid index! Please enter a valid contact index." << std::endl;
|
|
}
|
|
displayContact(index);
|
|
}
|
|
|
|
std::string PhoneBook::truncateString(const std::string& str) const {
|
|
if (str.length() > 10) {
|
|
return str.substr(0, 9) + ".";
|
|
}
|
|
return str;
|
|
}
|
|
|
|
void PhoneBook::displayContact(int index) const {
|
|
std::cout << std::endl << "=== Contact Details ===" << std::endl;
|
|
std::cout << "First Name: " << _contacts[index].getFirstName() << std::endl;
|
|
std::cout << "Last Name: " << _contacts[index].getLastName() << std::endl;
|
|
std::cout << "Nickname: " << _contacts[index].getNickname() << std::endl;
|
|
std::cout << "Phone Number: " << _contacts[index].getPhoneNumber() << std::endl;
|
|
std::cout << "Darkest Secret: " << _contacts[index].getDarkestSecret() << std::endl;
|
|
std::cout << "======================" << std::endl;
|
|
}
|