0% found this document useful (0 votes)
19 views

Assignment

Uploaded by

Sharafat Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Assignment

Uploaded by

Sharafat Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <fstream>
#include <sstream>
using namespace std;

class Account {
private:
string AccountNo, Password;
int Balance;

public:
Account() {
AccountNo = "";
Password = "";
Balance = 0;
}

void setAccountNo(const string& id) {


AccountNo = id;
}

void setPassword(const string& pw) {


Password = pw;
}
void setBalance(int bal) {
Balance = bal;
}

string getAccountNo() const {


return AccountNo;
}

string getPassword() const {


return Password;
}

int getBalance() const {


return Balance;
}
};

void openAccount(Account& user) {


system("cls");
string ID, pw;
cout << "Enter Account Number: ";
cin >> ID;
cout << "Enter a Strong Password: ";
cin >> pw;

user.setAccountNo(ID);
user.setPassword(pw);
user.setBalance(0);

ofstream outfile("Account.txt", ios::app);


if (!outfile) { cout << "Error! File Can't Open.\n";
}
else {
outfile << user.getAccountNo() << " : " << user.getPassword() << " : " <<
user.getBalance() <<
endl;
cout << "Account Created Successfully!\n";
}
outfile.close();
system("pause");
}

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");
}

void withdraw(Account& user) {


system("cls");
string id, pw;
cout << "Enter Account No: ";
cin >> id;
cout << "Enter Password: ";
cin >> pw;

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 (id == userID && pw == userpw) {


found = true;
int cash;
cout << "Enter Cash: ";
cin >> cash;

if (cash <= userbalance) {


userbalance -= cash;
outfile << userID << " : " << userpw << " : " << userbalance << endl;
cout << "Transaction Successful. Remaining Balance: " << userbalance << endl;
}
else {
cout << "Low Balance!\n";
outfile << line << endl;
}
}
else {
outfile << line << endl;
}
}

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;
}

You might also like