Advertisement
Dotunoyesanmi

Untitled

Feb 23rd, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.92 KB | None | 0 0
  1. /*
  2.       Assumptions
  3.       ===========
  4.       This programs assumes te following:
  5.  
  6.       a. Initial balance alloted during account creation is a non-negative number(integer/double0
  7.       b. Value of deposited amount is non negative.
  8.       c. D
  9.       d. All primitive data types are valid wi
  10.  
  11. */
  12.  
  13. #include <iostream>
  14. #include <fstream>
  15. #include <string>
  16. using namespace std;
  17.  
  18. void introText();
  19. void addAcc( int acc , string first_name , string last_name , double bal );
  20. void delAcc( int acc , string first_name , string last_name , double bal );
  21. void searchAcc( int acc , string first_name , string last_name , double bal );
  22. void withdraw( int acc , string first_name , string last_name , double bal );
  23. void deposit( int acc , string first_name , string last_name , double bal );
  24. void transfer( int acc , string first_name , string last_name , double bal );
  25. void listAcc();
  26. void exit();
  27.  
  28. int main()
  29. {
  30.     bool exit = false;
  31.  
  32.     while ( exit == false )
  33.     {
  34.         introText();
  35.  
  36.         cout.setf( ios::fixed );
  37.         cout.setf( ios::showpoint );
  38.         cout.precision( 2 );
  39.  
  40.         int response;
  41.         int acc = 0;
  42.         double bal = 0.0;
  43.         string first_name;
  44.         string last_name;
  45.  
  46.         cout << "Your selection" << "  ";
  47.         cin >> response;
  48.  
  49.         switch ( response )
  50.         {
  51.  
  52.             case 1:
  53.                 addAcc( acc , first_name , last_name , bal );
  54.                 break;
  55.  
  56.             case 2:
  57.                 delAcc( acc , first_name , last_name , bal );
  58.                 break;
  59.  
  60.             case 3:
  61.                 searchAcc( acc , first_name , last_name , bal );
  62.                 break;
  63.  
  64.             case 4:
  65.                 withdraw( acc , first_name , last_name , bal );
  66.                 break;
  67.  
  68.             case 5:
  69.                 deposit( acc , first_name , last_name , bal );
  70.                 break;
  71.  
  72.             case 6:
  73.                 transfer( acc , first_name , last_name , bal );
  74.                 break;
  75.  
  76.             case 7:
  77.                 listAcc();
  78.                 break;
  79.  
  80.             case 8:
  81.                 exit = true;
  82.                 break;
  83.  
  84.             default:
  85.             cout << "Wrong selction. Response must be between 1 and 8\n\n";
  86.         }
  87.     }
  88.     return 0;
  89. }
  90.  
  91. //A function to cout text selection.
  92. void introText()
  93. {
  94.     cout << "\n\n************************************\n";
  95.     cout << "Please make your selections:\n";
  96.     cout << "1. Add an account\n";
  97.     cout << "2. Delete an account\n";
  98.     cout << "3. Search an account\n";
  99.     cout << "4. Make a withdraw\n";
  100.     cout << "5. Make a deposit\n";
  101.     cout << "6. Transfer money\n";
  102.     cout << "7. List all records\n";
  103.     cout << "8. Exit the programn\n";
  104.     cout << "************************************\n\n\n";
  105. }
  106.  
  107. //A function to create a new account.
  108. void addAcc( int acc , string first_name , string last_name , double bal )
  109. {
  110.     ifstream fin( "bank.txt" );
  111.  
  112.     int accNum = 0;
  113.     double accBal = 0.0;
  114.     int count = 0;
  115.     string fname = " ";
  116.     string lname = " ";
  117.  
  118.     cout << "Enter account number : ";
  119.     cin  >> accNum;
  120.  
  121.     cout << "Enter first name : ";
  122.     cin  >> fname;
  123.  
  124.     cout << "Enter last name : ";
  125.     cin  >> lname;
  126.  
  127.     cout << "Enter account balance : ";
  128.     cin  >> accBal;
  129.  
  130.     //file loop to check if account number already exist.
  131.     while ( fin >> acc >> first_name >> last_name >> bal )
  132.     {
  133.         if ( acc == accNum )
  134.         {
  135.             count++;
  136.             ofstream temp( "temp.txt" );
  137.             temp << acc << " " << first_name << " " << last_name << " " << bal;
  138.             temp.close();
  139.         }
  140.     }
  141.     fin.close();
  142.  
  143.     //Insert new account if user supplied account number doesnt exist already.
  144.     if ( count == 0 )
  145.     {
  146.         ofstream fout( "bank.txt" , ios::app );
  147.         fout << accNum << " " << fname << " " << lname << " " << accBal << "\n";
  148.         cout << "\nNew Account Inserted.\n" << endl;
  149.         fout.close();
  150.         remove( "temp.txt" );
  151.     }
  152.  
  153.     //This should print the account profile that already exist on file, that the user wanted to reinsert.
  154.     if ( count > 0 )
  155.     {
  156.         ifstream output( "temp.txt" );
  157.         string custInfo;
  158.  
  159.         while ( getline( output , custInfo ) )
  160.         {
  161.             cout << "\n" << custInfo << "\n";
  162.         }
  163.         cout << "Same account is found. The account cannt be added.\n" << endl;
  164.         output.close();
  165.         remove( "temp.txt" );
  166.     }
  167. }
  168.  
  169. //Function to print out all the user account/information on file.
  170. void listAcc()
  171. {
  172.     ifstream fin( "bank.txt" );
  173.  
  174.     string custInfo;
  175.     int i = 1;
  176.  
  177.     while ( getline( fin , custInfo ) )
  178.     {
  179.         cout << i << ".  " << custInfo << "\n";
  180.         i++;
  181.     }
  182.     fin.close();
  183. }
  184.  
  185. //Function to search for a specific account profile, if it exists.
  186. void searchAcc( int acc , string first_name , string last_name , double bal )
  187. {
  188.     ifstream fin( "bank.txt" );
  189.  
  190.     int accNum = 0;
  191.     int count = 0;
  192.  
  193.     cout << "Enter account number : ";
  194.     cin >> accNum;
  195.  
  196.     //Loop to check if account number exists on file, if so, save the profile into anothe file.
  197.     while ( fin >> acc >> first_name >> last_name >> bal )
  198.     {
  199.         if ( acc == accNum )
  200.         {
  201.             ofstream fout( "temp.txt" );
  202.             fout << acc << " " << first_name << " " << last_name << " " << bal;
  203.             count++;
  204.             fout.close();
  205.         }
  206.     }
  207.  
  208.     //Account not found
  209.     if ( count == 0 )
  210.     {
  211.         cout << "\nCould not find this account.\n" << endl;
  212.     }
  213.  
  214.     fin.close();
  215.  
  216.     ifstream temp( "temp.txt" );
  217.     string custInfo;
  218.  
  219.     //Print account profile for the account the user searched for.
  220.     while ( getline( temp , custInfo ) )
  221.     {
  222.         cout << "\n" << custInfo << "\n\n";
  223.     }
  224.     temp.close();
  225.     remove( "temp.txt" );
  226. }
  227.  
  228. //Function to delete an account.
  229. void delAcc( int acc , string first_name , string last_name , double bal )
  230. {
  231.     ifstream fin( "bank.txt" );
  232.     ofstream fout( "temp.txt" );
  233.  
  234.     int accNum = 0;
  235.     int count = 0;
  236.  
  237.     cout << "Enter account number you wish to delete : ";
  238.     cin >> accNum;
  239.  
  240.     //All accounts that we do not want to delete should be copied into anothe file.
  241.     while ( fin >> acc >> first_name >> last_name >> bal )
  242.     {
  243.         if ( acc != accNum )
  244.         {
  245.             fout << acc << " " << first_name << " " << last_name << " " << bal << "\n";
  246.         }
  247.  
  248.         if ( acc == accNum )
  249.         {
  250.             count++;
  251.         }
  252.  
  253.     }
  254.  
  255.     //Account could not be found.
  256.     if ( count == 0 )
  257.     {
  258.         cout << "\nCould not find this account.\n" << endl;
  259.     }
  260.  
  261.     fin.close();
  262.     fout.close();
  263.  
  264.     //Rename temp file back to bank.txt and delete temp file. Account deleted.
  265.     if ( count > 0 )
  266.     {
  267.         ifstream temp( "temp.txt" );
  268.         string custInfo;
  269.  
  270.         temp.close();
  271.         remove( "bank.txt" );
  272.         rename( "temp.txt" , "bank.txt" );
  273.  
  274.         cout << "\nAccount deleted.";
  275.     }
  276. }
  277.  
  278. //Function to withdraw money from an account
  279. void withdraw( int acc , string first_name , string last_name , double bal )
  280. {
  281.     ifstream fin( "bank.txt" );
  282.     ofstream fout( "temp.txt" );
  283.  
  284.     int accNum = 0;
  285.     double withdrawAmount = 0.0;
  286.     int custNum = 0;
  287.     string custFirstName = " ";
  288.     string custLastname = " ";
  289.     double custAccBal = 0.0;
  290.  
  291.     cout << "Enter account number you want to withdraw from ";
  292.     cin >> accNum;
  293.  
  294.     cout << "Enter amount you want to withdraw ";
  295.     cin >> withdrawAmount;
  296.  
  297.     int count = 0;
  298.  
  299.     //If account number is found, save the each word/number from that account to a variable.
  300.     while ( fin >> acc >> first_name >> last_name >> bal )
  301.     {
  302.         if ( acc == accNum )
  303.         {
  304.             count++;
  305.             custNum = acc;
  306.             custFirstName = first_name;
  307.             custLastname = last_name;
  308.             custAccBal = bal;
  309.         }
  310.  
  311.         //This will copy all copy all account that does not match account we want to withdraw from into temp file
  312.         if ( acc != accNum )
  313.         {
  314.             fout << acc << " " << first_name << " " << last_name << " " << bal << "\n";
  315.         }
  316.     }
  317.  
  318.     fin.close();
  319.     fout.close();
  320.  
  321.     if ( count > 0 )
  322.     {
  323.         //check if account balance is greater than amount to be withdrawn
  324.         if ( withdrawAmount > custAccBal )
  325.         {
  326.             cout << "\nNot enough money in the account.\n" << endl;
  327.         }
  328.        
  329.         //If amount to be withdrawn is equal to or less than account balance, then withdraw.
  330.         if ( withdrawAmount <= custAccBal )
  331.         {
  332.             custAccBal = custAccBal - withdrawAmount;
  333.             ofstream fout( "temp.txt" , ios::app );
  334.             fout << custNum << " " << custFirstName << " " << custLastname << " " << custAccBal << "\n";
  335.             remove( "bank.txt" );
  336.             fout.close();
  337.             rename( "temp.txt" , "bank.txt" );
  338.             cout << "\nMoney was successfully withdrawn.";
  339.         }
  340.     }
  341.  
  342.     //Account not found.
  343.     if ( count == 0 )
  344.     {
  345.         cout << "\nThis account does not exist.\n" << endl;
  346.     }
  347. }
  348.  
  349. //Function to deposit into an existing account.
  350. void deposit( int acc , string first_name , string last_name , double bal )
  351. {
  352.     ifstream fin( "bank.txt" );
  353.     ofstream fout( "temp.txt" );
  354.  
  355.     int accNum = 0;
  356.     double withdrawAmount = 0.0;
  357.     double custAccBal = 0.0;
  358.     int custNum = 0;
  359.     string custFirstName = " ";
  360.     string custLastname = " ";
  361.  
  362.     cout << "Enter account number you want to deposit into ";
  363.     cin >> accNum;
  364.  
  365.     cout << "Enter amount you want to deposit ";
  366.     cin >> withdrawAmount;
  367.  
  368.     int count = 0;
  369.  
  370.     //If account number is found, save the each word/number from that account to a variable.
  371.     while ( fin >> acc >> first_name >> last_name >> bal )
  372.     {
  373.         if ( acc == accNum )
  374.         {
  375.             count++;
  376.             custNum = acc;
  377.             custFirstName = first_name;
  378.             custLastname = last_name;
  379.             custAccBal = bal;
  380.         }
  381.  
  382.         //This will copy all account that does not match account we want to deposit into to temp file
  383.         if ( acc != accNum )
  384.         {
  385.             fout << acc << " " << first_name << " " << last_name << " " << bal << "\n";
  386.         }
  387.     }
  388.  
  389.     fin.close();
  390.     fout.close();
  391.  
  392.     /*
  393.     If account found, deposite should be made. This function does not test for cases where the user enters a negative number, which will
  394.     actually deduct from the account, rather than deposit into it.
  395.     */
  396.     if ( count > 0 )
  397.     {
  398.         custAccBal = custAccBal + withdrawAmount;
  399.         ofstream fout( "temp.txt" , ios::app );
  400.         fout << custNum << " " << custFirstName << " " << custLastname << " " << custAccBal << "\n";
  401.         remove( "bank.txt" );
  402.         fout.close();
  403.         rename( "temp.txt" , "bank.txt" );
  404.         cout << "\nMoney was successfully deposited.";
  405.     }
  406.  
  407.     //Account not found.
  408.     if ( count == 0 )
  409.     {
  410.         cout << "\nThis account does not exist.\n" << endl;
  411.     }
  412. }
  413.  
  414. //Function to transfer from one account to the other.
  415. void transfer( int acc , string first_name , string last_name , double bal )
  416. {
  417.     ifstream fin( "bank.txt" );
  418.  
  419.     int transFromAcc = 0;
  420.     int transToAcc = 0;
  421.     int fromAcc = 0;
  422.     int toAcc = 0;
  423.  
  424.     double transferAmount = 0.0;
  425.     double fromBal = 0.0;
  426.     double toBal = 0.0;
  427.  
  428.     bool boolfromAcc = false;
  429.     bool booltoAcc = false;
  430.     bool boolTransBal = false;
  431.  
  432.     string fromFirstName = " ";
  433.     string fromLastName = " ";
  434.     string toFirstName = " ";
  435.     string toLastName = " ";
  436.  
  437.     cout << "Enter account you want to transfer from ";
  438.     cin >> transFromAcc;
  439.  
  440.     cout << "Enter account you want to transfer in ";
  441.     cin >> transToAcc;
  442.  
  443.     cout << "Enter amount you want to transfer ";
  444.     cin >> transferAmount;
  445.  
  446.     int count = 1;
  447.  
  448.     //By default, this loop will save all accounts that are not part of the transfer process (withdraw and deposit.)
  449.     while ( fin >> acc >> first_name >> last_name >> bal )
  450.     {
  451.  
  452.         if ( ( acc != transFromAcc ) && ( acc != transToAcc ) )
  453.         {
  454.             ofstream fout( "temp.txt" , ios::app );
  455.             fout << acc << " " << first_name << " " << last_name << " " << bal << "\n";
  456.             fout.close();
  457.         }
  458.  
  459.         //If transferring account is found on file, it's data should be stored in variables. And then return true.
  460.         if ( acc == transFromAcc )
  461.         {
  462.             fromAcc = acc;
  463.             fromFirstName = first_name;
  464.             fromLastName = last_name;
  465.             fromBal = bal;
  466.  
  467.             boolfromAcc = true;
  468.  
  469.             //If Amount to be transferred is greater than the available balance, then retrun true.
  470.             if ( transferAmount > fromBal )
  471.             {
  472.                 boolTransBal = true;
  473.             }
  474.         }
  475.  
  476.         //If depositing account is found on file, it's data should be stored in variables. Then return true.
  477.         if ( acc == transToAcc )
  478.         {
  479.             toAcc = acc;
  480.             toFirstName = first_name;
  481.             toLastName = last_name;
  482.             toBal = bal;
  483.  
  484.             booltoAcc = true;
  485.         }
  486.     }
  487.     fin.close();
  488.  
  489.     /*This will check if both withdrawing and depositing accounts both exists, and available balance in withdrawing account is greater than
  490.     amount to be transferred. If TRUE, money is withdrawn and deposited respectively. Updated accounts are appended ot temp.txt, bank.txt
  491.     is deleted, then temp.txt is renamed to bank.txt.
  492.     */
  493.     if ( ( boolfromAcc ) && ( booltoAcc ) && ( !boolTransBal ) )
  494.     {
  495.         fromBal = fromBal - transferAmount;
  496.         toBal = toBal + transferAmount;
  497.  
  498.         ofstream fout( "temp.txt" , ios::app );
  499.         fout << fromAcc << " " << fromFirstName << " " << fromLastName << " " << fromBal << "\n";
  500.         fout << toAcc << " " << toFirstName << " " << toLastName << " " << toBal << "\n";
  501.         remove( "bank.txt" );
  502.         fout.close();
  503.         rename( "temp.txt" , "bank.txt" );
  504.         cout << "\nTransfer successful. \n";
  505.     }
  506.  
  507.     /*
  508.     If both accounts exist, but there is insufficent account balance to withdraw from, then do not carry out transfer operation, then delete
  509.     temp.txt. This also does not affect the bank.txt.
  510.     */
  511.     if ( ( boolfromAcc ) && ( booltoAcc ) && ( boolTransBal ) )
  512.     {
  513.         cout << "\nBalance in first account is less than amount you want to transfer\n";
  514.         remove( "temp.txt" );
  515.     }
  516.  
  517.     /*
  518.     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.
  519.     */
  520.     if ( ( !boolfromAcc ) || ( !booltoAcc ) )
  521.     {
  522.         cout << "\nEither one of the accounts does not exist";
  523.         remove( "temp.txt" );
  524.     }
  525. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement