Assignment
Assignment
#include <fstream>
#include <sstream>
using namespace std;
class Account {
private:
string AccountNo, Password;
int Balance;
public:
Account() {
AccountNo = "";
Password = "";
Balance = 0;
}
user.setAccountNo(ID);
user.setPassword(pw);
user.setBalance(0);
void addCash() {
system("cls");
string id;
cout << "Enter Account No: ";
cin >> id;
ifstream infile("Account.txt");
ofstream outfile("Account_temp.txt");
if (!infile || !outfile) {
cout << "Error! File Not Found.\n";
return;
}
string line;
bool found = false;
while (getline(infile, line)) {
stringstream ss(line);
string userID, userpw;
int userbal;
char delimiter;
ss >> userID >> delimiter >> userpw >> delimiter >> userbal;
if (id == userID) {
found = true;
int cash;
cout << "Enter Cash: ";
cin >> cash;
userbal += cash;
outfile << userID << " : " << userpw << " : " << userbal << endl;
cout << "New Balance Is: " << userbal << endl;
}
else {
outfile << line << endl;
}
}
if (!found) {
cout << "Enter Valid Account No!\n";
}
outfile.close();
infile.close();
remove("Account.txt");
rename("Account_temp.txt", "Account.txt");
system("pause");
}
ifstream infile("Account.txt");
ofstream outfile("Account_temp.txt");
if (!infile || !outfile) {
cout << "Error! File Not Found.\n";
return;
}
string line;
bool found = false;
while (getline(infile, line)) {
stringstream ss(line);
string userID, userpw;
int userbalance;
char delimiter;
ss >> userID >> delimiter >> userpw >> delimiter >> userbalance;
if (!found) {
cout << "Invalid Account No or Password!\n";
}
outfile.close();
infile.close();
remove("Account.txt");
rename("Account_temp.txt", "Account.txt");
system("pause");
}
int main() {
Account user;
bool exit = false;
while (!exit) {
system("cls");
int choice;
cout << "Welcome To Bank Account Management\n";
cout << "1. Open New Account\n";
cout << "2. Add Cash\n";
cout << "3. Withdraw Cash\n";
cout << "4. Exit\n";
cout << "Enter Your Choice: ";
cin >> choice;
switch (choice) {
case 1:
openAccount(user);
break;
case 2:
addCash();
break;
case 3:
withdraw(user);
break;
case 4:
exit = true;
cout << "Exiting the Program...\n";
system("pause");
break;
default:
cout << "Invalid Choice!\n";
system("pause");
break;
}
}
return 0;
}