-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclone.cpp
More file actions
861 lines (717 loc) · 20.7 KB
/
clone.cpp
File metadata and controls
861 lines (717 loc) · 20.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
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
//Continue testing and return all the function calls into main function, and bring the clear screen function and stabilixe it where possible.
#include<bits/stdc++.h>
#include<cstdlib>
#include<filesystem>
#include<string>
#include<stdexcept>
#include<windows.h>
#ifdef _WIN32
#endif
using namespace std;
set<string> months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
class check_input{
private:
string input;
public:
void assign(string inp){input = inp;}
bool check(){
for(int i = 0; i < input.size(); i++){
if(!isdigit(input[i])){
return false;
}
}
return true;
}
};
void add_universal_file(string fname, string accountnum, string password, string pin);
void deposit(string accountnum);
string registration();
string acc_correction(int num);
bool u_check(string name);
bool acc_num_check(string acc_num);
char options();
char intro();
string date_time();
int transfer(string, string);
void transfer_update_history(string, string, int, int, int);
bool login(string* name);
bool withdraw(string, string);
int operation_check();
void withdrawal_update_history(string, int, int);
void deposit_update_history(string a_number, int amount, int bal);
void clearConsole();
string universal_extract(string);
string check_balance(string acc_num, string);
void balance_update_history(string, string);
void enableVTMode();
void clearUserInputOnly();
bool check_check_input(string pin);
bool check_pin(string, string);
int main(){
clearConsole();
char ans = intro();
if(ans == '1'){
lc:
clearConsole();
string username;
bool login_check = login(&username);
clearConsole();
if(login_check == true){
string acc_num = universal_extract(username);
cout << "Login Successful\n";
cout << "\tACCESS GRANTED ^_^\n\n";
char option_check = options();
clearConsole();
if(option_check == 1){
bool withdraw_check = withdraw(acc_num, username);
clearConsole();
if(withdraw_check == true){
cout << "Transaction Successful.\n";
}else{
cout << "\tInsufficient balance/Invalid PIN!\n";
for(int i = 0; i < 3; i++){
cout << "\t....................\n";
}
Sleep(3000);
}
int oc_check = operation_check();
if(oc_check == 1){
goto lc;
}else if(ans == 2){
main();
}
}else if(option_check == 2){
int transfer_check = transfer(acc_num, username);
clearConsole();
if(transfer_check == 1){
cout << "Transaction Successful.\n";
}else if(transfer_check == 2){
cout << "Invalid reciever's detail.\n";
}else if(transfer_check == 3){
cout << "Insufficient balance.\n";
for(int i = 0; i < 3; i++){
cout << "\t.....................\n";
}
Sleep(3000);
}else if(transfer_check == 5){
cout << "Invalid Transaction PIN.\n";
for(int i = 0; i < 3; i++){
cout << "\t.....................\n";
}
Sleep(3000);
}
int oc_check = operation_check();
if(oc_check == 1){
goto lc;
}else if(ans == 2){
main();
}
}else if(option_check == 3){
deposit(acc_num);
clearConsole();
cout << "Transaction Successful.\n";
int oc_check = operation_check();
if(oc_check == 1){
goto lc;
}else if(ans == 2){
main();
}
}else if(option_check == 4){
string balance_n = check_balance(acc_num, username);
cout << "Your balance is " << balance_n << endl;
Sleep(3000);
clearConsole();
int oc_check = operation_check();
if(oc_check == 1){
goto lc;
}else if(ans == 2){
main();
}
}else if(option_check > 5){
clearConsole();
int oc_check = operation_check();
if(oc_check == 1){
goto lc;
}else if(ans == 2){
main();
}
}
}else{
clearConsole();
cout << "Invalid Login Details.\n";
for(int i = 0; i < 3; i++){
cout << "\t.....................\n";
}
Sleep(3000);
main();
}
}
if(ans == '2'){
clearConsole();
string account_n = registration();
clearConsole();
cout << "\nYOUR ACCOUNT NUMBER IS: " << account_n << ".\n\t\t*Copy it down somewhere.\n";
cout << "\nDo you want to make a Deposit?\n\t1. Yes\n\t2.No\n";
int ans;
cin >> ans;
if(ans == 1){
deposit(account_n);
}else{
main();
}
}
if(ans != '1' || ans != '2'){
main();
}
return 0;
}
// Enable ANSI escape codes on Windows 10+
void enableVTMode() {
#ifdef _WIN32
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE) return;
DWORD dwMode = 0;
if (!GetConsoleMode(hOut, &dwMode)) return;
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
#endif
}
// Deletes the typed input after Enter, keeping prompt intact
void clearUserInputOnly() {
// Move cursor up, go to end of prompt, clear from there
std::cout << "\x1b[1A" // Move cursor up one line
<< "\x1b[s" // Save cursor position
<< "\x1b[999C" // Move far right (max columns)
<< "\x1b[1K" // Clear from cursor to start of line
<< "\x1b[u"; // Restore cursor to saved position
std::cout.flush();
}
void clearConsole(){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = {0, 0};
if (hConsole == INVALID_HANDLE_VALUE) return;
// Get console buffer info
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) return;
cellCount = csbi.dwSize.X * csbi.dwSize.Y;
// Fill the entire buffer with spaces
if (!FillConsoleOutputCharacter(hConsole, (TCHAR)' ', cellCount, homeCoords, &count)) return;
// Fill with the current colors and attributes
if (!FillConsoleOutputAttribute(hConsole, csbi.wAttributes, cellCount, homeCoords, &count)) return;
// Move the cursor home
SetConsoleCursorPosition(hConsole, homeCoords);
}
string date_time(){
auto now = chrono::system_clock::now();
time_t currentTime = chrono::system_clock::to_time_t(now);
tm localTime{};
#ifdef _WIN32
localtime_s(&localTime, ¤tTime);
#else
localtime_r(¤tTime, &localTime);
#endif
ostringstream oss;
oss << put_time(&localTime, "%Y-%m-%d %H:%M:%S");
return oss.str();
}
string acc_correction(int num){
string s = to_string(num);
while (s.length() < 10) {
s = "0" + s;
}
return s;
}
void add_universal_file(string fname, string accountnum, string password, string pin){
fstream myfile;
string bankname = "Major_";
string ext = ".txt";
//string temp = fname;
//bankname.appenD(fname);
bankname.append(fname);
bankname.append(ext);
myfile.open(bankname, ios::out);
myfile << accountnum << endl;
myfile << password << endl;
mysile << pin << endl;
myfile.close();
}
char intro(){
cout << "\tHello, How's your day?\n";
cout << "\tWhat operation do you want to do today?\n";
cout << "\t1. Login.\n\t2. Register.\n";
char ans;
cin >> ans;
return ans;
}
string universal_extract(string name){
fstream myfile;
string ext = "Major_";
string ext2 = ".txt";
ext.append(name);
ext.append(ext2);
myfile.open(ext);
string line;
getline(myfile, line);
myfile.close();
return line;
}
bool check_pin(string uname, string pin){
fstream myfile;
string ext1 = "Dbank_";
string ext2 = ".txt";
ext1.append(uname);
ext1.append(ext2);
myfile.open(ext1, ios::in);
int linenum = 0;
string line;
while(getline(myfile, line)){
linenum++;
if(linenum == 3 && line == pin){
return true;
}
}
return false;
}
bool u_check(string name){
fstream my_file;
string bankname = "Major_";
string ext = ".txt";
bankname.append(name);
bankname.append(ext);
//string filename = bankname;
my_file.open(bankname, ios::in);
if(!my_file.is_open()){
return false;
}else{
my_file.close();
return true;
}
// string line;
// int linenum = 0;
// while(getline(my_file, line)){
// linenum++;
// }
// my_file.close();
// if(linenum <= 1){
// filesystem::remove(bankname);
// return false;
// }else{
// return true;
// }
}
bool acc_num_check(string acc_num){
fstream myfile;
string ext = "_info.txt";
string bankname = "Dbank_";
//bool check = false;
bankname.append(acc_num);
bankname.append(ext);
myfile.open(bankname, ios::in);
if(myfile.fail()){
return false;
}
return true;
}
int operation_check(){
cout << "Do you want to perform another transaction?\n\t1. Yes\n\t2. No\n";
int ans;
cin >> ans;
while(ans > 2 || ans < 1){
cout << "Invalid option.\n";
cin >> ans;
}
return ans;
}
bool login(string* name){
fstream myfile;
string username;
string password;
string temp;
string ext = "Major_";
string ext2 = ".txt";
//uc:
cout << "Enter your username: ";
cin >> username;
cout << "Enter your password: ";
cin >> password;
bool lc = u_check(username);
if(lc == false){
return false;
}
*name = username;
ext.append(username);
ext.append(ext2);
myfile.open(ext, ios::in);
int linenum = 0;
string line;
while(getline(myfile, line)){
linenum++;
if(linenum == 2 && line != password){
myfile.close();
return false;
}
}
myfile.close();
return true;
}
char options(){
char ans;
cout << "\t1. Withdraw\n\t2. Transfer\n\t3. Deposit\n\t4. Balance\n\t5. Exit\n";
cin >> ans;
int temp =
while(ans > 5 || ans < 0){
cout << "Invalid option!\n";
cin >> ans;
}
return ans;
}
bool withdraw(string a_num, string usern){
fstream myfile;
string ext = "Dbank_";
string ext2 = "_balance.txt";
string temp = a_num;
ext.append(a_num);
ext.append(ext2);
myfile.open(ext, ios::in);
string balance;
getline(myfile, balance);
myfile.close();
int line;
try{
line = stoi(balance);
}catch(...){
line = 0;
}
cout << "How much do you want to withdraw?\n";
int ans;
cin >> ans;
if(line < ans){
return false;
}
cpi:
string pin;
cout << "Enter your transaction PIN: ";
cin >> pin;
bool cpin = check_check_input(pin);
if(cpin == false){
clearUserInputOnly();
return false;
}
cpin = check_pin(usern, pin);
if(cpin == false){
clearUserInputOnly();
return false;
}
line = line-ans;
myfile.open(ext, ios::out);
myfile << line << endl;
myfile.close();
withdrawal_update_history(temp, ans, line);
return true;
}
void withdrawal_update_history(string a_number, int amount, int bal){
fstream myfile;
string ext = "_history.txt";
string bankname = "Dbank_";
string dt = date_time();
bankname.append(a_number);
bankname.append(ext);
myfile.open(bankname, ios::app);
myfile << "History: Account was Debited." << endl;
myfile << "Amount = " << amount << endl;
myfile << "Date & Time: " << dt << endl;
myfile << "Balance: " << bal << endl;
myfile << ".........." << endl;
myfile.close();
}
int transfer(string s_num, string usern){
fstream myfile;
string ext = "_balance.txt";
string bankname = "Dbank_";
string ext2 = "_balance.txt";
string bankname2 = "Dbank_";
cout << "How much do you want to transfer?\n";
int amount;
cin >> amount;
cout << "Enter the reciever's account number;\n";
string r_a_n;
cin >> r_a_n;
//string send = to_string(r_a_n);
bool check_a_n = acc_num_check(r_a_n);
if(check_a_n == false){
return 2;
}
cout << "Enter your transaction PIN: ";
cin >> pin;
bool cpin = check_check_input(pin);
if(cpin == false){
clearUserInputOnly();
return 5;
}
cpin = check_pin(usern, pin);
if(cpin == false){
clearUserInputOnly();
return 5;
}
bankname.append(s_num);
bankname.append(ext);
string line;
int temp_balance;
int balance;
myfile.open(bankname, ios::in);
getline(myfile, line);
myfile.close();
try{
temp_balance = stoi(line);
}catch(...){
temp_balance = 0;
}
if(temp_balance < amount){
return 3;
}
bankname2.append(r_a_n);
bankname2.append(ext2);
myfile.open(bankname2, ios::in);
getline(myfile, line);
myfile.close();
try{
balance = stoi(line);
}catch(...){
balance = 0;
}
balance = balance+amount;
myfile.open(bankname2, ios::out);
myfile << balance << endl;
myfile.close();
temp_balance = temp_balance-amount;
myfile.open(bankname, ios::out);
myfile << temp_balance << endl;
myfile.close();
transfer_update_history(s_num, r_a_n, amount, temp_balance, balance);
return 1;
}
void transfer_update_history(string s_a_n, string r_a_n, int amount, int s_balance, int r_balance){
fstream myfile;
string ext = "_history.txt";
string bankname = "Dbank_";
string temp = bankname;
string dt = date_time();
bankname.append(s_a_n);
bankname.append(ext);
myfile.open(bankname, ios::app);
myfile << "History: Account was Debited." << endl;
myfile << "Amount = " << amount << endl;
myfile << "Date & Time: " << dt << endl;
myfile << "Balance: " << s_balance << endl;
myfile << ".........." << endl;
myfile.close();
temp.append(r_a_n);
temp.append(ext);
myfile.open(temp, ios::app);
myfile << "History: Account was Credited." << endl;
myfile << "Amount = " << amount << endl;
myfile << "Date & Time: " << dt << endl;
myfile << "Balance: " << r_balance << endl;
myfile << ".........." << endl;
myfile.close();
}
void deposit(string accountnum){
fstream myfile;
string ext1 = "Dbank_";
string ext2 = "_balance.txt";
string line;
int balance;
ext1.append(accountnum);
ext1.append(ext2);
myfile.open(ext1, ios::in);
getline(myfile, line);
myfile.close();
cout << line;
try{
balance = stoi(line);
}catch(...){
balance = 0;
}
cout << "How much do you want to deposit?\n";
int ans;
cin >> ans;
balance = balance+ans;
myfile.open(ext1, ios::out);
myfile << balance << endl;
myfile.close();
deposit_update_history(accountnum, ans, balance);
}
void deposit_update_history(string a_number, int amount, int bal){
fstream myfile;
string ext = "_history.txt";
string bankname = "Dbank_";
string dt = date_time();
bankname.append(a_number);
bankname.append(ext);
myfile.open(bankname, ios::app);
myfile << "History: Account was Credited." << endl;
myfile << "Amount = " << amount << endl;
myfile << "Date & Time: " << dt << endl;
myfile << "Balance: " << bal << endl;
myfile << ".........." << endl;
myfile.close();
}
string check_balance(string acc_num, string usern){
fstream myfile;
string ext = "_balance.txt";
string bankname = "Dbank_";
bankname.append(acc_num);
bankname.append(ext);
cout << "Enter your transaction PIN: ";
cin >> pin;
bool cpin = check_check_input(pin);
if(cpin == false){
clearUserInputOnly();
cout << "Invalid PIN!\n";
return 0;
}
cpin = check_pin(usern, pin);
if(cpin == false){
clearUserInputOnly();
cout << "Invalid PIN!\n";
return 0;
}
myfile.open(bankname, ios::in);
string line;
getline(myfile, line);
myfile.close();
balance_update_history(acc_num, line);
return line;
}
void balance_update_history(string acc_num, string balance){
fstream myfile;
string bankname = "Dbank_";
string ext = "_history.txt";
string dt = date_time();
bankname.append(acc_num);
bankname.append(ext);
myfile.open(bankname, ios::app);
myfile << "History: Balance Checked." << endl;
myfile << "Amount = " << endl;
myfile << "Date & Time: " << dt << endl;
myfile << "Balance: " << balance << endl;
myfile << ".........." << endl;
}
bool check_check_input(string pin){
check_input cp;
cp.assign(pin);
bool cci = cp.check();
return cci;
}
string registration(){
fstream myfile;
ac:
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> distrib_int(1, 100000000);
long long acc_num = distrib_int(gen);
string account_n = acc_correction(acc_num);
bool check = acc_num_check(account_n);
if(check == true){
goto ac;
}
string ext = ".txt";
string ext2 = "_balance.txt";
string ext3 = "_history.txt";
string ext4 = "_info.txt";
string bank_name = "Dbank_";
string bankname = "Dbank_";
string temp = account_n;
string name;
int dob_day;
string dob_month;
int dob_year;
string address;
long long phone_num;
string email;
string username;
string password;
string cp;
string pin;
cout << "Enter your name: ";
cin.ignore();
getline(cin, name);
dc:
cout << "Enter the day you were born: ";
cin >> dob_day;
if(dob_day > 31 || dob_day < 1){
cout << "Invalid day!\n";
goto dc;
}
mc:
cout << "Enter the month you were born: ";
cin >> dob_month;
if(months.count(dob_month) != 1){
cout << "Invalid month. First letter is always capital.\n";
goto mc;
}
yc:
cout << "Enter the year you were born: ";
cin >> dob_year;
if(dob_year > 2025 || dob_year < 1900){
cout << "Invalid year.\n";
goto yc;
}
cout << "Enter your Home Address: ";
cin.ignore();
getline(cin, address);
cout << "Enter your Phone Number: ";
cin >> phone_num;
cout << "Enter your Email: ";
cin >> email;
cout << "Enter your username: ";
cin >> username;
while(username.size() <= 5){
cout << "Username can't be " << username.size() << " letters.\n";
//goto uc;
cout << "Enter your username: ";
cin >> username;
}
bool check_file = u_check(username);
while(check_file == true){
cout << "It has been taken.\n";
//goto uc;
cout << "Enter your username: ";
cin >> username;
check_file = u_check(username);
}
pt:
cout << "Enter your password: ";
cin >> password;
cout << "Confirm your Password: ";
cin >> cp;
if(cp != password){
cout << "Password does'nt match\n";
goto pt;
}
pc:
cout << "Enter a 4 digit Transaction Pin: ";
cin >> pin;
bool ccii = check_check_input();
if(ccii == false){
cout << "This is a digit only PIN!\n";
goto pc;
}
add_universal_file(username, account_n, password, pin);
bank_name.append(temp);
bank_name.append(ext4);
myfile.open(bank_name, ios::out);
myfile << account_n << endl;
myfile << "Name: " << name << endl;
myfile << "DOB: " << dob_day << ", " << dob_month << ", " << dob_year << endl;
myfile << "Address: " << address << endl;
myfile << "Phone Number: " << phone_num << endl;
myfile << "Email: " << email << endl;
myfile << "Username: " << username << endl;
myfile << "Password: " << password << endl;
myfile << "Transaction PIN: " << pin << endl;
myfile.close();
return account_n;
}