-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportalmodule.cpp
More file actions
490 lines (441 loc) · 17.7 KB
/
portalmodule.cpp
File metadata and controls
490 lines (441 loc) · 17.7 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <regex> // required for generating custom patterns for string validation
#include "portal.h"
using namespace std;
// function manages the login process
bool login::checklogindata(string inputusername, string inputpassword, string username, string password)
{
// stores the data taken from username.txt and password.txt in username and password string
ifstream infile("username.txt");
if (infile.is_open())
{
(getline(infile, username));
infile.close();
}
ifstream fileinput("password.txt");
if (fileinput.is_open())
{
(getline(fileinput, password));
fileinput.close();
}
// initializes the value of valid to 0 or false
bool valid = 0;
cout << "Please enter your username: ";
cin >> inputusername;
cout << "Please enter your password: ";
cin >> inputpassword;
// valid is set to 1 or true if the input username and password match the values in the username and password string
if ((inputusername == username) && (inputpassword == password))
{
cout << "Login successful." << endl;
valid = 1;
}
// valid remains at 0 or false if the input username and password do not match the values in the username and password string
else if ((inputusername != username) or (inputpassword != password))
{
cout << "Invalid username or password." << endl;
valid = 0;
}
// value of valid is returned
return valid;
}
// function adds an entry into the csv file
void dataentry::addentry(string firstname, string middlename, string lastname, int countrycode, long long int personid, long long int phone, string email, int dob, string mob, int yob, string gender)
{
ifstream inputfile("database.csv");
if (!inputfile.is_open())
{
cerr << "Unable to open database." << endl;
exit(1);
}
// Find the maximum personid in the database
long long int max_personid = 0;
string line;
while (getline(inputfile, line))
{
istringstream ss(line);
long long int id;
ss >> id;
if (id > max_personid)
{
max_personid = id;
}
}
inputfile.close();
// Set the new personid to the maximum personid + 1
personid = max_personid + 1;
cout << "Enter first name: ";
cin >> firstname;
regex firstName("[A-Za-z]+");
while (!regex_match(firstname, firstName))
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid first name input. Please enter a valid first name: ";
cin >> firstname;
}
cout << "Enter middle name: ";
cin >> middlename;
regex middleName("[A-Za-z]+");
while (!regex_match(middlename, middleName) && (middlename != "-"))
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid middle name input. Please enter a valid middle name: ";
cin >> middlename;
}
cout << "Enter last name: ";
cin >> lastname;
regex lastName("[A-Za-z]+");
while (!regex_match(lastname, lastName))
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid last name input. Please enter a valid last name: ";
cin >> lastname;
}
cout << "Enter phone number (without country code): ";
cin >> phone;
while ((phone <= 0) || cin.fail())
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Please enter a valid phone number: ";
cin >> phone;
}
cout << "Enter country code (Eg: 1, 44, etc.): ";
cin >> countrycode;
while ((countrycode <= 0) || cin.fail())
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Please enter a valid country code (omit the '+' sign): ";
cin >> countrycode;
}
cout << "Enter E-mail I.D.: ";
cin >> email;
// check if input format is valid
regex pattern("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$");
while (!regex_match(email, pattern))
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid email input. Please enter a valid email address: ";
cin >> email;
}
cout << "Enter date of birth (Eg: 21, 15, etc.): ";
cin >> dob;
// check if input is valid
while (cin.fail() || dob < 1 || dob > 31)
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid date input. Please enter a value between 1 and 31: ";
cin >> dob;
}
cout << "Enter month of birth (Eg: January, September, etc.): ";
cin >> mob;
while ((mob != "January") and (mob != "February") and (mob != "March") and (mob != "April") and (mob != "May") and (mob != "June") and (mob != "July") and (mob != "August") and (mob != "September") and (mob != "October") and (mob != "November") and (mob != "December") or cin.fail())
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid month input. Please enter a valid month: ";
cin >> mob;
}
while ((dob > 30) and ((mob == "April") or (mob == "June") or (mob == "September") or (mob == "November")))
{
cout << "The month of " << mob << " has only 30 days. Please enter the correct date: ";
cin >> dob;
}
while ((dob > 31) and ((mob == "January") or (mob == "March") or (mob == "May") or (mob == "July") or (mob == "August") or (mob == "October") or (mob == "December")))
{
cout << "The month of " << mob << " has only 31 days. Please enter the correct date: ";
cin >> dob;
}
cout << "Enter year of birth (Eg: 1987, 2006, etc.): ";
cin >> yob;
while ((yob < 1850) or (yob > 2024))
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid year input. Please enter a value between 1850 and 2024: ";
cin >> yob;
}
while ((dob > 28) and (mob == "February") and (yob % 4 != 0))
{
cout << "February " << yob << " has only 28 days. Please enter a valid date: ";
cin >> dob;
}
while ((dob > 29) and (mob == "February") and (yob % 4 == 0))
{
cout << "February " << yob << " has only 29 days. Please enter a valid date: ";
cin >> dob;
}
cout << "Enter gender (Eg: Male, Female, etc.): ";
cin >> gender;
regex Gender("[A-Za-z]+");
while (!regex_match(firstname, Gender))
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Please enter a valid gender: ";
cin >> gender;
}
ofstream outfile;
outfile.open("database.csv", ios::app);
// the values stored in the variables are entered in the csv file.
outfile << personid << "," << firstname << "," << middlename << "," << lastname << "," << countrycode << "," << phone << "," << email << "," << dob << "," << mob << "," << yob << "," << gender << endl;
outfile.close();
}
// function displays all the data in the csv file
void dataentry::showdata()
{
string line;
ifstream inFile("database.csv");
cout << endl;
cout << "Output displayed in the order: user I.D., First Name, Middle Name, Last Name, Country Code, Phone Number, Email, Date of Birth, Month of Birth, Year of Birth, Gender" << endl;
cout << endl;
if (inFile.is_open())
{
// every line from the csv file is stored in the 'line' string, which is then displayed
while (getline(inFile, line))
{
cout << line << endl;
}
inFile.close();
}
cout << endl;
}
// function displays a single data entry based on the person I.D.
void dataentry::showentry(long long int personid)
{
string line;
vector<string> lines;
ifstream INFile("database.csv");
int lineNumber = 0;
if (INFile.is_open())
{
while (getline(INFile, line))
{
lineNumber++;
stringstream ss(line);
string field;
getline(ss, field, ',');
if (stoll(field) == personid)
{
cout << endl;
cout << line << endl;
cout << endl;
}
}
INFile.close();
}
else
{
cout << "Unable to open file" << endl;
}
}
// function updates a data entry based on the person I.D.
void dataentry::updateentry(string firstname, string middlename, string lastname, int countrycode, long long int personid, long long int phone, string email, int dob, string mob, int yob, string gender)
{
string line;
vector<string> lines;
ifstream INFILE("database.csv");
int lineNumber = 0;
bool personFound = false;
if (INFILE.is_open())
{
while (getline(INFILE, line))
{
lineNumber++;
stringstream ss(line);
string field;
getline(ss, field, ',');
if (stoll(field) == personid)
{
personFound = true;
// formats the updated data and adds it to a vector of strings called lines
stringstream updatedData;
// user enters new data. the same logic as addentry() is followed
cout << "Enter first name: ";
cin >> firstname;
regex firstName("[A-Za-z]+");
while (!regex_match(firstname, firstName))
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid first name input. Please enter a valid first name: ";
cin >> firstname;
}
cout << "Enter middle name: ";
cin >> middlename;
regex middleName("[A-Za-z]+");
while (!regex_match(middlename, middleName) && (middlename != "-"))
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid middle name input. Please enter a valid middle name: ";
cin >> middlename;
}
cout << "Enter last name: ";
cin >> lastname;
regex lastName("[A-Za-z]+");
while (!regex_match(lastname, lastName))
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid last name input. Please enter a valid last name: ";
cin >> lastname;
}
cout << "Enter phone number (without country code): ";
cin >> phone;
while ((phone <= 0) || cin.fail())
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Please enter a valid phone number: ";
cin >> phone;
}
cout << "Enter country code (Eg: 1, 44, etc.): ";
cin >> countrycode;
while ((countrycode <= 0) || cin.fail())
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Please enter a valid country code (omit the '+' sign): ";
cin >> countrycode;
}
cout << "Enter E-mail I.D.: ";
cin >> email;
// check if input format is valid
regex pattern("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$");
while (!regex_match(email, pattern))
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid email input. Please enter a valid email address: ";
cin >> email;
}
cout << "Enter date of birth (Eg: 21, 15, etc.): ";
cin >> dob;
// check if input is valid
while (cin.fail() || dob < 1 || dob > 31)
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid date input. Please enter a value between 1 and 31: ";
cin >> dob;
}
cout << "Enter month of birth (Eg: January, September, etc.): ";
cin >> mob;
while ((mob != "January") and (mob != "February") and (mob != "March") and (mob != "April") and (mob != "May") and (mob != "June") and (mob != "July") and (mob != "August") and (mob != "September") and (mob != "October") and (mob != "November") and (mob != "December") or cin.fail())
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid month input. Please enter a valid month: ";
cin >> mob;
}
while ((dob > 30) and ((mob == "April") or (mob == "June") or (mob == "September") or (mob == "November")))
{
cout << "The month of " << mob << " has only 30 days. Please enter the correct date: ";
cin >> dob;
}
while ((dob > 31) and ((mob == "January") or (mob == "March") or (mob == "May") or (mob == "July") or (mob == "August") or (mob == "October") or (mob == "December")))
{
cout << "The month of " << mob << " has only 31 days. Please enter the correct date: ";
cin >> dob;
}
cout << "Enter year of birth (Eg: 1987, 2006, etc.): ";
cin >> yob;
while ((yob < 1850) or (yob > 2024))
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Invalid year input. Please enter a value between 1850 and 2024: ";
cin >> yob;
}
while ((dob > 28) and (mob == "February") and (yob % 4 != 0))
{
cout << "February " << yob << " has only 28 days. Please enter a valid date: ";
cin >> dob;
}
while ((dob > 29) and (mob == "February") and (yob % 4 == 0))
{
cout << "February " << yob << " has only 29 days. Please enter a valid date: ";
cin >> dob;
}
cout << "Enter gender (Eg: Male, Female, etc.): ";
cin >> gender;
regex Gender("[A-Za-z]+");
while (!regex_match(firstname, Gender))
{
cin.clear(); // clear error flags
cin.ignore(100, '\n'); // ignore remaining characters in buffer
cout << "Please enter a valid gender: ";
cin >> gender;
}
updatedData << personid << "," << firstname << "," << middlename << "," << lastname << "," << countrycode << "," << phone << "," << email << "," << dob << "," << mob << "," << yob << "," << gender << "\n";
lines.push_back(updatedData.str());
}
else
{
// If the record is not found, the original line from the file is added to the lines vector.
lines.push_back(line + "\n");
}
}
INFILE.close();
if (!personFound)
{
cout << "Person with ID " << personid << " not found." << endl;
return;
}
ofstream OUTFILE("database.csv");
for (int i = 0; i < lines.size(); i++)
{
OUTFILE << lines[i];
}
OUTFILE.close();
cout << "Entry updated successfully." << endl;
}
else
{
cout << "Unable to open file." << endl;
}
}
// function deletes an entry based on the person I.D.
void dataentry::deleteentry(long long int personid)
{
string line;
vector<string> lines;
ifstream InFILE("database.csv");
int lineNumber = 0;
if (InFILE.is_open())
{
while (getline(InFILE, line))
{
lineNumber++; // increments lineNumber in every iteration
stringstream ss(line); // separates each field by a comma
string field;
getline(ss, field, ',');
if (stoll(field) != personid)
{
lines.push_back(line); // the data in 'line' is added to 'lines' if the personid and stoll(field) are not equal
}
}
InFILE.close();
}
else
{
cout << "Unable to open file" << endl;
}
ofstream OUTFILE("database.csv");
for (string l : lines)
{
OUTFILE << l << endl;
}
OUTFILE.close();
}