|
| 1 | +#include "PhoneBook.hpp" |
| 2 | + |
| 3 | +PhoneBook::PhoneBook() { |
| 4 | + last_added = -1; |
| 5 | + users_count = 0; |
| 6 | +} |
| 7 | + |
| 8 | +void PhoneBook::Add(Contact contact){ |
| 9 | + if (!contact.IsEmpty()){ |
| 10 | + if (users_count < USERS_COUNT) |
| 11 | + users_count++; |
| 12 | + if (last_added == USERS_COUNT - 1) |
| 13 | + last_added = -1; |
| 14 | + last_added++; |
| 15 | + contacts[last_added] = contact; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +void PhoneBook::Search(){ |
| 20 | + if (users_count == 0){ |
| 21 | + std::cout << "Phonebook is empty" << std::endl; |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + std::string tmp; |
| 26 | + int id_to_show; |
| 27 | + |
| 28 | + PrintString_("index"); |
| 29 | + std::cout << '|'; |
| 30 | + PrintString_("first name"); |
| 31 | + std::cout << '|'; |
| 32 | + PrintString_("last name"); |
| 33 | + std::cout << '|'; |
| 34 | + PrintString_("nickname"); |
| 35 | + std::cout << '|' << std::endl; |
| 36 | + |
| 37 | + for (int i = 0; i < USERS_COUNT; ++i){ |
| 38 | + if (contacts[i].IsEmpty()){ |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + PrintString_(std::to_string(i)); |
| 43 | + std::cout << '|'; |
| 44 | + |
| 45 | + PrintString_(contacts[i].GetFirstName()); |
| 46 | + std::cout << '|'; |
| 47 | + |
| 48 | + PrintString_(contacts[i].GetLastName()); |
| 49 | + std::cout << '|'; |
| 50 | + |
| 51 | + PrintString_(contacts[i].GetNickname()); |
| 52 | + std::cout << std::endl; |
| 53 | + } |
| 54 | + |
| 55 | + id_to_show = -1; |
| 56 | + std::cout << "enter id: "; |
| 57 | + std::cin >> id_to_show; |
| 58 | + |
| 59 | + if (id_to_show < 0 || id_to_show >= users_count){ |
| 60 | + std::cin.clear(); |
| 61 | + std::cout << "Invalid id" << std::endl; |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + std::cout << "First name: " << contacts[id_to_show].GetFirstName() << std::endl; |
| 66 | + std::cout << "Last name: " << contacts[id_to_show].GetLastName() << std::endl; |
| 67 | + std::cout << "Nickname: " << contacts[id_to_show].GetNickname() << std::endl; |
| 68 | + std::cout << "Phone number: " << contacts[id_to_show].GetPhoneNumber() << std::endl; |
| 69 | + std::cout << "Darkest secret: " << contacts[id_to_show].GetDarkestSecret() << std::endl; |
| 70 | +} |
| 71 | + |
| 72 | +void PhoneBook::PrintString_(std::string str){ |
| 73 | + if (str.size() == STR_LEN){ |
| 74 | + std::cout << str; |
| 75 | + } else if (str.size() < STR_LEN){ |
| 76 | + std::cout << std::string(STR_LEN - str.size(), ' ') << str; |
| 77 | + } else { |
| 78 | + std::cout << str.substr(0, STR_LEN - 1) << '.'; |
| 79 | + } |
| 80 | +} |
0 commit comments