Guest User

Untitled

a guest
Dec 6th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4. #include <conio.h>
  5. #include <stdio.h>
  6.  
  7. struct User {
  8.     int acctNumber1;            //User account number 1
  9.     int acctNumber2;
  10.     double acctBalance1;        //user account balance 1
  11.     double acctBalance2;       
  12.     char acctName[256];         //User account Name
  13.     char acctPass[256];         //user account password
  14.     int logInNumber;
  15.     bool isAdmin;
  16. } account[8999]; // 9999 - 1000 because we virtually start at record '1000'
  17.  
  18. /*  Note that using namespace std is *deliberately* not used here, because it can generate ambiguity errors
  19.     Instead cin and cout are prefixed with std:: */
  20.  
  21. void printSummary(int user) {
  22.     printf("\n\n"
  23.            "+-----------------+\n"
  24.            "| Account Summary |\n"
  25.            "+=================+\n\n"
  26.            "+------------------------------------+\n"
  27.            "| Account | Account number | Balance |\n"
  28.            "+---------+----------------+---------+\n"
  29.            "| 1       | %d           | %.2lf   \n"
  30.            "| 2       | %d           | %.2lf   \n\n\n",
  31.            account[user].acctNumber1, account[user].acctBalance1, account[user].acctNumber2, account[user].acctBalance2);
  32. }
  33.  
  34. void createAccount(int *n) {
  35.     *n = *n+1;
  36.     int *inputInt = new int[1];
  37.     std::cout << "Please Fill out the following information to create a new account" << std::endl;
  38.     std::cout << "Full name: ";
  39.     std::cin >> account[*n].acctName;
  40.     std::cout << "Password: ";
  41.     std::cin >> account[*n].acctPass;
  42.                
  43.     // get first account number
  44.     std::cout << "First account number: ";
  45.     std::cin >> *inputInt;
  46.     while(*inputInt < 1000 || *inputInt > 9999) { // check none of the users already have this account number, if they do keep prompting
  47.         std::cout   << "Account number invalid. Must be between 1000 and 9999.\n"
  48.                     << "------------------------------------------------------\n"
  49.                     << "First account number: ";                   
  50.         std::cin >> *inputInt;
  51.     }
  52.     account[*n].acctNumber1 = *inputInt; // valid (new) account number, add it to the user
  53.  
  54.     // get first account balance
  55.     std::cout << "First account Balance: ";
  56.     std::cin >> account[*n].acctBalance1;
  57.  
  58.     // get second account number
  59.     std::cout << "Second account number: ";
  60.     std::cin >> *inputInt;
  61.     while(*inputInt < 1000 || *inputInt > 8999) { // check none of the users already have this account number, if they do keep prompting
  62.         std::cout   << "Account number invalid. Must be between 1000 and 9999.\n"
  63.                     << "------------------------------------------------------\n"
  64.                     << "Second account number: ";
  65.         std::cin >> *inputInt;
  66.     }
  67.     account[*n].acctNumber2 = *inputInt; // valid (new) account number, add it to the user
  68.  
  69.     //get second account balance
  70.     std::cout << "Second account balance: ";
  71.     std::cin >> account[*n].acctBalance2;
  72.  
  73.     printSummary(*n);
  74. }
  75.  
  76. // returns string (array of char) for username
  77. void login(int *userId, int *loggedIn) {
  78.     char *inputText = new char[256];
  79.     std::cout << "Please enter username: ";
  80.     std::cin >> inputText;
  81.     for(int i = 0; i < 8999; i++) {
  82.         if ( strcmp (account[i].acctName, inputText) == 0 ) {
  83.             std::cout << "Welcome " << account[i].acctName << ", Please enter your password: " << std::endl;
  84.             std::cin >> inputText;
  85.             if ( strcmp (account[i].acctPass, inputText) == 0 ) {
  86.                 printSummary(i);
  87.                 std::cout << "Press [Enter] to continue" << std::endl;
  88.                 _getch();
  89.                 *loggedIn = 1;
  90.                 account[i].logInNumber++;
  91.                 *userId = i;
  92.             }
  93.             else {
  94.                 std::cout << "Incorrect password." << std::endl;
  95.                 break;
  96.             }
  97.         }
  98.     }
  99. }
  100.  
  101.  
  102.  
  103. void transferMoney(int *userId) {
  104.     double amount = 0.00;
  105.     bool enoughFunds = false;
  106.     std::cout   << "Please enter amount to transer: ";
  107.     std::cin    >> amount;
  108.     if(account[*userId].acctNumber1 == account[*userId].acctNumber2) {
  109.         printf( "   ^       +-----------+\n"
  110.                 " / ! \\     | Declined: |  Account numbers are the same.\n"
  111.                 "/____ \\    +-----------+\n\n");
  112.     }
  113.     if(amount > account[*userId].acctBalance1) {
  114.         printf( "   ^       +-----------+\n"
  115.                 " / ! \\     | Declined: |  Not enough founds in account number %d\n"
  116.                 "/____ \\    +-----------+\n\n",
  117.                 account[*userId].acctNumber1);
  118.     } else { enoughFunds = true; }
  119.     if(enoughFunds && (account[*userId].acctBalance1 - amount) < 10.00) {
  120.         printf( "+----------+\n"
  121.                 "| Warning: |   Account number %d now has a balance less than $10.00.\n"
  122.                 "+----------+\n\n",
  123.                 account[*userId].acctNumber1);
  124.         account[*userId].acctBalance2 += amount;
  125.         account[*userId].acctBalance1 -= amount;
  126.         std::cout << "Press [Enter] to continue" << std::endl;
  127.         _getch();
  128.         enoughFunds = false; // do this to prevent a second transfer happening
  129.     }
  130.     if(enoughFunds) {
  131.         if((amount + account[*userId].acctBalance2) > 100000.00) {
  132.             printf( "+----------+\n"
  133.                     "| Warning: |   Account number %d will have a balance greater\n"
  134.                     "+----------+   than the federally insurable amount of\n"
  135.                     "               $100,000.00 if you proceed.\n\n",
  136.                     account[*userId].acctNumber2);
  137.             char yn = 'x';     
  138.             // loop until the input is a y or n
  139.             while(yn != 'y' && yn != 'n') {
  140.                 std::cout << "Continue with transfer? Select [Y]es or [N]o" << std::endl;
  141.                 std::cin >> yn;
  142.             }
  143.             if(yn = 'n') {
  144.                 exit(0);
  145.             }
  146.         } else {
  147.             // make the transfer
  148.             account[*userId].acctBalance2 += amount;
  149.             account[*userId].acctBalance1 -= amount;
  150.             printf( "\n\n\n+--------------------+"
  151.                     "| Transfer complete. |"
  152.                     "+--------------------+\n\n\n");
  153.             printSummary(*userId);
  154.         }
  155.     }
  156.     std::cout << "Press [Enter] to continue" << std::endl;
  157.     _getch();
  158. }
  159.  
  160. int main()
  161. {
  162.     time_t _time;
  163.     struct tm timeinfo;
  164.     time ( &_time );
  165.     localtime_s (&timeinfo,&_time);
  166.  
  167.     // loops and menu initialisation
  168.     int n = 0;
  169.     int menuInput = 0;              //top level menu
  170.     int subMenuInput = 0;           //logged in account level menu
  171.     int loggedIn = 0;
  172.  
  173.     // user account details
  174.     int inputInt = 0;
  175.     int userId = 10000;
  176.  
  177.     std::cout << "Welcome to Interbanking Proprietary limited Super Squad" << std::endl;    //Welcome Message
  178.     createAccount(&n); 
  179.     while (!loggedIn) // present the menu screen until the user logs in
  180.     {
  181.         /*
  182.         std::cout << "Please select from the following menu"    <<std::endl;    //Welcome Menu
  183.         std::cout << "1. Log in with existing bank account."    <<std::endl;
  184.         std::cout << "2. Create new bank account."              <<std::endl;
  185.         std::cin  >> menuInput;
  186.         switch(menuInput)
  187.         {
  188.             case 1:
  189.                 login(&n, &loggedIn);
  190.             break; // end option 1     
  191.             case 2:            
  192.                 createAccount(&n); 
  193.                 // reset menu
  194.                 menuInput = 0;
  195.             break;      } // end switch menuInput
  196.         */     
  197.         login(&userId, &loggedIn);
  198.        
  199.     } // end loggedIn loop
  200.     transferMoney(&userId);
  201. } // end main
Add Comment
Please, Sign In to add comment