-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneBook.cpp
More file actions
156 lines (144 loc) · 5.24 KB
/
PhoneBook.cpp
File metadata and controls
156 lines (144 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PhoneBook.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zelhajou <zelhajou@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/27 15:54:34 by zelhajou #+# #+# */
/* Updated: 2024/06/04 20:08:46 by zelhajou ### ########.fr */
/* */
/* ************************************************************************** */
#include "PhoneBook.hpp"
#include <iostream>
#include <iomanip>
#include "Contact.hpp"
#include <cctype>
PhoneBook::PhoneBook()
{
this->contact_index = 0;
this->contact_count = 0;
}
PhoneBook::~PhoneBook()
{
}
void PhoneBook::addContact()
{
std::string contact_info[5];
int current_indx = 0;
const std::string contact_info_str[] = {"First Name", "Last Name", "Nick Name", "Phone Number", "Darkest Secret"};
if (this->contact_index == 8)
this->contact_index = 0;
std::cout << "Enter the contact's information" << std::endl;
while (true)
{
std::cout << "\033[1m\033[38;5;255m" << contact_info_str[current_indx] << ": \033[0m";
std::getline(std::cin, contact_info[current_indx]);
if (std::cin.eof())
break;
if (contact_info[current_indx].empty())
{
std::cout << "Please enter a valid " << contact_info_str[current_indx] << std::endl;
continue;
}
if (current_indx == firstName)
this->contacts[this->contact_index].setFirstName(contact_info[current_indx]);
if (current_indx == lastName)
this->contacts[this->contact_index].setLastName(contact_info[current_indx]);
if (current_indx == nickName)
this->contacts[this->contact_index].setNickName(contact_info[current_indx]);
if (current_indx == phoneNumber)
{
if (contact_info[current_indx].length() != 10)
{
std::cout << "Please enter a valid phone number" << std::endl;
continue;
}
for (int i = 0; i < 10; i++)
{
if (!isdigit(contact_info[current_indx][i]))
{
std::cout << "Please enter a valid phone number" << std::endl;
break;
}
}
this->contacts[this->contact_index].setPhoneNumber(contact_info[current_indx]);
}
if (current_indx == darkestSecret)
this->contacts[this->contact_index].setDarkestSecret(contact_info[current_indx]);
if (current_indx == darkestSecret)
break;
current_indx++;
}
this->contact_index++;
if (this->contact_count < 8)
this->contact_count++;
}
void PhoneBook::displayContacts()
{
std::cout << "\033[38;5;51mYou have " << this->contact_count << " contacts available\033[0m" << std::endl;
std::cout << "\033[1m\033[37m";
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) << "Nick Name" << "|" << std::endl;
std::cout << "\033[0m";
for (int i = 0; i < this->contact_count; i++)
{
std::cout << std::setw(10) << i << "|";
if (this->contacts[i].getFirstName().length() > 10)
std::cout << this->contacts[i].getFirstName().substr(0, 9) << ".|";
else
std::cout << std::setw(10) << this->contacts[i].getFirstName() << "|";
if (this->contacts[i].getLastName().length() > 10)
std::cout << this->contacts[i].getLastName().substr(0, 9) << ".|";
else
std::cout << std::setw(10) << this->contacts[i].getLastName() << "|";
if (this->contacts[i].getNickName().length() > 10)
std::cout << this->contacts[i].getNickName().substr(0, 9) << ".|";
else
std::cout << std::setw(10) << this->contacts[i].getNickName() << "|";
std::cout << std::endl;
}
}
void PhoneBook::searchContact()
{
int index;
std::string input;
int valid_input = 0;
if (this->contact_count == 0)
{
std::cout << "No contacts available" << std::endl;
return ;
}
while (true)
{
this->displayContacts();
std::cout << "\033[38;5;226mEnter the index of the contact you want to display or type EXIT to return to the main menu\033[0m" << std::endl;
std::getline(std::cin, input);
if (std::cin.eof())
return ;
if (input.empty())
continue;
if (input == "EXIT")
return ;
if (input.length() > 1 || !isdigit(input[0]))
{
std::cout << "Invalid index" << std::endl;
continue;
}
index = input[0] - '0';
if (index >= this->contact_index || index < 0)
{
std::cout << "Contact not found" << std::endl;
continue;
}
valid_input = 1;
break;
}
std::cout << "\033[1m\033[38;5;255mFirst Name: \033[0m" << this->contacts[index].getFirstName() << std::endl;
std::cout << "\033[1m\033[38;5;255mLast Name: \033[0m" << this->contacts[index].getLastName() << std::endl;
std::cout << "\033[1m\033[38;5;255mNick Name: \033[0m" << this->contacts[index].getNickName() << std::endl;
std::cout << "\033[1m\033[38;5;255mPhone Number: \033[0m" << this->contacts[index].getPhoneNumber() << std::endl;
std::cout << "\033[1m\033[38;5;255mDarkest Secret: \033[0m" << this->contacts[index].getDarkestSecret() << std::endl;
}