-
Notifications
You must be signed in to change notification settings - Fork 0
/
fct.cpp
121 lines (87 loc) · 2.98 KB
/
fct.cpp
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
/**
* @file fct.cpp
* @author Anthony D.
* @date 21 avril 2023
*
* @brief Contains the functions' core
* @see fct.hpp
*/
#include "fct.hpp"
void CreateContact(GlobalMap &gl) {
putchar('\n');
// create new contact
Contact newContact;
// new variable init for user input
std::string newName;
std::cout << "Please enter the contact's name : ";
std::cin >> newName;
// check if contact already exists
if (!gl.count(newName)) {
// ask user for phone number
std::string newNumber;
std::cout << "Please enter the contact's phone number : ";
std::cin >> newNumber;
// create contact
newContact.insert({NAME,newName});
newContact.insert({NUMBER, newNumber});
// ask about adding email
AddEmailToContact(newContact);
// add contact to registery
gl.insert({newName, newContact});
} else {
std::cout << newName << " already exists." << std::endl;
}
}
void DeleteContact(GlobalMap &gl) {
putchar('\n');
// ask user which contact to delete
std::string contactToDelete;
std::cout << "Which contact would you like to delete from your registery? ";
std::cin >> contactToDelete;
// delete user if it exists (Map.erase returns the number of deleted elements)
(gl.erase(contactToDelete) == 0) ?
std::cout << "Contact has not been found in the registery." << std::endl :
std::cout << contactToDelete << " has been deleted..." << std::endl;
}
void DisplayContact(GlobalMap &gl) {
putchar('\n');
// ask user contact to check
std::string contactToCheck;
std::cout << "Which contact do you wanna check? ";
std::cin >> contactToCheck;
putchar('\n');
// check if contact exists using iterators
GlobalMap::const_iterator it = gl.begin();
if ((it = gl.find(contactToCheck)) != gl.end()) {
for (auto const &info : it->second) {
std::cout << info.first << " : " << info.second << std::endl;
}
} else {
std::cout << "Contact has not been found in the registery." << std::endl;
}
}
void AddEmailToContact(Contact &co) {
u_char userChoice;
std::cout << "Would you like to provide an email for? (y/n) ";
std::cin >> userChoice;
if ((userChoice == 'Y') || (userChoice == 'y')) {
// ask user to enter mail
std::string newMail;
std::cout << "Please enter the email ";
std::cin >> newMail;
// add mail to contact
co.insert({EMAIL, newMail});
}
}
uint8_t WelcomeMenu() {
std::cout << "\n\n";
std::cout << " Welcome to your contact registery !" << std::endl;
putchar('\n');
std::cout << "[1] Search for contact" << std::endl;
std::cout << "[2] Add contact to registery" << std::endl;
std::cout << "[3] Delete contact from registery" << std::endl;
uint8_t userChoice;
std::cout << "Choose an available option : ";
std::cin >> userChoice;
return userChoice;
}