-
Notifications
You must be signed in to change notification settings - Fork 0
/
C++ file
602 lines (525 loc) · 18.9 KB
/
C++ file
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<fstream>
#include<random>
#include <cstdlib>
#include <cstring>
#include <set>
#include<regex>
#include<ctime>
using namespace std;
class transaction{
public: char name[20];
char phone[11];
char acc_no[12];
const char* age;
char dob[10];
std::string type;
char buffer[150];
char secretPIN[5];
char amount[5];
char total[5];
char email[20];
std::string admin_id;
std::string password;
transaction(){
admin_id = "OurBank@Ifrit";
password = "RA@LLss2024*";}
bool isValidDate(const std::string& date){
regex dateRegex(R"(^(0[1-9]|[1-2][0-9]|3[0-1])/(0[1-9]|1[0-2])/((19|20)\d{2})$)");
return std::regex_match(date, dateRegex);}
bool isValidEmail(const std::string& email) {
regex emailRegex(R"(^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$)");
return regex_match(email, emailRegex);
}
bool isAccountNumberExist(const std::string& accNo){
std::ifstream inputFile("AccountNew.txt");
std::string line;
while (std::getline(inputFile, line)){
std::istringstream iss(line);
std::string accNoFromFile;
std::getline(iss, accNoFromFile, '|');
if (accNoFromFile == accNo) {
inputFile.close();
return true;
}
}
inputFile.close();
return false;
}
bool isSecretPINUnique(const std::string& filename, const char* secretPIN) {
std::ifstream file(filename);
if (!file) {
return true;
}
std::string line;
while (std::getline(file, line)) {
std::string storedSecretPIN = line.substr(line.find_last_of('|') + 1);
if (storedSecretPIN == secretPIN) {
file.close();
return false; // Found a duplicate secret PIN
}
}
file.close();
return true; // Secret PIN is unique
}
int calculateAge(const std::string& dateOfBirth){
// Get the current time
std::time_t currentTime = std::time(nullptr);
std::tm* now = std::localtime(¤tTime);
// Parse the date of birth
std::tm dob = {};
std::istringstream(dateOfBirth.substr(0, 2)) >> dob.tm_mday;
std::istringstream(dateOfBirth.substr(3, 2)) >> dob.tm_mon;
std::istringstream(dateOfBirth.substr(6, 4)) >> dob.tm_year;
// Adjust month and year values
dob.tm_mon -= 1; // Month is zero-based
dob.tm_year -= 1900; // Years since 1900
// Calculate the age
int age = now->tm_year - dob.tm_year;
if (now->tm_mon < dob.tm_mon || (now->tm_mon == dob.tm_mon && now->tm_mday < dob.tm_mday)) {
age--;
}
return age;
}
}rec[20];
fstream file;
transaction t;
std::set<unsigned int> generatedNumbers;
void newuser(){
file.open("AccountNew.txt",ios::app);
if(!file){
std::cout<<"Couldn't open";
exit(1);
}
std::cout<<"Enter your name";
std::cin>>t.name;
std::cout<<"Enter your phone number:";
cin>>t.phone;
if(strlen(t.phone)>10){
std::cout<<"Invalid number";
exit(0);
}
std::cout<<"Enter your date of birth:";
std::cin>>t.dob;
if (t.isValidDate(t.dob)) {
std::cout << "Valid date." << std::endl;
} else {
std::cout << "Invalid date." << std::endl;
return;
}
int Charage = t.calculateAge(t.dob);
std::ostringstream oss;
oss << Charage;
std::string ageStr = oss.str();
t.age = ageStr.c_str();
std::cout<<"Your age is:"<<t.age<<"\n";
std::cout<<"Enter the type of account (e.g., savings, current)\n";
std::cout<<"s-savings,c-current\n";
std::cin>>t.type;
if(t.type=="s"|| t.type=="S"||t.type=="c"||t.type=="C"){
if(t.type=="s"||t.type=="S"){
std::cout<<"You have opted Saving account!"<<std::endl;
}
else{
std::cout<<"You have opted Current Account!"<<std::endl;
}
}
else{
std::cout<<"You must opt saving(s) or current(c) account\n";
return;
}
cout<<"Enter your email-Id";
cin>>t.email;
if (t.isValidEmail(t.email)) {
std::cout << "Valid email address." << std::endl;
} else {
std::cout << "Invalid email address." << std::endl;
}
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_int_distribution<unsigned int> dist(111111111111,999999999999);
unsigned int randomAccNo;
// unsigned int randomAccNo;
do {
randomAccNo = dist(generator);
} while (generatedNumbers.count(randomAccNo) > 0 || t.isAccountNumberExist(std::to_string(randomAccNo)));
generatedNumbers.insert(randomAccNo);
std::string accNoStr = std::to_string(randomAccNo);
std::strcpy(t.acc_no, accNoStr.c_str());
std::cout << "Your Account number is generated: " << t.acc_no << std::endl;
std::cout << "Enter secret PIN: ";
std::cin >> t.secretPIN;
while (!t.isSecretPINUnique("AccountNew.txt", t.secretPIN)) {
std::cout << "Secret PIN already entered. Please enter a different PIN." << std::endl;
std::cin >> t.secretPIN;
}
std::cout<<"please depoist intial amount!!"<<std::endl;
std::cout<<"Please make sure that depoist is more than 1000 Rupees."<<std::endl;
cin>>t.amount;
if (std::stoi(t.amount) >= 1000) {
std::strcpy(t.total, t.amount);
} else {
std::cout << "Please enter an amount greater than 1000 Rupees." << std::endl;
return;
}
std::cout<<"Account Created Successfully!!"<<std::endl;
file<<t.acc_no<<"|"<<t.name<<"|"<<t.type<<"|"<<t.dob<<"|"<<t.age<<"|"<<t.phone<<"|"<<t.email<<"|"<<t.total<<"|"<<t.secretPIN<<std::endl;
file.close();
}
void deposit() {
std::ifstream inputFile("AccountNew.txt");
std::ofstream outputFile("temp.txt");
if (!inputFile || !outputFile) {
std::cout << "Couldn't open the files." << std::endl;
exit(1);
}
std::string acc_number;
std::cout << "Enter the account number: ";
std::cin >> acc_number;
std::string secretPIN;
std::cout << "Enter the secret PIN: ";
std::cin >> secretPIN;
bool accountFound = false;
std::string line;
while (std::getline(inputFile, line)) {
std::istringstream iss(line);
std::string accNoFromFile;
std::string name,type, age, phone, email, total,dob, secretPINFromFile;
std::getline(iss, accNoFromFile, '|');
std::getline(iss, name, '|');
std::getline(iss,type,'|');
std::getline(iss, age, '|');
std::getline(iss,dob,'|');
std::getline(iss, phone, '|');
std::getline(iss, email, '|');
std::getline(iss, total, '|');
std::getline(iss, secretPINFromFile, '\n');
if (accNoFromFile == acc_number && secretPINFromFile == secretPIN) {
int depositAmount;
std::cout << "Enter the amount you want to deposit: ";
std::cin >> depositAmount;
int currentTotal = std::stoi(total);
int newTotal = currentTotal + depositAmount;
total = std::to_string(newTotal);
accountFound = true;
}
outputFile << accNoFromFile << "|" << name << "|"<<type<< "|"<< dob << "|"<<age<<"|"<< phone << "|"
<< email << "|" << total << "|" << secretPINFromFile << std::endl;
}
inputFile.close();
outputFile.close();
if (accountFound) {
std::remove("AccountNew.txt");
std::rename("temp.txt", "AccountNew.txt");
std::cout << "Deposit successful." << std::endl;
} else {
std::remove("temp.txt");
std::cout << "Invalid account number or secret PIN." << std::endl;
}
}
void withdraw() {
std::ifstream inputFile("AccountNew.txt");
std::ofstream outputFile("temp.txt");
if (!inputFile || !outputFile) {
std::cout << "Couldn't open the files." << std::endl;
exit(1);
}
std::string acc_number;
std::cout << "Enter the account number: ";
std::cin >> acc_number;
std::string secretPIN;
std::cout<<"Enter the secret pin";
std::cin>>secretPIN;
std::string line;
bool accountFound = false;
while (std::getline(inputFile, line)) {
std::istringstream iss(line);
std::string accNoFromFile;
std::string name,age,phone,type,email,total,dob,secretPINFromFile;
std::getline(iss,accNoFromFile,'|');
std::getline(iss, name, '|');
std::getline(iss,type,'|');
std::getline(iss, age, '|');
std::getline(iss,dob,'|');
std::getline(iss, phone, '|');
std::getline(iss, email, '|');
std::getline(iss, total, '|');
std::getline(iss, secretPINFromFile, '\n');
if (accNoFromFile == acc_number && secretPINFromFile == secretPIN) {
int withdrawAmount;
std::cout << "Enter the amount you want to withdraw: ";
std::cin >> withdrawAmount;
int currentTotal = std::stoi(total);
if (withdrawAmount <= currentTotal) {
int newTotal = currentTotal - withdrawAmount;
if(newTotal>=1000){
total = std::to_string(newTotal);
outputFile << accNoFromFile << "|" << name <<"|"<<type<< "|" << dob << "|"<<age<<"|" << phone << "|"
<< email << "|" << total << "|" << secretPIN << std::endl;
accountFound = true;
}
else{
std::cout<<"Your account must have a initial amount of 1000 rupees!";
}}
else {
std::cout << "Insufficient funds." << std::endl;
outputFile << line << std::endl; // Copy the line as it is to the temporary file
}
} else {
outputFile << line << std::endl; // Copy the line as it is to the temporary file
}
}
inputFile.close();
outputFile.close();
if (accountFound) {
std::remove("AccountNew.txt"); // Remove the original file
std::rename("temp.txt", "AccountNew.txt"); // Rename the temporary file to the original file name
std::cout << "Withdrawal successful." << std::endl;
} else {
std::remove("temp.txt"); // Remove the temporary file if the account was not found
std::cout << "Invalid Account number or Secret Pin." << std::endl;
}
}
void retrieveRecord(const std::string& searchValue) {
std::ifstream inputFile("AccountNew.txt");
if (!inputFile) {
std::cout << "Couldn't open the file." << std::endl;
return;
}
std::string line;
bool recordFound = false;
while (std::getline(inputFile, line)) {
std::istringstream iss(line);
std::string accNoFromFile;
std::getline(iss, accNoFromFile, '|');
if (accNoFromFile == searchValue) {
std::string name, age, phone,type, email, total,dob, secretPIN;
std::getline(iss, name, '|');
std::getline(iss,type,'|');
std::getline(iss, age, '|');
std::getline(iss,dob,'|');
std::getline(iss, phone, '|');
std::getline(iss, email, '|');
std::getline(iss, total, '|');
std::getline(iss, secretPIN, '\n');
std::cout << "Record found:" << std::endl;
std::cout<<"\n======Account Number\tName\ttype\tAge\tDOB\t\tPhone Number\t\tEmail-Id\tTotal Balance======";
std::cout <<"\n"<<" "<<accNoFromFile<<"\t"<<name<<"\t"<<type<<"\t"<<dob<<"\t"<<age<<"\t\t"<<phone<<"\t"<<email<<"\t"<<total;
recordFound = true;
break; // Stop searching after finding the record
}
}
inputFile.close();
if (!recordFound) {
std::cout << "Record not found." << std::endl;
}
}
void deleteRecord(const std::string& accNumber) {
std::ifstream inputFile("AccountNew.txt");
std::ofstream outputFile("temp.txt");
if (!inputFile || !outputFile) {
std::cout << "Couldn't open the files." << std::endl;
return;
}
std::string line;
bool recordFound = false;
while (std::getline(inputFile, line)) {
std::istringstream iss(line);
std::string accNoFromFile;
std::getline(iss, accNoFromFile, '|');
if (accNoFromFile == accNumber) {
// Skip writing this line to the temporary file
recordFound = true;
} else {
// Write the line to the temporary file
outputFile << line << std::endl;
}
}
inputFile.close();
outputFile.close();
if (recordFound) {
std::remove("AccountNew.txt"); // Remove the original file
std::rename("temp.txt", "AccountNew.txt"); // Rename the temporary file to the original file name
std::cout << "Record deleted successfully." << std::endl;
} else {
std::remove("temp.txt"); // Remove the temporary file if the record was not found
std::cout << "Record not found." << std::endl;
}
}
void updateRecord(const string& accNumber) {
ifstream inputFile("AccountNew.txt");
ofstream outputFile("temp.txt");
if (!inputFile || !outputFile) {
cout << "Couldn't open the files." << endl;
return;
}
string line;
bool recordFound = false;
while (getline(inputFile, line)) {
istringstream iss(line);
string accNoFromFile;
string name, age, phone,type, dob,secretPIN,email,total;
getline(iss, accNoFromFile, '|');
getline(iss, name, '|');
getline(iss,type,'|');
getline(iss, age, '|');
getline(iss,dob,'|');
getline(iss, phone, '|');
getline(iss, email, '|');
getline(iss,total,'|');
getline(iss,secretPIN,'\n');
if (accNoFromFile == accNumber) {
// Update the record
std::cout << "Enter the updated name: ";
std::cin>>name;
cout << "Enter the updated phone number: ";
std::cin>>phone;
cout << "Enter the updated date of birth (DD/MM/YYYY): ";
std::cin>>dob;
if (!t.isValidDate(dob)) {
cout << "Invalid date format. Please enter in the format DD/MM/YYYY." << endl;
return;
}
int Charage = t.calculateAge(dob);
std::ostringstream oss;
oss << Charage;
std::string ageStr = oss.str();
age = ageStr.c_str();
std::cout<<"Your age is:"<<age<<"\n";
cout<<"Enter the updated email";
std::cin>>email;
if (!t.isValidEmail(email)){
std::cout<<"Invalid format of email.Please enter email with \"@\" in it"<<endl;
return;
}
recordFound = true;
}
outputFile << accNoFromFile << "|" << name<<"|"<<type<< "|"<<dob<<"|" <<age<< "|" << phone << "|"<<email<<'|'
<< total << "|"<<secretPIN<<"\n";
}
inputFile.close();
outputFile.close();
if (recordFound) {
remove("AccountNew.txt"); // Remove the original file
rename("temp.txt", "AccountNew.txt"); // Rename the temporary file to the original file name
cout << "Record updated successfully." << endl;
} else {
remove("temp.txt"); // Remove the temporary file
cout << "Record not found." << endl;
}
}
void displayRecords() {
std::ifstream inputFile("AccountNew.txt");
if (!inputFile) {
std::cout << "Couldn't open the file." << std::endl;
return;
}
std::string line;
int recordCount = 0;
std::cout<<"\nAcc_No\t\tName\tType\tDOB\t\tAge\tPhone\t\tEmail\t\tBalance"<<std::endl;
while (std::getline(inputFile, line)) {
std::istringstream iss(line);
std::string field;
int fieldCount = 0;
while (std::getline(iss, field, '|')) {
++fieldCount;
if (fieldCount < 9) {
std::cout << field << "\t";
}
}
std::cout << std::endl;
++recordCount;
}
inputFile.close();
}
int main() {
int ch;
while (1) {
std::cout<<"\n-------------------------------------------------------------------------------\n";
std::cout<<"\n=============================WELCOME TO YOUR BANK==============================\n";
std::cout<<"\n-------------------------------------------------------------------------------\n";
std::cout<<"Our services provided are...\n";
std::cout<<"1.Create an account\n";
std::cout<<"2.Deposit amount\n";
std::cout<<"3.Withdraw amount\n";
std::cout<<"4.Balance Enquiry\n";
std::cout<<"5.Delete Recordn\n";
std::cout<<"6.Update Record\n";
std::cout<<"7.Display All\n";
std::cout <<"\nEnter your choice:";
std::cin >> ch;
switch (ch) {
case 1:
newuser();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4: {
std::string searchValue;
std::cout << "Enter the account number to retrieve the record: ";
std::cin >> searchValue;
retrieveRecord(searchValue);
break;
}
case 5:{
std::string adminid;
std::string pass;
std::cout<<"Enter the Admin-Id";
std::cin>>adminid;
std::cout<<"Enter the admin password";
std::cin>>pass;
if(pass==t.password && adminid==t.admin_id){
std::string acc;
std::cout << "Enter the account number to delete the record: ";
std::cin >> acc;
deleteRecord(acc);}
else
std::cout<<"The password is wrong";
break;
}
case 6:{
std::string adminid;
std::string pass;
std::cout<<"Enter the Admin-Id";
std::cin>>adminid;
std::cout<<"Enter the admin password";
std::cin>>pass;
if(pass==t.password && adminid==t.admin_id){
std::string accNumber;
std::cout << "Enter the account number of the record to update: ";
std::cin>>accNumber;
if (!t.isAccountNumberExist(accNumber)) {
std::cout << "Record not found." << std::endl;
return 0;
}
updateRecord(accNumber);
break;
}}
case 7:{
std::string adminid;
std::string pass;
std::cout<<"Enter the Admin-Id";
std::cin>>adminid;
std::cout<<"Enter the admin password";
std::cin>>pass;
if(pass==t.password && adminid==t.admin_id){
displayRecords();}
else{
std::cout<<"Wrong Admin ID or Password"<<std::endl;
}
break;}
case 8:
return 0;
default:
cout << "Invalid choice. Please try again.\n" << endl;
break;
}
}
return 0;
}