Guest User

ATM simulator

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