-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
250 lines (239 loc) · 8.74 KB
/
main.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* Copyright Navid Mafi Ranji
This source code is provided only for reference purposes. This is not a public domain source code.
All rights reserved.
*/
#include <iostream>
#include <sqlite3.h>
#include "ContactController.h"
#include "DisplayController.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
// init static db pointer
sqlite3 *ContactController::db;
int ContactController::dbSize = 0;
int main(void)
{
// Init DB
ContactController::openDatabase();
ContactController::initDatabase();
ContactController::readDBSize();
while (true)
{
DisplayController::showMainMenu();
// user input
int mainMenuInput = DisplayController::readInput(1, 8);
switch (mainMenuInput)
{
case 1:
// creating a local scope because C++ standard forbids a goto to bypass an initialization of a non-POD object
{
ContactType newContact;
newContact = DisplayController::readContactInput();
ContactController::addContact(newContact);
break;
}
case 2:
DisplayController::clearScreen();
DisplayController::showContactListHeader();
ContactController::listContacts();
if (ContactController::dbSize == 0)
{
logger("userInput", "No contact to show details, press enter to go back");
cin.get();
}
else
{
logger("userInput", "Enter contact ID to show details");
int subInput;
subInput = DisplayController::readInput(1, ContactController::dbSize);
ContactController::showSingleContact(subInput);
cin.get();
}
break;
case 3:
DisplayController::clearScreen();
ContactController::listContacts();
if (ContactController::dbSize == 0)
{
logger("userInput", "No contact to edit, press enter to go back");
cin.get();
}
else
{
logger("userInput", "Enter contact ID to edit");
int subInput;
subInput = DisplayController::readInput(1, ContactController::dbSize);
ContactController::showSingleContact(subInput);
logger("userInput", "enter what info do you want to edit.");
logger("userInput", "pass empty input to not edit anything");
logger("userInput", " 1- First name 2- Last name 3- Address 4- Email 5- Phone Number");
int userSelection;
userSelection = DisplayController::readInput(1, 6);
// This is a dirty and lazy way to do it. But since there is no repetition and we only use this logic in one place, it's fine
switch (userSelection)
{
// Using new scopes in switch cases are necessary. It's not a programmer mistake.
case 1:
{
logger("userInput", "Enter new firstName: ");
string newFirstName;
newFirstName = DisplayController::readOptionalInput<string>();
if (newFirstName != "Unknown")
{
ContactController::editContactField(subInput, "LASTNAME", newFirstName);
}
else
{
logger("info", "No change made");
}
break;
}
case 2:
{
logger("userInput", "Enter new lastName: ");
string newLastName;
newLastName = DisplayController::readOptionalInput<string>();
if (newLastName != "Unknown")
{
ContactController::editContactField(subInput, "LASTNAME", newLastName);
}
else
{
logger("info", "No change made");
}
break;
}
case 3:
{
logger("userInput", "Enter new address : ");
string newAddress;
newAddress = DisplayController::readOptionalInput<string>();
if (newAddress != "Unknown")
{
ContactController::editContactField(subInput, "ADDRESS", newAddress);
}
else
{
logger("info", "No change made");
}
break;
}
case 4:
{
logger("userInput", "Enter new email: ");
string newEmail;
newEmail = DisplayController::readOptionalInput<string>();
if (newEmail != "Unknown")
{
ContactController::editContactField(subInput, "EMAIL", newEmail);
}
else
{
logger("info", "No change made");
}
break;
}
case 5:
{
logger("userInput", "Enter new phoneNumber: ");
string newPhoneNumber;
newPhoneNumber = DisplayController::readOptionalInput<string>();
if (newPhoneNumber != "Unknown")
{
ContactController::editContactField(subInput, "PHONENUMBER", newPhoneNumber);
}
else
{
logger("info", "No change made");
}
break;
}
default:
logger("info", "Invalid input");
break;
}
}
break;
break;
case 4:
DisplayController::clearScreen();
ContactController::listContacts();
if (ContactController::dbSize == 0)
{
logger("userInput", "No contact to delete, press enter to go back");
cin.get();
}
else
{
logger("userInput", "Enter contact ID to delete");
int subInput;
subInput = DisplayController::readInput(1, ContactController::dbSize);
// confirm deletion
logger("userInput", "Are you sure you want to delete this contact? (y/n)");
char userSelection;
// using ASCII char number :D
userSelection = cin.get();
if (userSelection == 'y')
{
ContactController::deleteContact(subInput);
}
else
{
logger("info", "No change made");
}
}
break;
case 5:
DisplayController::confirmDBClear();
int confirmInput;
confirmInput = DisplayController::readInput(1, 2);
if (confirmInput == 1)
{
ContactController::clearDatabase();
}
else
{
logger("info", "DB clear cancelled");
}
break;
case 6:
{
DisplayController::clearScreen();
DisplayController::showContactSearchHeader();
cout << "Enter a name to search for: ";
string searchString;
cin >> searchString;
ContactController::findContact(searchString);
logger("userInput", "enter id to show details or enter 0 to go back");
int subInput;
subInput = DisplayController::readInput(0, ContactController::dbSize);
if (subInput == 0)
{
break;
}
ContactController::showSingleContact(subInput);
}
cout << "Press Enter to continue" << endl;
cin.get();
break;
case 7:
DisplayController::clearScreen();
DisplayController::showAbout();
cout << "Press Enter to continue" << endl;
cin.get();
break;
case 8:
cout << "bye." << endl;
exit(0);
default:
// we should never get here because we provided min and max for readInput
logger("error", "Invalid input (unhandled)");
break;
}
}
// Cleanup
ContactController::closeDatabase();
return EXIT_SUCCESS;
}