CPP00 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
PhoneBookSearchContact.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* PhoneBookSearchContact.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/04/12 13:04:39 by kamitsui #+# #+# */
9/* Updated: 2025/04/13 01:28:55 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "PhoneBook.hpp"
14
27std::string PhoneBook::truncateString(const std::string &str, size_t maxLength) const {
28 if (str.length() > maxLength) {
29 return str.substr(0, maxLength - 1) + ".";
30 }
31 return str;
32}
33
40void PhoneBook::displaySavedContacts() const {
41 // Display header
42 std::cout << std::setw(10) << "Index" << "|" << std::setw(10) << "First Name" << "|" << std::setw(10) << "Last Name"
43 << "|" << std::setw(10) << "NickName" << std::endl;
44
45 // Display contacts
46 for (int i = 0; i < contactCount_; ++i) {
47 std::string firstName = truncateString(contacts_[i].getFirstName(), 10);
48 std::string lastName = truncateString(contacts_[i].getLastName(), 10);
49 std::string nickName = truncateString(contacts_[i].getNickName(), 10);
50
51 std::cout << std::setw(10) << i + 1 << "|" << std::setw(10) << firstName << "|" << std::setw(10) << lastName
52 << "|" << std::setw(10) << nickName << std::endl;
53 }
54}
55
64bool PhoneBook::getValidIndex(int &index) const {
65 std::string input = getInputLine();
66 if (input.empty())
67 return (false);
68 std::istringstream iss(input);
69 if (!(iss >> index)) {
70 std::cout << "Invalid input. Please enter the numbers." << std::endl;
71 return (false);
72 }
73 if (index >= 1 && index <= contactCount_) {
74 char remaining;
75 if (iss >> remaining) {
76 std::cout << "Warning: Number contains additional characters" << std::endl;
77 return (false);
78 }
79 } else {
80 std::cout << "Invalid index. Please input (1-" << contactCount_ << ")" << std::endl;
81 return (false);
82 }
83 return (true);
84}
85
93void PhoneBook::displayContactInfo(const Contact &contact) const {
94 std::cout << "First Name: " << contact.getFirstName() << std::endl;
95 std::cout << "Last Name: " << contact.getLastName() << std::endl;
96 std::cout << "NickName: " << contact.getNickName() << std::endl;
97 std::cout << "Phone Number: " << contact.getPhoneNumber() << std::endl;
98 std::cout << "Darkest Secret: " << contact.getDarkestSecret() << std::endl;
99}
100
108 if (contactCount_ < 1) {
109 std::cout << "No contact information registered." << std::endl;
110 return;
111 }
112
113 displaySavedContacts();
114 std::cout << "Enter index: ";
115 int index;
116 if (getValidIndex(index) != true)
117 return;
118 displayContactInfo(contacts_[index - 1]);
119}
PhoneBook Class
_Setw setw(int __n)
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
ostream cout
basic_string substr(size_type __pos=0, size_type __n=npos) const
size_type length() const noexcept
bool empty() const noexcept
Defines the Contact class.
Definition Contact.hpp:37
std::string getLastName() const
Gets the last name
Definition Contact.cpp:77
std::string getDarkestSecret() const
Gets the darkest secret of contact.
Definition Contact.cpp:98
std::string getPhoneNumber() const
Gets the phone number of contact.
Definition Contact.cpp:91
std::string getFirstName() const
Gets the first name of contact.
Definition Contact.cpp:70
std::string getNickName() const
Gets the nick name of contact.
Definition Contact.cpp:84
std::string getInputLine() const
Reads a line of text from standard input.
Definition PhoneBook.cpp:35
void searchContact() const
Searches for and displays contact information by index