|
| 1 | +#include<bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | +//Bank class |
| 4 | +class Bank{ |
| 5 | + int accNo; |
| 6 | + char name[25]; |
| 7 | + char father[25]; |
| 8 | + char Aadhar[25]; |
| 9 | + char mobile[25]; |
| 10 | + char email[25]; |
| 11 | + float amount; |
| 12 | + int newAmount; |
| 13 | + fstream file, file1; |
| 14 | + |
| 15 | +public: |
| 16 | + int search; |
| 17 | + |
| 18 | + void createAccount(void); |
| 19 | + void depositAmount(void); |
| 20 | + void withdrawAmount(void); |
| 21 | + void checkInfo(void); |
| 22 | + bool isValidEmail(const string &email); |
| 23 | + bool isDuplicate(const string &Aadhar, const string &email, const string &phone); |
| 24 | +}; |
| 25 | +//check email valid ya invalid |
| 26 | +bool Bank::isValidEmail(const string &email) { |
| 27 | + return (email.find('@') != string::npos && email.find('.') != string::npos && email.find(' ') == string::npos); |
| 28 | +} |
| 29 | +//check duplicate user |
| 30 | +bool Bank::isDuplicate(const string &cnicVal, const string &emailVal, const string &phoneVal) { |
| 31 | + fstream f; |
| 32 | + f.open("bankdata.txt", ios::in); |
| 33 | + if (!f) |
| 34 | + return false; |
| 35 | + |
| 36 | + int acc; |
| 37 | + char n[25], fn[25], c[25], p[25], e[25]; |
| 38 | + float amt; |
| 39 | + while(f>>acc>>n>>fn>>c>>p>>e>>amt){ |
| 40 | + if(strcmp(c, cnicVal.c_str()) == 0 || strcmp(e, emailVal.c_str()) == 0 || strcmp(p, phoneVal.c_str()) == 0) { |
| 41 | + f.close(); |
| 42 | + return true; |
| 43 | + } |
| 44 | + } |
| 45 | + f.close(); |
| 46 | + return false; |
| 47 | +} |
| 48 | +//create Account |
| 49 | +void Bank::createAccount(){ |
| 50 | + srand(time(0)); |
| 51 | + accNo = rand()*rand() + rand()*rand(); |
| 52 | + |
| 53 | + cout << "Enter Your name -: "; |
| 54 | + cin >> name; |
| 55 | + cout << "Enter Your Father name -: "; |
| 56 | + cin >> father; |
| 57 | + cout << "Enter Your Aadhar no. -: "; |
| 58 | + cin >> Aadhar; |
| 59 | + cout << "Enter Your phone no. -: "; |
| 60 | + cin >> mobile; |
| 61 | + cout << "Enter Your email -: "; |
| 62 | + cin >> email; |
| 63 | + |
| 64 | + // Email Validation |
| 65 | + if(!isValidEmail(email)){ |
| 66 | + cout << "\n Invalid email format! Use a valid email (e.g., abc@gmail.com)\n"; |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // Duplicate Check |
| 71 | + if(isDuplicate(Aadhar, email, mobile)){ |
| 72 | + cout << "\n Account with same Aadhar, Email or Phone already exists!\n"; |
| 73 | + return; |
| 74 | + } |
| 75 | + cout << "Enter Your amount to deposit -: "; |
| 76 | + cin>>amount; |
| 77 | + cout << endl |
| 78 | + << accNo << " This is your account number...\n"; |
| 79 | + cout << "Please save it.\n\n"; |
| 80 | + |
| 81 | + file.open("bankdata.txt", ios::out | ios::app); |
| 82 | + file << accNo << "\t" << name << "\t" << father << "\t" << Aadhar << "\t" << mobile << "\t" << email << "\t" << amount << endl; |
| 83 | + file.close(); |
| 84 | + |
| 85 | + cout << "✅ Account created successfully!\n"; |
| 86 | +} |
| 87 | +//deposit |
| 88 | +void Bank::depositAmount() { |
| 89 | + cout << "Enter amount to deposit -: "; |
| 90 | + cin >> newAmount; |
| 91 | + |
| 92 | + file.open("bankdata.txt", ios::in); |
| 93 | + file1.open("bankdata1.txt", ios::out | ios::app); |
| 94 | + file >> accNo >> name >> father >> Aadhar >> mobile >> email >> amount; |
| 95 | + |
| 96 | + bool found = false; |
| 97 | + while(!file.eof()){ |
| 98 | + if(accNo == search){ |
| 99 | + found = true; |
| 100 | + cout << "\nCurrent amount -: " << amount; |
| 101 | + amount = amount + newAmount; |
| 102 | + cout << "\nUpdated amount -: " << amount << endl; |
| 103 | + } |
| 104 | + file1 << accNo << "\t" << name << "\t" << father << "\t" << Aadhar << "\t" << mobile << "\t" << email << "\t" << amount << endl; |
| 105 | + file >> accNo >> name >> father >> Aadhar >> mobile >> email >> amount; |
| 106 | + } |
| 107 | + |
| 108 | + if (!found) |
| 109 | + cout << "\n Account not found!\n"; |
| 110 | + |
| 111 | + file.close(); |
| 112 | + file1.close(); |
| 113 | + remove("bankdata.txt"); |
| 114 | + rename("bankdata1.txt", "bankdata.txt"); |
| 115 | +} |
| 116 | +//withdraw |
| 117 | +void Bank::withdrawAmount() { |
| 118 | + cout << "Enter amount to withdraw -: "; |
| 119 | + cin >> newAmount; |
| 120 | + |
| 121 | + file.open("bankdata.txt", ios::in); |
| 122 | + file1.open("bankdata1.txt", ios::out | ios::app); |
| 123 | + file >> accNo >> name >> father >> Aadhar >> mobile >> email >> amount; |
| 124 | + |
| 125 | + bool found = false; |
| 126 | + while(!file.eof()){ |
| 127 | + if(accNo == search){ |
| 128 | + found = true; |
| 129 | + cout << "\nCurrent amount -: " << amount; |
| 130 | + if(newAmount > amount) { |
| 131 | + cout << "\n Insufficient balance!\n"; |
| 132 | + }else { |
| 133 | + amount = amount - newAmount; |
| 134 | + cout << "\nUpdated amount -: " << amount << endl; |
| 135 | + } |
| 136 | + } |
| 137 | + file1 << accNo << "\t" << name << "\t" << father << "\t" << Aadhar << "\t" << mobile << "\t" << email << "\t" << amount << endl; |
| 138 | + file >> accNo >> name >> father >> Aadhar >> mobile >> email >> amount; |
| 139 | + } |
| 140 | + |
| 141 | + if (!found) |
| 142 | + cout << "\n Account not found!\n"; |
| 143 | + |
| 144 | + file.close(); |
| 145 | + file1.close(); |
| 146 | + remove("bankdata.txt"); |
| 147 | + rename("bankdata1.txt", "bankdata.txt"); |
| 148 | +} |
| 149 | +//check info |
| 150 | +void Bank::checkInfo() { |
| 151 | + fstream file; |
| 152 | + file.open("bankdata.txt", ios::in); |
| 153 | + if (!file) { |
| 154 | + cout << " File opening error!"; |
| 155 | + return; |
| 156 | + } |
| 157 | + file >> accNo >> name >> father >> Aadhar >> mobile >> email >> amount; |
| 158 | + bool found = false; |
| 159 | + while (!file.eof()) { |
| 160 | + if (accNo == search) { |
| 161 | + found = true; |
| 162 | + cout << "\n_________________________________________\n"; |
| 163 | + cout << "\t# Account Number - " << accNo << endl; |
| 164 | + cout << "\t# User Name - " << name << endl; |
| 165 | + cout << "\t# Father Name - " << father << endl; |
| 166 | + cout << "\t# Aadhar number - " << Aadhar << endl; |
| 167 | + cout << "\t# Phone Number - " << mobile << endl; |
| 168 | + cout << "\t# Email - " << email << endl; |
| 169 | + cout << "\t# Current amount - " << amount << endl; |
| 170 | + cout << "___________________________________________\n\n"; |
| 171 | + } |
| 172 | + file >> accNo >> name >> father >> Aadhar >> mobile >> email >> amount; |
| 173 | + } |
| 174 | + |
| 175 | + if (!found) |
| 176 | + cout << "\n Account not found!\n"; |
| 177 | + |
| 178 | + file.close(); |
| 179 | +} |
| 180 | +//main function |
| 181 | +int main() { |
| 182 | + Bank obj; |
| 183 | + char choice; |
| 184 | + |
| 185 | + cout << "\n\n\n\t\tWelcome! choose your Option :"; |
| 186 | + cout << "\n\t\t1. press 1 to Login Account "; |
| 187 | + cout << "\n\t\t2. press 2 to Create Account"; |
| 188 | + cout << "\n\t\t3. press 0 to Exit "; |
| 189 | + cout << "\n\t\t >Enter choice... :\t"; |
| 190 | + cin >> choice; |
| 191 | +//choice |
| 192 | + switch (choice) { |
| 193 | + case '1': |
| 194 | + cout << "Enter your account no -: "; |
| 195 | + cin >> obj.search; |
| 196 | + while (1) { |
| 197 | + cout << "\n\n\n\t\tWelcome! choose your Option :"; |
| 198 | + cout << "\n\t\t1. press 1 to Deposit Amount "; |
| 199 | + cout << "\n\t\t2. press 2 to Withdraw Amount"; |
| 200 | + cout << "\n\t\t3. press 3 to Check Info"; |
| 201 | + cout << "\n\t\t4. press 0 to Exit Menu"; |
| 202 | + cout << "\n\t\t>Enter Choice... :\t"; |
| 203 | + cin >> choice; |
| 204 | + |
| 205 | + switch (choice) { |
| 206 | + case '1': |
| 207 | + obj.depositAmount(); |
| 208 | + break; |
| 209 | + case '2': |
| 210 | + obj.withdrawAmount(); |
| 211 | + break; |
| 212 | + case '3': |
| 213 | + obj.checkInfo(); |
| 214 | + break; |
| 215 | + case '0': |
| 216 | + return 0; |
| 217 | + default: |
| 218 | + cout << "Invalid Choice...!"; |
| 219 | + break; |
| 220 | + } |
| 221 | + system("pause"); |
| 222 | + system("cls"); |
| 223 | + } |
| 224 | + break; |
| 225 | + case '2': |
| 226 | + obj.createAccount(); |
| 227 | + break; |
| 228 | + case '0': |
| 229 | + cout << "Exiting... Thank you!\n"; |
| 230 | + break; |
| 231 | + default: |
| 232 | + cout << "\n Invalid choice...! "; |
| 233 | + break; |
| 234 | + } |
| 235 | + return 0; |
| 236 | +} |
0 commit comments