Guest User

ATM Sim With password

a guest
Aug 10th, 2012
1,365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  OS = Windows 7
  2.     Compiler = CodeBlocks 10.05
  3.  
  4.     ATM simulator - Password is "1234"
  5. */
  6.  
  7. #include <iostream>
  8. #include <windows.h>
  9. #include <Stdlib.h>
  10. #include <ctime>
  11. #include <string>
  12. #include <cstdio>
  13. using namespace std;
  14.  
  15. char ch156 = 156; // £ sign
  16.  
  17. int password();
  18. int selectMenu ();
  19. double depositMoney(double mon);
  20. double widrawMoney(double WMoney);
  21. double balanceInquiry (double bal);
  22. char tryAgain();
  23. void quitProgram( );
  24.  
  25. HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  26. COORD coord;
  27.  
  28. // Thanks to NULL on (http://www.cplusplus.com/articles/E6vU7k9E/) for your wisdom
  29. string getpass(const char *prompt, bool show_asterisk = true)
  30. {
  31.      const char BACKSPACE = 8;
  32.      const char RETURN = 13;
  33.      int i = 0;
  34.  
  35.      string password;
  36.      unsigned char ch = 0;
  37.  
  38.      coord.X = 13;
  39.      coord.Y = 8;
  40.      SetConsoleCursorPosition(console, coord);
  41.      cout << prompt << endl;
  42.  
  43.      DWORD con_mode;
  44.      DWORD dwRead;
  45.  
  46.      HANDLE hIn=GetStdHandle(STD_INPUT_HANDLE);
  47.  
  48.      GetConsoleMode( hIn, &con_mode );
  49.      SetConsoleMode( hIn, con_mode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT) );
  50.  
  51.      while(ReadConsoleA( hIn, &ch, 1, &dwRead, NULL) && ch !=RETURN)
  52.      {
  53.           if(ch==BACKSPACE)
  54.           {
  55.                if(password.length()!=0)
  56.                {
  57.                     if(show_asterisk)
  58.                          cout <<"\b \b";
  59.                     password.resize(password.length()-1);
  60.                }
  61.           }
  62.           else
  63.           {
  64.                password+=ch;
  65.                if(show_asterisk)
  66.                {
  67.                     coord.X = 13 + i;
  68.                     coord.Y = 8;
  69.                     SetConsoleCursorPosition(console, coord);
  70.                     cout <<'*';
  71.                     i++;
  72.                }
  73.           }
  74.      }
  75.      coord.X = 0;
  76.      coord.Y = 0;
  77.      SetConsoleCursorPosition(console, coord);
  78.      SetConsoleMode( hIn, con_mode );  
  79.      return password;
  80. }
  81.  
  82. // MSDN - alternative to system("CLS");
  83. void cls(HANDLE hConsole)
  84. {
  85.    COORD coordScreen = {0, 0};    // home for the cursor
  86.    DWORD cCharsWritten;
  87.    CONSOLE_SCREEN_BUFFER_INFO csbi;
  88.    DWORD dwConSize;
  89.  
  90.    // Get the number of character cells in the current buffer.
  91.    if( !GetConsoleScreenBufferInfo(hConsole, &csbi))
  92.    {
  93.       return;
  94.    }
  95.    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  96.  
  97.    // Fill the entire screen with blanks.
  98.    if( !FillConsoleOutputCharacter(hConsole,        // Handle to console screen buffer
  99.                                    (TCHAR) ' ',     // Character to write to the buffer
  100.                                    dwConSize,       // Number of cells to write
  101.                                    coordScreen,     // Coordinates of first cell
  102.                                    &cCharsWritten ))// Receive number of characters written
  103.    {
  104.       return;
  105.    }
  106.  
  107.    // Get the current text attribute.
  108.    if( !GetConsoleScreenBufferInfo(hConsole, &csbi))
  109.    {
  110.       return;
  111.    }
  112.  
  113.    // Set the buffer's attributes accordingly.
  114.    if( !FillConsoleOutputAttribute(hConsole,         // Handle to console screen buffer
  115.                                    csbi.wAttributes, // Character attributes to use
  116.                                    dwConSize,        // Number of cells to set attribute
  117.                                    coordScreen,      // Coordinates of first cell
  118.                                    &cCharsWritten )) // Receive number of characters written
  119.    {
  120.       return;
  121.    }
  122.  
  123.    // Put the cursor at its home coordinates.
  124.    SetConsoleCursorPosition(hConsole, coordScreen);
  125. }
  126.  
  127. int main()
  128. {
  129.      double balance = 10000.00;
  130.      char ch;
  131.      int select;
  132.  
  133.     password();
  134.  
  135.      do
  136.      {
  137.           select = selectMenu();
  138.           switch (select)
  139.           {
  140.                case 1://add code here for DepositMoney
  141.                     balance = depositMoney(balance);
  142.  
  143.                     break;
  144.                case 2: //add code here for widrawMoney
  145.                     balance = widrawMoney(balance);
  146.  
  147.                     break;
  148.                case 3://add code here for balanceInquiry;
  149.                     balance = balanceInquiry(balance);
  150.  
  151.                     break;
  152.                case 4://add code here forquitProgram();
  153.                     quitProgram();
  154.                     ch = 'N';
  155.                     break;
  156.           }
  157.           if (ch != 'N')
  158.           {
  159.                ch = tryAgain();
  160.           }
  161.      }
  162.      while (ch=='Y' || ch == 'y');
  163.  
  164.      return 0;
  165. }
  166.  
  167. int password()
  168. {
  169.     const char *correct_password="1234";
  170.  
  171.     //main double boarder
  172.     char ch201 = 201; char ch199 = 198; char ch196 = 196; char ch182 = 182;
  173.     char ch205 = 205; char ch186 = 186; char ch187 = 187; char ch204 = 204;
  174.     char ch185 = 185; char ch200 = 200; char ch188 = 188; char prev = ' ';
  175.  
  176.     //internal PIN border
  177.     char ch218 = 218; char ch191 = 191;
  178.     char ch179 = 179;
  179.     char ch192 = 192; char ch217 = 217;
  180.  
  181.     //Password screen
  182.      cls(console);
  183.      cout << ch201; cout.fill (205); cout.width (31); cout << ch187 << endl;
  184.      cout << ch186 << " Welcome to ABC Banking Group " << ch186 << endl;
  185.      cout << ch204; cout.fill (205); cout.width (31); cout << ch185 << endl;
  186.      cout.fill (prev);
  187.      cout << ch186; cout.width (31); cout << ch186 << endl;
  188.      cout << ch186; cout << "      Please enter your       " << ch186 << endl;
  189.      cout << ch186; cout << "         4 digit PIN          " << ch186 << endl;
  190.      cout << ch186; cout.width (31); cout << ch186 << endl;
  191.      cout << ch186; cout << "          " << ch218 << ch196 << ch196 << ch196 << ch196 << ch196 << ch196 << ch196 << ch191 << "           " << ch186 << endl;
  192.      cout << ch186; cout << "          " << ch179 << "       " << ch179 <<"           "  << ch186 << endl;
  193.      cout << ch186; cout << "          " << ch192 << ch196 << ch196 << ch196 << ch196 << ch196 << ch196 << ch196 << ch217 << "           " << ch186 << endl;
  194.      cout << ch186; cout.width (31); cout << ch186 << endl;
  195.      cout << ch186; cout.width (31); cout << ch186 << endl;
  196.      cout << ch186; cout.width (31); cout << ch186 << endl;
  197.      cout << ch186; cout.width (31); cout << ch186 << endl;
  198.      cout << ch200; cout.fill (205); cout.width (31); cout << ch188 << endl;
  199.  
  200.      string password = getpass("",true); // Show asterisks
  201.      if(password == correct_password)
  202.     //Todo - 1: error message if password is wrong and limmit number of attemps to 3.
  203.     //       2: After 3 attemps lock the user out and advise the to contact the bank.
  204.  
  205.      return 0;
  206. }
  207.  
  208. int selectMenu ()
  209. {
  210.      int choice = 0;
  211.      char ch201 = 201; char ch199 = 198; char ch196 = 196; char ch182 = 182;
  212.      char ch205 = 205; char ch186 = 186; char ch187 = 187; char ch204 = 204;
  213.      char ch185 = 185; char ch200 = 200; char ch188 = 188; char prev = ' ';
  214.      //Select screen
  215.      cls(console);
  216.      cout << ch201; cout.fill (205); cout.width (31); cout << ch187 << endl;
  217.      cout << ch186 << " Welcome to ABC Banking Group " << ch186 << endl;
  218.      cout << ch204; cout.fill (205); cout.width (31); cout << ch185 << endl;
  219.      cout.fill (prev);
  220.      cout << ch186; cout.width (31); cout << ch186 << endl;
  221.      cout << ch186; cout << "          Main Menu           " << ch186 << endl;
  222.      cout << ch186; cout.width (31); cout << ch186 << endl;
  223.      cout << ch204; cout.fill (196); cout.width (31); cout << ch185 << endl;
  224.      cout << ch186; cout << "      1 - Depsit Money        " << ch186 << endl;
  225.      cout << ch204; cout.fill (196); cout.width (31); cout << ch185 << endl;
  226.      cout << ch186; cout << "      2 - Withdraw Money      " << ch186 << endl;
  227.      cout << ch204; cout.fill (196); cout.width (31); cout << ch185 << endl;
  228.      cout << ch186; cout << "      3 - Balance Inquiry     " << ch186 << endl;
  229.      cout << ch204; cout.fill (196); cout.width (31); cout << ch185 << endl;
  230.      cout << ch186; cout << "      4 - Quit and Exit?      " << ch186 << endl;
  231.      cout << ch200; cout.fill (205); cout.width (31); cout << ch188 << endl;
  232.      cin >> choice;
  233.  
  234.      do
  235.      {
  236.           if((!(choice >= 1)) || (!(choice <= 4)))
  237.           {
  238.                cout << " INVALID Choice! Please choose options 1 - 4 " << endl;
  239.                cin >> choice;
  240.           }
  241.      }
  242.      while((!(choice >= 1)) || (!(choice <= 4)));
  243.  
  244.      return choice;
  245. }
  246.  
  247. double depositMoney(double mon)
  248. {
  249.      double amount = 0;
  250.  
  251.      cout << "Enter amount to deposit: " << ch156 ;
  252.      cin >> amount;
  253.      if(amount < 500)
  254.      {
  255.           do
  256.           {
  257.                cout << " INCORRECT Amount! Please depsit over " << ch156 << "500.00" << endl;
  258.                cout << " Enter amount to deposit: " << ch156 ;
  259.                cin >> amount;
  260.           }
  261.           while( amount < 500);
  262.      }
  263.      else
  264.      {
  265.           cout << " Transaction Sucsessful - Thank You!" << endl<< endl;
  266.           mon += amount;
  267.      }
  268.  
  269.      return mon;
  270. }
  271.  
  272. double widrawMoney(double WMoney)
  273. {
  274.      double amount = 0;
  275.      bool draw;
  276.      draw = true;
  277.  
  278.      if (WMoney >= 5000)
  279.      {
  280.           cout << "You can draw an amount between " << ch156 << " 100.00 - "
  281.                << ch156 << "5000.00" << endl;
  282.                draw = true;
  283.      }
  284.      if ((WMoney > 100) && (WMoney < 5000))
  285.      {
  286.           cout << "You can draw an amount between " << ch156 << " 100.00 - "
  287.                << ch156 << WMoney << endl;
  288.                draw = true;
  289.      }
  290.      if(WMoney < 100)
  291.      {
  292.           cout << "INSUFFICIONT FUNDS!" << endl;
  293.           draw = false;
  294.      }
  295.      if (draw = true)
  296.      {
  297.           cout << "Enter the amount you wish to withdraw " << ch156 ;
  298.           cin >> amount;
  299.           if(amount > WMoney)
  300.           {
  301.                do
  302.                {
  303.                     cout << " INCORRECT Amount! You can draw an amount between ";
  304.                     if(WMoney >= 5000)
  305.                     {
  306.                          cout << ch156 << " 100.00 - " << ch156 << "5000" << endl;
  307.                     }
  308.                     else if(WMoney < 5000)
  309.                     {
  310.                          cout << ch156 << " 100.00 - " << ch156 << WMoney << endl;
  311.                     }
  312.                     cout << " Enter amount to withdraw : " << ch156 ;
  313.                     cin >> amount;
  314.                }
  315.                while( amount > WMoney);
  316.           }
  317.           WMoney -= amount;
  318.      }
  319.      double total;
  320.      int thous, fiveH, twoH, oneH;
  321.      total = amount;
  322.      cout << "\nAmount Breakdown :" << endl;
  323.      if (amount >= 1000)
  324.      {
  325.           thous = int(amount) / 1000;
  326.           cout << "Number of " << ch156 << "1000  : " << thous << endl;
  327.           amount -= (thous * 1000);
  328.      }
  329.      if (amount >= 500)
  330.      {
  331.           fiveH = int(amount) / 500;
  332.           cout << "Number of " << ch156 << "500   : " << fiveH << endl;
  333.           amount -= (fiveH * 500);
  334.      }
  335.      if (amount >= 200)
  336.      {
  337.           twoH = int(amount) / 200;
  338.           cout << "Number of " << ch156 << "200   : " << twoH << endl;
  339.           amount -= (twoH * 200);
  340.      }
  341.      if (amount >= 100)
  342.      {
  343.           oneH = int(amount) / 100;
  344.           cout << "Number of " << ch156 << "100   : " << oneH << endl;
  345.           amount -= (oneH * 100);
  346.      }
  347.      cout << "\nTotal amount withdrawn " << ch156 << total << endl << endl;
  348.  
  349.      return WMoney;
  350. }
  351.  
  352. double balanceInquiry(double bal)
  353. {
  354.      cout << "Your current balance is " << ch156 << bal << endl << endl;
  355.  
  356.      return bal;
  357. }
  358.  
  359. char tryAgain()
  360. {
  361.      char ch;
  362.  
  363.      cout << "Would you like another transaction? [Y/N] : ";
  364.      cin >> ch;
  365.      ch = (toupper(ch));
  366.  
  367.      if (ch != 'Y' && ch != 'N')
  368.      {
  369.           do
  370.           {
  371.                cout << "Invalid Selection! Please choose Y/N : ";
  372.                cin >> ch;
  373.                ch = (toupper(ch));
  374.           }
  375.           while (ch != 'Y' && ch != 'N') ;
  376.      }
  377.      if(ch == 'N')
  378.           quitProgram();
  379.      return ch;
  380. }
  381.  
  382. void quitProgram()
  383. {
  384.     //main double boarder
  385.      char ch201 = 201; char ch199 = 198; char ch196 = 196; char ch182 = 182;
  386.      char ch205 = 205; char ch186 = 186; char ch187 = 187; char ch204 = 204;
  387.      char ch185 = 185; char ch200 = 200; char ch188 = 188; char prev = ' ';
  388.      char ch;
  389.     //Exit screen
  390.      cls(console);
  391.      cout << ch201; cout.fill (205); cout.width (31); cout << ch187 << endl;
  392.      cout << ch186 << " Welcome to ABC Banking Group " << ch186 << endl;
  393.      cout << ch204; cout.fill (205); cout.width (31); cout << ch185 << endl;
  394.      cout.fill (prev);
  395.      cout << ch186; cout.width (31); cout << ch186 << endl;
  396.      cout << ch186; cout.width (31); cout << ch186 << endl;
  397.      cout << ch186; cout.width (31); cout << ch186 << endl;
  398.      cout << ch186; cout << "     Thank you for using      " << ch186 << endl;
  399.      cout << ch186; cout.width (31); cout << ch186 << endl;
  400.      cout << ch186; cout << "      ABC Banking Group       " << ch186 << endl;
  401.      cout << ch186; cout.width (31); cout << ch186 << endl;
  402.      cout << ch186; cout.width (31); cout << ch186 << endl;
  403.      cout << ch186; cout.width (31); cout << ch186 << endl;
  404.      cout << ch186; cout.width (31); cout << ch186 << endl;
  405.      cout << ch186; cout.width (31); cout << ch186 << endl;
  406.      cout << ch200; cout.fill (205); cout.width (31); cout << ch188 << endl;
  407.  
  408.      // Delay the exit screen incase your compiler exits at the last return in main.
  409.      // It also allows the user to acknowledge the exit screen.
  410.      time_t start,end;
  411.      double dif = 0.0;
  412.  
  413.      time (&start);
  414.      do
  415.      {
  416.         time (&end);
  417.         dif = difftime (end,start);
  418.      }while(dif < 4);  // 3 second delay
  419. }
Advertisement
Add Comment
Please, Sign In to add comment