fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class BankAccount {
  5. private:
  6. std::string accountHolder;
  7. int accountNumber;
  8. int pin;
  9. double balance;
  10.  
  11. public:
  12. // Constructor
  13. BankAccount(std::string name, int accNum, int userPin, double initialBalance)
  14. : accountHolder(name), accountNumber(accNum), pin(userPin), balance(initialBalance) {}
  15.  
  16. // Verify PIN
  17. bool authenticate(int inputPin) const {
  18. return pin == inputPin;
  19. }
  20.  
  21. // Getters
  22. double getBalance() const {
  23. return balance;
  24. }
  25.  
  26. std::string getAccountHolder() const {
  27. return accountHolder;
  28. }
  29.  
  30. // Account Actions
  31. void deposit(double amount) {
  32. if (amount > 0) {
  33. balance += amount;
  34. std::cout << "\nSuccessfully deposited $" << amount << "\n";
  35. std::cout << "New Balance: $" << balance << "\n";
  36. } else {
  37. std::cout << "\nInvalid deposit amount.\n";
  38. }
  39. }
  40.  
  41. bool withdraw(double amount) {
  42. if (amount <= 0) {
  43. std::cout << "\nInvalid withdrawal amount.\n";
  44. return false;
  45. }
  46. if (amount > balance) {
  47. std::cout << "\nInsufficient funds! Current Balance: $" << balance << "\n";
  48. return false;
  49. }
  50. balance -= amount;
  51. std::cout << "\nPlease collect your cash: $" << amount << "\n";
  52. std::cout << "Remaining Balance: $" << balance << "\n";
  53. return true;
  54. }
  55. };
  56.  
  57. int main() {
  58. // Default account setup (Holder, Account No, PIN, Starting Balance)
  59. BankAccount userAccount("Alex Morgan", 100234, 1234, 500.00);
  60.  
  61. int enteredPin;
  62. int attempts = 0;
  63. bool authenticated = false;
  64.  
  65. std::cout << "====================================\n";
  66. std::cout << " WELCOME TO MINI ATM \n";
  67. std::cout << "====================================\n";
  68.  
  69. // PIN Authentication Loop (Max 3 Attempts)
  70. while (attempts < 3) {
  71. std::cout << "Enter your 4-digit PIN: ";
  72. std::cin >> enteredPin;
  73.  
  74. if (userAccount.authenticate(enteredPin)) {
  75. authenticated = true;
  76. break;
  77. } else {
  78. attempts++;
  79. std::cout << "Incorrect PIN. Attempts left: " << (3 - attempts) << "\n\n";
  80. }
  81. }
  82.  
  83. if (!authenticated) {
  84. std::cout << "Card locked due to multiple incorrect attempts. Exiting...\n";
  85. return 0;
  86. }
  87.  
  88. // ATM Main Menu
  89. int choice;
  90. do {
  91. std::cout << "\n------------------------------------\n";
  92. std::cout << "Welcome, " << userAccount.getAccountHolder() << "!\n";
  93. std::cout << "1. Check Balance\n";
  94. std::cout << "2. Deposit Money\n";
  95. std::cout << "3. Withdraw Money\n";
  96. std::cout << "4. Exit\n";
  97. std::cout << "Choose an option (1-4): ";
  98. std::cin >> choice;
  99.  
  100. switch (choice) {
  101. case 1:
  102. std::cout << "\nYour Current Balance: $" << userAccount.getBalance() << "\n";
  103. break;
  104.  
  105. case 2: {
  106. double depositAmount;
  107. std::cout << "Enter amount to deposit: $";
  108. std::cin >> depositAmount;
  109. userAccount.deposit(depositAmount);
  110. break;
  111. }
  112.  
  113. case 3: {
  114. double withdrawAmount;
  115. std::cout << "Enter amount to withdraw: $";
  116. std::cin >> withdrawAmount;
  117. userAccount.withdraw(withdrawAmount);
  118. break;
  119. }
  120.  
  121. case 4:
  122. std::cout << "\nThank you for using Mini ATM. Goodbye!\n";
  123. break;
  124.  
  125. default:
  126. std::cout << "\nInvalid choice. Please pick between 1 and 4.\n";
  127. }
  128. } while (choice != 4);
  129.  
  130. return 0;
  131. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
====================================
       WELCOME TO MINI ATM          
====================================
Enter your 4-digit PIN: Incorrect PIN. Attempts left: 2

Enter your 4-digit PIN: Incorrect PIN. Attempts left: 1

Enter your 4-digit PIN: Incorrect PIN. Attempts left: 0

Card locked due to multiple incorrect attempts. Exiting...