Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.91 KB | None | 0 0
  1.    
  2. //using std with cout and cin instead of namespace
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include <ctime>
  8. #include <conio.h>
  9. #include <stdio.h>
  10. #include <sstream>
  11. using namespace std;
  12.  
  13. struct User {
  14.         int acctNumber1;                        //User account number 1
  15.         int acctNumber2;
  16.         double acctBalance1;            //user account balance 1
  17.         double acctBalance2;          
  18.         int     acctInterest1;                  //user account interest rate1
  19.         int acctInterest2;
  20.         char acctName[256];                     //User account Name
  21.         char acctPass[256];                     //user account password
  22.         int logInNumber;
  23.         bool isAdmin;
  24. } account[8999]; // 9999 - 1000 because we virtually start at record '1000'
  25.  
  26. void saveAccounts()
  27. {/*
  28.         std::stringstream stream;
  29.         char buffer[100];
  30.         FILE *accountsFile = fopen("C:\\Users\\James\\Desktop\\accounts.txt", "w+");
  31.         if (accountsFile!=NULL)
  32.         {
  33.             for(int i = 0; i < 8999; i++) {    
  34.                 if(account[i].acctNumber1!=NULL) {
  35.                     stream << account[i].acctNumber1;
  36.                     string s = stream.str();
  37.                     fputs(s.c_str(), accountsFile);                
  38.                     accountsFile << account[i].acctNumber1 << '\n';
  39.                 }
  40.             }
  41.         } else { cout << "Unable to open file!"; }
  42.         fclose(accountsFile);
  43.         */
  44.     ofstream accountsFile;
  45.     accountsFile.open ("accounts.dat");
  46.     for(int i = 0; i < 8999; i++) {    
  47.         if(account[i].acctNumber1 != NULL) {
  48.             accountsFile << account[i].acctNumber1 << "\n";
  49.         }
  50.     }
  51.     accountsFile.close();
  52.     _getch();
  53. }
  54.  
  55.  
  56. void printSummary(int user) {
  57.         printf("\n\n"
  58.                    "+-----------------+\n"
  59.                    "| Account Summary |\n"
  60.                    "+=================+\n\n"
  61.                    "+-----------------------------------------------------+\n"
  62.                    "| Account | Account number | Balance  | Interest Rate |\n"
  63.                    "+---------+----------------+--------------------------+\n"
  64.                    "| 1       | %d           | %.2lf                      \n"           //add interest rate 1 and 2
  65.                    "| 2       | %d           | %.2lf                      \n\n\n",
  66.                    account[user].acctNumber1, account[user].acctBalance1, account[user].acctNumber2, account[user].acctBalance2);
  67. }
  68.  
  69. void createAccount(int *n) {
  70.         *n = *n+1;
  71.         int *inputInt = new int[1];
  72.         std::cout << "Please Fill out the following information to create a new account" << std::endl;
  73.         std::cout << "Full name: ";
  74.         std::cin >> account[*n].acctName;
  75.         std::cout << "Password: ";
  76.         std::cin >> account[*n].acctPass;
  77.                                
  78.         // get first account number
  79.         std::cout << "First account number: ";
  80.         std::cin >> *inputInt;
  81.         while(*inputInt < 1000 || *inputInt > 9999) { // check none of the users already have this account number, if they do keep prompting
  82.                 std::cout       << "Account number invalid. Must be between 1000 and 9999.\n"
  83.                                         << "------------------------------------------------------\n"
  84.                                         << "First account number: ";                                  
  85.                 std::cin >> *inputInt;
  86.         }
  87.         account[*n].acctNumber1 = *inputInt; // valid (new) account number, add it to the user
  88.  
  89.         // get first account balance
  90.         std::cout << "First account Balance: ";
  91.         std::cin >> account[*n].acctBalance1;
  92.  
  93.         //check its between 0 and 100,000 else re ask
  94.  
  95.         //get first interest rate, check its between .01% and 15% else re ask
  96.  
  97.         // get second account number
  98.         std::cout << "Second account number: ";
  99.         std::cin >> *inputInt;
  100.         while(*inputInt < 1000 || *inputInt > 8999) { // check none of the users already have this account number, if they do keep prompting
  101.                 std::cout       << "Account number invalid. Must be between 1000 and 9999.\n"
  102.                                         << "------------------------------------------------------\n"
  103.                                         << "Second account number: ";
  104.                 std::cin >> *inputInt;
  105.         }
  106.         account[*n].acctNumber2 = *inputInt; // valid (new) account number, add it to the user
  107.  
  108.         //get second account balance
  109.         std::cout << "Second account balance: ";
  110.         std::cin >> account[*n].acctBalance2;
  111.  
  112.         //check its between 0 and 100,000 else re ask
  113.  
  114.         //get first interest rate, check its between .01% and 15% else re ask
  115.        
  116. }
  117.  
  118. void login(int *userId, int *loggedIn) {
  119.         char *inputText = new char[256];
  120.         std::cout << "Please enter username: ";
  121.         std::cin >> inputText;
  122.         for(int i = 0; i < 8999; i++) {
  123.                 if ( strcmp (account[i].acctName, inputText) == 0 ) {
  124.                         std::cout << "Welcome " << account[i].acctName << ", Please enter your password: " << std::endl; //move to logged in menu
  125.                         std::cin >> inputText;
  126.                         if ( strcmp (account[i].acctPass, inputText) == 0 ) {
  127.                                 printSummary(i);
  128.                                 std::cout << "Press [Enter] to continue" << std::endl;
  129.                                 _getch();
  130.                                 *loggedIn = 1;
  131.                                 account[i].logInNumber++;
  132.                                 *userId = i;
  133.                         }
  134.                         else {
  135.                                 std::cout << "Incorrect password." << std::endl;
  136.                                 break;
  137.                         }
  138.                 }
  139.         }
  140.         saveAccounts();
  141. }
  142.  
  143. void transferMoney(int *userId) {
  144.         double amount = 0.00;
  145.         bool enoughFunds = false;
  146.         std::cout       << "Please enter amount to transer: ";
  147.         std::cin        >> amount;
  148.         if(account[*userId].acctNumber1 == account[*userId].acctNumber2) {
  149.                 printf( "   ^       +-----------+\n"
  150.                                 " / ! \\     | Declined: |      Account numbers are the same.\n"
  151.                                 "/____ \\    +-----------+\n\n");
  152.         }
  153.         if(amount > account[*userId].acctBalance1) {
  154.                 printf( "   ^       +-----------+\n"
  155.                                 " / ! \\     | Declined: |      Not enough founds in account number %d\n"
  156.                                 "/____ \\    +-----------+\n\n",
  157.                                 account[*userId].acctNumber1);
  158.         } else { enoughFunds = true; }
  159.         if(enoughFunds && (account[*userId].acctBalance1 - amount) < 10.00) {
  160.                 printf( "+----------+\n"
  161.                                 "| Warning: |   Account number %d now has a balance less than $10.00.\n"
  162.                                 "+----------+\n\n",
  163.                                 account[*userId].acctNumber1);
  164.                 account[*userId].acctBalance2 += amount;
  165.                 account[*userId].acctBalance1 -= amount;
  166.                 std::cout << "Press [Enter] to continue" << std::endl;
  167.                 enoughFunds = false; //prevent a second transfer happening
  168.         }
  169.         if(enoughFunds) {
  170.                 if((amount + account[*userId].acctBalance2) > 100000.00) {
  171.                         printf( "+----------+\n"
  172.                                         "| Warning: |   Account number %d will have a balance greater\n"
  173.                                         "+----------+   than the federally insurable amount of\n"
  174.                                         "               $100,000.00 if you proceed.\n\n",
  175.                                         account[*userId].acctNumber2);
  176.                         char yn = 'x';        
  177.                         // loop until the input is a y or n
  178.                         while(yn != 'y' && yn != 'n') {
  179.                                 std::cout << "Continue with transfer? Select [Y]es or [N]o" << std::endl;
  180.                                 std::cin >> yn;
  181.                         }
  182.                         if(yn = 'n') {
  183.                                 exit(0);
  184.                         }
  185.                 } else {
  186.                         // make the transfer
  187.                         account[*userId].acctBalance2 += amount;
  188.                         account[*userId].acctBalance1 -= amount;
  189.                         printf( "\n\n\n+--------------------+"
  190.                                         "| Transfer complete. |"
  191.                                         "+--------------------+\n\n\n");
  192.                         printSummary(*userId);
  193.                 }
  194.         }
  195.         std::cout << "Press [Enter] to continue" << std::endl;
  196.         _getch();
  197. }
  198.  
  199. void saveData() {//save all user account data to a csv file
  200. //ACCTNAME, ACCTNUMBER1, ACCTBALANCE1, ACCTINTEREST1, ACCTNUMBER2, ACCTBALANCE2, ACCT INTEREST2, PASSWORD,
  201. }
  202.  
  203. void accountDeposit(int *userId) {
  204.        
  205.         int acctDepNum = 0;
  206.         int acctDepAmt = 0;
  207.  
  208.         std::cout       <<      "Please enter the account number you wish to deposit to"        <<      std::endl
  209.                                 <<      "1."    <<      std::endl //user ACCTNUMBER 1
  210.                                 <<      "2."    <<      std::endl //user acctnumber 2
  211.                                 <<      std::endl;
  212.         std::cin        >>      acctDepNum;
  213.  
  214.         std::cout       <<      "Please enter deposit amount"   <<      std::endl;
  215.         std::cin        >>      acctDepNum;
  216.  
  217.         if (acctDepNum == 1) {
  218.                 account[*userId].acctBalance1 +=        acctDepAmt;
  219.         }
  220.         if (acctDepNum == 2) {
  221.                 account[*userId].acctBalance2 += acctDepAmt;
  222.         }
  223.         saveAccounts();
  224.         return; //return to logged in menu
  225. }
  226.  
  227. void accountWithdraw(int *userId) {
  228.        
  229.         int acctWitNum = 0;
  230.         int acctWitAmt = 0;
  231.  
  232.         std::cout<<"Please enter the account number you wish to deposit to"<<std::endl
  233.                  <<"1."<<std::endl //user ACCTNUMBER 1
  234.                  <<"2."<<std::endl //user acctnumber 2
  235.                  <<std::endl;
  236.         std::cin        >>  acctWitNum;
  237.  
  238.         std::cout       <<      "Please enter deposit amount"<< std::endl;
  239.         std::cin        >>      acctWitNum;
  240.  
  241.         if(acctWitNum == 1) {
  242.                 account[*userId].acctBalance1 +=        acctWitAmt;
  243.         }
  244.         if(acctWitNum == 2) {
  245.                 account[*userId].acctBalance2 += acctWitAmt;
  246.         }
  247.         saveAccounts(); //run save data
  248.         return;
  249. }
  250.  
  251. void loggedInMenu(int *userId) {
  252.  
  253.         int loggedinmenuselect = 0;
  254.                 while(loggedinmenuselect == 0) {    //show menu when logged in is complete.
  255.                         std::cout       <<"Hello, acctname! Welcome to your account screen, Please select from the following menu: " <<std::endl;
  256.                         std::cout       <<"1. Print account summary" <<std::endl;
  257.                         std::cout       <<"2. Initiate Balance Transfer"<<std::endl;
  258.                         std::cout       <<"3. Deposit to account"<<std::endl;
  259.                         std::cout       <<"4. Withdraw from account"<<std::endl;
  260.                         std::cout       <<"5. Calculate Interest/ Balance Forecast"<<std::endl;
  261.                         std::cout       <<"6. Log out."<<std::endl;
  262.  
  263.                         std::cin        >>      loggedinmenuselect;
  264.  
  265.                         //1. account summary
  266.                         if(loggedinmenuselect ==1) {
  267.                                 printSummary(*userId);
  268.                         }
  269.                         //2. initiate balance transfer
  270.                         if(loggedinmenuselect ==2) {
  271.                                 transferMoney(userId);
  272.                         }
  273.                         //3. deposit to account
  274.                         if(loggedinmenuselect ==3) {
  275.                                 printSummary(*userId);
  276.                         }
  277.                         //4. withdraw from account
  278.                         if(loggedinmenuselect ==4) {
  279.                                 printSummary(*userId);
  280.                         }
  281.                         //5. balance forecast
  282.                         if(loggedinmenuselect ==5) {
  283.                                 printSummary(*userId);
  284.                         }
  285.                         //6. exit
  286.                         if(loggedinmenuselect ==6) {
  287.                                 //savedata
  288.                                 //break;        Break;  Break;
  289.                         }
  290.                 }
  291. }
  292.  
  293. int main()
  294. {
  295.         time_t _time;
  296.         struct tm timeinfo;
  297.         time ( &_time );
  298.         localtime_s (&timeinfo,&_time);
  299.  
  300.         // loops and menu initialisation
  301.         int n = 0;
  302.         int menuInput = 0;
  303.  
  304.                                         //top level menu
  305.         int subMenuInput = 2;                   //logged in account level menu
  306.         int loggedIn = 0;
  307.  
  308.         // user account details
  309.         int inputInt = 0;
  310.         int userId = 10000;
  311.  
  312.         std::cout << "Welcome to Interbanking Proprietary limited Super Squad" << std::endl;    //Welcome Message
  313.         //createAccount(&n);  
  314.         while(loggedIn == 0) // present the menu screen until the user logs in
  315.         {              
  316.                 std::cout << "Please select from the following menu"    <<std::endl;    //Welcome Menu
  317.                 std::cout << "1. Log in with existing bank account."    <<std::endl;
  318.                 std::cout << "2. Create new bank account."                              <<std::endl;
  319.                 std::cin  >> menuInput;
  320.                 if(menuInput == 1)
  321.                 {
  322.                         login(&n, &loggedIn); // end option 1          
  323.                 }
  324.                 if(menuInput == 2)
  325.                 {
  326.                         createAccount(&n);    
  327.                         menuInput = 0;        
  328.                 }
  329.                
  330.                 login(&userId, &loggedIn);
  331.                
  332.         } // end loggedIn loop 
  333. } // end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement