Advertisement
albela

Untitled

Apr 18th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int accountNumber = 12345;
  5. int pinNumber = 54321;
  6. int balance = 0;
  7.  
  8. bool login () {
  9.    
  10.     int givenAccountNumber = -1;
  11.     int givenPinNumber = -1;
  12.     bool isAccountInvalid = true;
  13.     bool isPinInvalid = true;
  14.    
  15.     cout << "Welcome!" << endl;
  16.    
  17.     while (isAccountInvalid) {
  18.        
  19.         cout << "Please enter your account number: ";
  20.         cin >> givenAccountNumber;
  21.        
  22.         if (givenAccountNumber == accountNumber) {
  23.             isAccountInvalid = false;
  24.         } else {
  25.             cout << "Invalid account number! Try again." << endl;
  26.         }
  27.     }
  28.  
  29.     while (isPinInvalid) {
  30.        
  31.         cout << "Enter your PIN: ";
  32.         cin >> givenPinNumber;
  33.        
  34.         if (givenPinNumber == pinNumber) {
  35.             isPinInvalid = false;
  36.         } else {
  37.             cout << "Invalid PIN number! Try again." << endl;
  38.         }
  39.  
  40.     }
  41.  
  42.     return true;
  43.        
  44. }
  45.  
  46. int getUserOption () {
  47.  
  48.     int selectedOption = -1;
  49.  
  50.     cout << "Main menu:" << endl;
  51.     cout << "   1 - View my balance" << endl;
  52.     cout << "   2 - Withdraw cash" << endl;
  53.     cout << "   3 - Deposit funds" << endl;
  54.     cout << "   4 - Exit" << endl;
  55.     cout << "Enter a choice: " << endl;
  56.     cin >> selectedOption;
  57.    
  58.     return selectedOption;
  59.    
  60. }
  61.  
  62. void viewBalance () {
  63.  
  64.     cout << "You have:" << endl;
  65.     cout << "$" << balance << endl;
  66.  
  67. }
  68.  
  69. void withdrawCash() {
  70.    
  71.     int selectedOption = -1;
  72.     int valueToWithdraw = 0;
  73.     bool isNotFinished = true;
  74.  
  75.     do {
  76.        
  77.         cout << "Withdrawal options:" << endl;
  78.         cout << "1 - $20" << endl;
  79.         cout << "2 - $40" << endl;
  80.         cout << "3 - $60" << endl;
  81.         cout << "4 - $100" << endl;
  82.         cout << "5 - $200" << endl;
  83.         cout << "6 - cancel transaction" << endl;
  84.         cout << "choose a withdrawal option (1-6)" << endl;
  85.  
  86.         cin >> selectedOption;
  87.         switch (selectedOption) {
  88.             case 1:
  89.                 valueToWithdraw = 20;
  90.                 break;
  91.             case 2:
  92.                 valueToWithdraw = 40;
  93.                 break;
  94.             case 3:
  95.                 valueToWithdraw = 60;
  96.                 break;
  97.             case 4:
  98.                 valueToWithdraw = 100;
  99.                 break;
  100.             case 5:
  101.                 valueToWithdraw = 200;
  102.                 break;
  103.             case 6:
  104.                 isNotFinished = false;
  105.                 break;
  106.             default:
  107.                 cout << "Invalid option! Try again." << endl;
  108.                 break;
  109.         }
  110.        
  111.         if (valueToWithdraw != 0) {
  112.             if (valueToWithdraw > balance) {
  113.                 cout << "You just have $" << balance << ". You can't withdraw $" << valueToWithdraw << endl;
  114.             } else {
  115.                 balance = balance - valueToWithdraw;
  116.                 isNotFinished = false;
  117.             }
  118.             valueToWithdraw = 0;
  119.         }
  120.  
  121.     } while (isNotFinished);   
  122.  
  123. }
  124.  
  125. void depositFunds() {
  126.  
  127.     int selectedOption = -1;   
  128.     bool isNotFinished = true;
  129.  
  130.     do {
  131.        
  132.         cout << "Deposit options:" << endl;
  133.         cout << "1 - $20" << endl;
  134.         cout << "2 - $40" << endl; 
  135.         cout << "3 - $60" << endl;
  136.         cout << "4 - $100" << endl;
  137.         cout << "5 - $200" << endl;
  138.         cout << "6 - cancel transaction" << endl;
  139.         cout << "choose a deposit option (1-6)" << endl;
  140.  
  141.         cin >> selectedOption;
  142.         switch (selectedOption) {
  143.             case 1:
  144.                 balance = balance + 20;
  145.                 isNotFinished = false;
  146.                 break;
  147.             case 2:
  148.                 balance = balance + 40;
  149.                 isNotFinished = false;
  150.                 break;
  151.             case 3:
  152.                 balance = balance + 60;
  153.                 isNotFinished = false;
  154.                 break;
  155.             case 4:
  156.                 balance = balance + 100;
  157.                 isNotFinished = false;
  158.                 break;
  159.             case 5:
  160.                 balance = balance + 200;
  161.                 isNotFinished = false; 
  162.                 break;
  163.             case 6:
  164.                 isNotFinished = false;
  165.                 break;
  166.             default:
  167.                 cout << "Invalid option! Try again." << endl;
  168.                 break;
  169.         }
  170.        
  171.     } while (isNotFinished);
  172.  
  173. }
  174.  
  175. int main () {
  176.                
  177.     if (login()) {
  178.        
  179.         int isNotFinished = true;
  180.  
  181.         do {
  182.            
  183.             switch (getUserOption()) {
  184.                 case 1:
  185.                     viewBalance();
  186.                     break;
  187.                 case 2:
  188.                     withdrawCash();
  189.                     break;
  190.                 case 3:
  191.                     depositFunds();
  192.                     break;
  193.                 case 4:
  194.                     isNotFinished = false;
  195.                     break;
  196.                 default:
  197.                     cout << "Invalid option! Try again." << endl;
  198.                     break;
  199.             }
  200.  
  201.         } while (isNotFinished);
  202.  
  203.     }
  204.  
  205.     return 0;
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement