-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneBook.cpp
More file actions
101 lines (88 loc) · 2.65 KB
/
Copy pathPhoneBook.cpp
File metadata and controls
101 lines (88 loc) · 2.65 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
#include "PhoneBook.hpp"
// Constructor
PhoneBook::PhoneBook()
{
std::cout << "------------------------------------\n"
<< "Shalom! You can use me in 3 ways:\n\n" <<
"ADD - save a new contact\n" << "SEARCH - display a specific contact\n"
<< "EXIT - goodbye\n" << "------------------------------------\n" << std::endl;
}
// Static variable initialization
int PhoneBook::index = 0;
// Prints the contact chosen by the input
void PhoneBook::display_contact(int i)
{
std::cout << "First Name: " << this->contacts[i].get_first_name("normal") << std::endl;
std::cout << "Last Name: " << this->contacts[i].get_last_name("normal") << std::endl;
std::cout << "Nickname: " << this->contacts[i].get_nickname("normal") << std::endl;
std::cout << "Phone Number: " << this->contacts[i].get_phone_number() << std::endl;
std::cout << "Darkest Secret: " << this->contacts[i].get_darkest_secret() << std::endl;
}
// Prints the Phonebook list and asks user for index to diplay
void PhoneBook::print_columns(void)
{
int i = 0;
if (index == 0)
return;
for (i = 0; i < 8; i++)
{
if (!this->contacts[i].get_first_name("sub").empty())
{
std::cout << "|" << std::setw(10) << i;
std::cout << "|" << std::setw(10) << this->contacts[i].get_first_name("sub");
std::cout << "|" << std::setw(10) << this->contacts[i].get_last_name("sub");
std::cout << "|" << std::setw(10) << this->contacts[i].get_nickname("sub");
std::cout << "|" << std::endl;
}
else
break;
}
std::cout << "Contact index:" << std::endl;
int num;
std::cin >> num;
if (std::cin.eof())
std::exit(0);
if (std::cin.fail() || num < 0 || num > 7)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "invalid index enter SEARCH to try again" << std::endl;
return;
}
if (num < i)
display_contact(num);
else
std::cout << "invalid index enter SEARCH to try again" << std::endl;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// Adds a new contact to the phonebook
void PhoneBook::add_contact(void)
{
Contact new_contact;
new_contact.get_new_contact();
if (!new_contact.check_if_valid())
{
std::cout << "Invalid Contact, try again" << std::endl;
return;
}
if (index == 8)
index = 0;
this->contacts[index++] = new_contact;
}
// Get's the input from user and runs the command
std::string PhoneBook::get_action(void)
{
std::string input;
std::cout << "< ";
std::getline(std::cin, input);
if (std::cin.eof())
std::exit(0);
if (input == "ADD")
return (add_contact(),"ADD");
else if (input == "SEARCH")
return (print_columns(),"SEARCH");
else if (input == "EXIT")
return ("EXIT");
else
return ("INVALID");
}