Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Assumptions
- ===========
- This programs assumes te following:
- a. Initial balance alloted during account creation is a non-negative number(integer/double0
- b. Value of deposited amount is non negative.
- c. D
- d. All primitive data types are valid wi
- */
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- void introText();
- void addAcc( int acc , string first_name , string last_name , double bal );
- void delAcc( int acc , string first_name , string last_name , double bal );
- void searchAcc( int acc , string first_name , string last_name , double bal );
- void withdraw( int acc , string first_name , string last_name , double bal );
- void deposit( int acc , string first_name , string last_name , double bal );
- void transfer( int acc , string first_name , string last_name , double bal );
- void listAcc();
- void exit();
- int main()
- {
- bool exit = false;
- while ( exit == false )
- {
- introText();
- cout.setf( ios::fixed );
- cout.setf( ios::showpoint );
- cout.precision( 2 );
- int response;
- int acc = 0;
- double bal = 0.0;
- string first_name;
- string last_name;
- cout << "Your selection" << " ";
- cin >> response;
- switch ( response )
- {
- case 1:
- addAcc( acc , first_name , last_name , bal );
- break;
- case 2:
- delAcc( acc , first_name , last_name , bal );
- break;
- case 3:
- searchAcc( acc , first_name , last_name , bal );
- break;
- case 4:
- withdraw( acc , first_name , last_name , bal );
- break;
- case 5:
- deposit( acc , first_name , last_name , bal );
- break;
- case 6:
- transfer( acc , first_name , last_name , bal );
- break;
- case 7:
- listAcc();
- break;
- case 8:
- exit = true;
- break;
- default:
- cout << "Wrong selction. Response must be between 1 and 8\n\n";
- }
- }
- return 0;
- }
- //A function to cout text selection.
- void introText()
- {
- cout << "\n\n************************************\n";
- cout << "Please make your selections:\n";
- cout << "1. Add an account\n";
- cout << "2. Delete an account\n";
- cout << "3. Search an account\n";
- cout << "4. Make a withdraw\n";
- cout << "5. Make a deposit\n";
- cout << "6. Transfer money\n";
- cout << "7. List all records\n";
- cout << "8. Exit the programn\n";
- cout << "************************************\n\n\n";
- }
- //A function to create a new account.
- void addAcc( int acc , string first_name , string last_name , double bal )
- {
- ifstream fin( "bank.txt" );
- int accNum = 0;
- double accBal = 0.0;
- int count = 0;
- string fname = " ";
- string lname = " ";
- cout << "Enter account number : ";
- cin >> accNum;
- cout << "Enter first name : ";
- cin >> fname;
- cout << "Enter last name : ";
- cin >> lname;
- cout << "Enter account balance : ";
- cin >> accBal;
- //file loop to check if account number already exist.
- while ( fin >> acc >> first_name >> last_name >> bal )
- {
- if ( acc == accNum )
- {
- count++;
- ofstream temp( "temp.txt" );
- temp << acc << " " << first_name << " " << last_name << " " << bal;
- temp.close();
- }
- }
- fin.close();
- //Insert new account if user supplied account number doesnt exist already.
- if ( count == 0 )
- {
- ofstream fout( "bank.txt" , ios::app );
- fout << accNum << " " << fname << " " << lname << " " << accBal << "\n";
- cout << "\nNew Account Inserted.\n" << endl;
- fout.close();
- remove( "temp.txt" );
- }
- //This should print the account profile that already exist on file, that the user wanted to reinsert.
- if ( count > 0 )
- {
- ifstream output( "temp.txt" );
- string custInfo;
- while ( getline( output , custInfo ) )
- {
- cout << "\n" << custInfo << "\n";
- }
- cout << "Same account is found. The account cannt be added.\n" << endl;
- output.close();
- remove( "temp.txt" );
- }
- }
- //Function to print out all the user account/information on file.
- void listAcc()
- {
- ifstream fin( "bank.txt" );
- string custInfo;
- int i = 1;
- while ( getline( fin , custInfo ) )
- {
- cout << i << ". " << custInfo << "\n";
- i++;
- }
- fin.close();
- }
- //Function to search for a specific account profile, if it exists.
- void searchAcc( int acc , string first_name , string last_name , double bal )
- {
- ifstream fin( "bank.txt" );
- int accNum = 0;
- int count = 0;
- cout << "Enter account number : ";
- cin >> accNum;
- //Loop to check if account number exists on file, if so, save the profile into anothe file.
- while ( fin >> acc >> first_name >> last_name >> bal )
- {
- if ( acc == accNum )
- {
- ofstream fout( "temp.txt" );
- fout << acc << " " << first_name << " " << last_name << " " << bal;
- count++;
- fout.close();
- }
- }
- //Account not found
- if ( count == 0 )
- {
- cout << "\nCould not find this account.\n" << endl;
- }
- fin.close();
- ifstream temp( "temp.txt" );
- string custInfo;
- //Print account profile for the account the user searched for.
- while ( getline( temp , custInfo ) )
- {
- cout << "\n" << custInfo << "\n\n";
- }
- temp.close();
- remove( "temp.txt" );
- }
- //Function to delete an account.
- void delAcc( int acc , string first_name , string last_name , double bal )
- {
- ifstream fin( "bank.txt" );
- ofstream fout( "temp.txt" );
- int accNum = 0;
- int count = 0;
- cout << "Enter account number you wish to delete : ";
- cin >> accNum;
- //All accounts that we do not want to delete should be copied into anothe file.
- while ( fin >> acc >> first_name >> last_name >> bal )
- {
- if ( acc != accNum )
- {
- fout << acc << " " << first_name << " " << last_name << " " << bal << "\n";
- }
- if ( acc == accNum )
- {
- count++;
- }
- }
- //Account could not be found.
- if ( count == 0 )
- {
- cout << "\nCould not find this account.\n" << endl;
- }
- fin.close();
- fout.close();
- //Rename temp file back to bank.txt and delete temp file. Account deleted.
- if ( count > 0 )
- {
- ifstream temp( "temp.txt" );
- string custInfo;
- temp.close();
- remove( "bank.txt" );
- rename( "temp.txt" , "bank.txt" );
- cout << "\nAccount deleted.";
- }
- }
- //Function to withdraw money from an account
- void withdraw( int acc , string first_name , string last_name , double bal )
- {
- ifstream fin( "bank.txt" );
- ofstream fout( "temp.txt" );
- int accNum = 0;
- double withdrawAmount = 0.0;
- int custNum = 0;
- string custFirstName = " ";
- string custLastname = " ";
- double custAccBal = 0.0;
- cout << "Enter account number you want to withdraw from ";
- cin >> accNum;
- cout << "Enter amount you want to withdraw ";
- cin >> withdrawAmount;
- int count = 0;
- //If account number is found, save the each word/number from that account to a variable.
- while ( fin >> acc >> first_name >> last_name >> bal )
- {
- if ( acc == accNum )
- {
- count++;
- custNum = acc;
- custFirstName = first_name;
- custLastname = last_name;
- custAccBal = bal;
- }
- //This will copy all copy all account that does not match account we want to withdraw from into temp file
- if ( acc != accNum )
- {
- fout << acc << " " << first_name << " " << last_name << " " << bal << "\n";
- }
- }
- fin.close();
- fout.close();
- if ( count > 0 )
- {
- //check if account balance is greater than amount to be withdrawn
- if ( withdrawAmount > custAccBal )
- {
- cout << "\nNot enough money in the account.\n" << endl;
- }
- //If amount to be withdrawn is equal to or less than account balance, then withdraw.
- if ( withdrawAmount <= custAccBal )
- {
- custAccBal = custAccBal - withdrawAmount;
- ofstream fout( "temp.txt" , ios::app );
- fout << custNum << " " << custFirstName << " " << custLastname << " " << custAccBal << "\n";
- remove( "bank.txt" );
- fout.close();
- rename( "temp.txt" , "bank.txt" );
- cout << "\nMoney was successfully withdrawn.";
- }
- }
- //Account not found.
- if ( count == 0 )
- {
- cout << "\nThis account does not exist.\n" << endl;
- }
- }
- //Function to deposit into an existing account.
- void deposit( int acc , string first_name , string last_name , double bal )
- {
- ifstream fin( "bank.txt" );
- ofstream fout( "temp.txt" );
- int accNum = 0;
- double withdrawAmount = 0.0;
- double custAccBal = 0.0;
- int custNum = 0;
- string custFirstName = " ";
- string custLastname = " ";
- cout << "Enter account number you want to deposit into ";
- cin >> accNum;
- cout << "Enter amount you want to deposit ";
- cin >> withdrawAmount;
- int count = 0;
- //If account number is found, save the each word/number from that account to a variable.
- while ( fin >> acc >> first_name >> last_name >> bal )
- {
- if ( acc == accNum )
- {
- count++;
- custNum = acc;
- custFirstName = first_name;
- custLastname = last_name;
- custAccBal = bal;
- }
- //This will copy all account that does not match account we want to deposit into to temp file
- if ( acc != accNum )
- {
- fout << acc << " " << first_name << " " << last_name << " " << bal << "\n";
- }
- }
- fin.close();
- fout.close();
- /*
- If account found, deposite should be made. This function does not test for cases where the user enters a negative number, which will
- actually deduct from the account, rather than deposit into it.
- */
- if ( count > 0 )
- {
- custAccBal = custAccBal + withdrawAmount;
- ofstream fout( "temp.txt" , ios::app );
- fout << custNum << " " << custFirstName << " " << custLastname << " " << custAccBal << "\n";
- remove( "bank.txt" );
- fout.close();
- rename( "temp.txt" , "bank.txt" );
- cout << "\nMoney was successfully deposited.";
- }
- //Account not found.
- if ( count == 0 )
- {
- cout << "\nThis account does not exist.\n" << endl;
- }
- }
- //Function to transfer from one account to the other.
- void transfer( int acc , string first_name , string last_name , double bal )
- {
- ifstream fin( "bank.txt" );
- int transFromAcc = 0;
- int transToAcc = 0;
- int fromAcc = 0;
- int toAcc = 0;
- double transferAmount = 0.0;
- double fromBal = 0.0;
- double toBal = 0.0;
- bool boolfromAcc = false;
- bool booltoAcc = false;
- bool boolTransBal = false;
- string fromFirstName = " ";
- string fromLastName = " ";
- string toFirstName = " ";
- string toLastName = " ";
- cout << "Enter account you want to transfer from ";
- cin >> transFromAcc;
- cout << "Enter account you want to transfer in ";
- cin >> transToAcc;
- cout << "Enter amount you want to transfer ";
- cin >> transferAmount;
- int count = 1;
- //By default, this loop will save all accounts that are not part of the transfer process (withdraw and deposit.)
- while ( fin >> acc >> first_name >> last_name >> bal )
- {
- if ( ( acc != transFromAcc ) && ( acc != transToAcc ) )
- {
- ofstream fout( "temp.txt" , ios::app );
- fout << acc << " " << first_name << " " << last_name << " " << bal << "\n";
- fout.close();
- }
- //If transferring account is found on file, it's data should be stored in variables. And then return true.
- if ( acc == transFromAcc )
- {
- fromAcc = acc;
- fromFirstName = first_name;
- fromLastName = last_name;
- fromBal = bal;
- boolfromAcc = true;
- //If Amount to be transferred is greater than the available balance, then retrun true.
- if ( transferAmount > fromBal )
- {
- boolTransBal = true;
- }
- }
- //If depositing account is found on file, it's data should be stored in variables. Then return true.
- if ( acc == transToAcc )
- {
- toAcc = acc;
- toFirstName = first_name;
- toLastName = last_name;
- toBal = bal;
- booltoAcc = true;
- }
- }
- fin.close();
- /*This will check if both withdrawing and depositing accounts both exists, and available balance in withdrawing account is greater than
- amount to be transferred. If TRUE, money is withdrawn and deposited respectively. Updated accounts are appended ot temp.txt, bank.txt
- is deleted, then temp.txt is renamed to bank.txt.
- */
- if ( ( boolfromAcc ) && ( booltoAcc ) && ( !boolTransBal ) )
- {
- fromBal = fromBal - transferAmount;
- toBal = toBal + transferAmount;
- ofstream fout( "temp.txt" , ios::app );
- fout << fromAcc << " " << fromFirstName << " " << fromLastName << " " << fromBal << "\n";
- fout << toAcc << " " << toFirstName << " " << toLastName << " " << toBal << "\n";
- remove( "bank.txt" );
- fout.close();
- rename( "temp.txt" , "bank.txt" );
- cout << "\nTransfer successful. \n";
- }
- /*
- If both accounts exist, but there is insufficent account balance to withdraw from, then do not carry out transfer operation, then delete
- temp.txt. This also does not affect the bank.txt.
- */
- if ( ( boolfromAcc ) && ( booltoAcc ) && ( boolTransBal ) )
- {
- cout << "\nBalance in first account is less than amount you want to transfer\n";
- remove( "temp.txt" );
- }
- /*
- If either accounts does not exist, then do not carry out transfer operation, then delete temp.txt. This also does not affect the bank.txt.
- */
- if ( ( !boolfromAcc ) || ( !booltoAcc ) )
- {
- cout << "\nEither one of the accounts does not exist";
- remove( "temp.txt" );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement