-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoApp.cpp
More file actions
223 lines (198 loc) · 4.94 KB
/
DemoApp.cpp
File metadata and controls
223 lines (198 loc) · 4.94 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//=======================================================
// Includes
//=======================================================
#include "AddressBookInterface.h"
//=======================================================
// ClearAndResetInput : Clear flags and ignore line
//=======================================================
static void ClearAndResetInput()
{
std::cout << "Please enter a number!" << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
//=======================================================
// ErrToString : Convert error to string
//=======================================================
static const std::string ErrToString(const AddressEntryError& error)
{
switch (error)
{
case AddressEntryError::kAddressEntrySuccess:
return "Success!";
case AddressEntryError::kAddressEntryDuplicate:
return "Duplicate found!";
case AddressEntryError::kAddressEntryInvalid:
return "Entry invalid!";
case AddressEntryError::kAddressEntryNotFound:
return "Entry not found!";
case AddressEntryError::kAddressEntryNotAttempted:
return "Action not attempted";
default:
return std::string();
}
}
//=======================================================
// GetAddressEntryOrderType : Get entry search type
//=======================================================
const AddressEntryOrderType GetAddressEntryOrderType()
{
while (true)
{
std::cout << "Please choose from the following order types:\n1. First Name Order\n2. Last Name Order" << std::endl;
int option = 0;
if (std::cin >> option)
{
switch (option)
{
case 1:
return AddressEntryOrderType::FirstNameOrder;
case 2:
return AddressEntryOrderType::LastNameOrder;
default:
std::cout << "Invalid order type! please try again." << std::endl;
break;
}
}
else
{
ClearAndResetInput();
}
}
}
//=======================================================
// GetEntrySearchType : Get entry search type
//=======================================================
const AddressEntrySearchType GetEntrySearchType()
{
while (true)
{
std::cout << "Please choose from the following search types:\n1. First Name Search\n2. Last Name Search\n3. First and Last Name Search" << std::endl;
int option = 0;
if (std::cin >> option)
{
switch (option)
{
case 1:
return AddressEntrySearchType::FirstNameSearch;
case 2:
return AddressEntrySearchType::LastNameSearch;
case 3:
return AddressEntrySearchType::FirstAndLastNameSearch;
default:
std::cout << "Invalid search type! please try again." << std::endl;
break;
}
}
else
{
ClearAndResetInput();
}
}
}
//=======================================================
// main
//=======================================================
int main()
{
std::cout << "Hello, welcome to the Simple Address Book demo!" << std::endl;
while (true)
{
std::cout << "\nPlease choose from the following options below:\n"
<< "1. Add entry\n2. Remove entry\n3. Show entries\n4. Search entries\n5. Clear entries\n0. Exit\n\n";
int option = 0;
if (std::cin >> option)
{
switch (option)
{
case 1:
{
std::cout << "Provide address entry to add:" << std::endl;
AddressEntry entryToAdd;
if (std::cin >> entryToAdd)
{
auto result = AddressBookInterface::AddEntry(entryToAdd);
std::cout << ErrToString(result);
}
break;
}
case 2:
{
std::cout << "Provide address entry to remove:" << std::endl;
AddressEntry entryToRemove;
if (std::cin >> entryToRemove)
{
auto result = AddressBookInterface::RemoveEntry(entryToRemove);
std::cout << ErrToString(result);
}
break;
}
case 3:
{
auto result = AddressBookInterface::RetrieveEntries(GetAddressEntryOrderType());
if (result.empty())
{
std::cout << "Address book is empty!" << std::endl;
}
else
{
std::cout << "Entries as follows:" << std::endl;
for (const auto& entry : result)
{
std::cout << entry;
}
}
break;
}
case 4:
{
std::cout << "Please enter search key: ";
std::string input;
if (std::cin >> input)
{
auto result = AddressBookInterface::Search(input, GetEntrySearchType());
if (result.empty())
{
std::cout << "No entries found!" << std::endl;
}
else
{
std::cout << "Entries found:" << std::endl;
for (const auto& entry : result)
{
std::cout << entry;
}
}
}
break;
}
case 5:
{
AddressBookInterface::Clear();
std::cout << "Address book cleared!" << std::endl;
break;
}
case 0:
{
std::cout << "Thank you for trying this demo, bye!" << std::endl;
return 0;
}
default:
{
std::cout << "Invalid option! Please try again." << std::endl;
break;
}
}
}
else
{
if (std::cin.eof())
{
std::cout << "user aborted." << std::endl;
return 1;
}
ClearAndResetInput();
}
}
return 0;
}