C++ Mini Projects: ATM Simulator
This is an ATM simulator that requires users to log in with a PIN, perform transactions, and log out—just like a real ATM machine. Let's build the software inside the ATM!
Skills Used
- Handling files: Logging all transactions to a file
- Conditional statements: Managing invalid inputs and account rules
- Functions: Breaking down the program into smaller, manageable parts
- Loops: Navigating the menu and handling repeated PIN attempts
- Input and output management: Managing user inputs and displaying outputs
The Code to Build the Project
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// Function prototypes
bool login(int correctPIN);
void displayMenu();
void checkBalance(double balance);
void deposit(double &balance, ofstream &logfile);
void withdraw(double &balance, ofstream &logfile);
void logTransaction(ofstream &logfile, const string &type, double amount);
int main() {
const int correctPIN = 1234; // Default PIN
double balance = 1000.0; // Default balance
ofstream logfile("transactions.txt", ios::app); // Logfile for transactions
// Welcome message
cout << "----- ATM Simulator -----\n";
// Login process
if (!login(correctPIN)) {
cout << "Too many failed attempts. Exiting...\n";
return 1;
}
// Main menu loop
int choice;
do {
displayMenu();
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
checkBalance(balance);
break;
case 2:
deposit(balance, logfile);
break;
case 3:
withdraw(balance, logfile);
break;
case 4:
cout << "Thank you for using the ATM!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 4);
logfile.close(); // Close the log file
return 0;
}
// Function to handle login
bool login(int correctPIN) {
int attempts = 0, enteredPIN;
while (attempts < 3) {
cout << "Enter your PIN: ";
cin >> enteredPIN;
if (enteredPIN == correctPIN) {
cout << "Login successful!\n";
return true;
} else {
cout << "Incorrect PIN. Try again.\n";
attempts++;
}
}
return false;
}
// Function to display the menu
void displayMenu() {
cout << "----- Main Menu -----\n";
cout << "1. Check balance\n";
cout << "2. Deposit money\n";
cout << "3. Withdraw money\n";
cout << "4. Exit\n";
}
// Function to check the balance
void checkBalance(double balance) {
cout << "Your current balance: $" << fixed << setprecision(2) << balance << "\n";
}
// Function to deposit money
void deposit(double &balance, ofstream &logfile) {
double amount;
cout << "Enter the amount to deposit: $";
cin >> amount;
if (amount > 0) {
balance += amount;
cout << "Deposit successful. New balance: $" << balance << "\n";
logTransaction(logfile, "Deposit", amount);
} else {
cout << "Invalid amount. Deposit failed.\n";
}
}
// Function to withdraw money
void withdraw(double &balance, ofstream &logfile) {
double amount;
cout << "Enter the amount to withdraw: $";
cin >> amount;
if (amount > 0 && amount <= balance) {
balance -= amount;
cout << "Withdrawal successful. New balance: $" << balance << "\n";
logTransaction(logfile, "Withdrawal", amount);
} else if (amount > balance) {
cout << "Insufficient balance. Withdrawal failed.\n";
} else {
cout << "Invalid amount. Withdrawal failed.\n";
}
}
// Function to log transactions
void logTransaction(ofstream &logfile, const string &type, double amount) {
logfile << type << ": $" << fixed << setprecision(2) << amount << "\n";
}
Explanation
Login Process
- The program prompts the user for a PIN.
- The user has three attempts to enter the correct PIN; otherwise, the program exits.
Menu Management
- The
displayMenu()
function shows the available options:
- Check balance
- Deposit money
- Withdraw money
- Exit
- The
checkBalance()
function displays the current balance.
- The
deposit()
function allows users to deposit money, updating the balance and logging the transaction.
- The
withdraw()
function enables users to withdraw money, ensuring the balance is sufficient.
- All transactions are recorded in
transactions.txt
.
Real-World Examples
- Real ATM transactions: Banks deploy similar logic in ATM machines, integrating database connectivity.
- Data management: In real-world banking applications, account details are stored in a centralized database, and transactions are logged for security purposes.
- Security protocols: Unlike this simple simulation, actual banking software enforces strict security measures.
Security and Error Handling
- In real banking systems, an incorrect PIN attempt would end the session and could temporarily lock the account.
- Additional security layers, such as two-factor authentication (2FA) and biometric scans, are often implemented.
- Banks use transaction logging to detect fraud. If unusual withdrawal patterns are observed, the account may be flagged for review.
- Implementing fraud detection in this ATM simulator would be an important improvement.
Problem-Solving Approaches
Improved Security
- Challenge: Currently, PINs are stored as simple integers.
- Solution: Encrypt stored PINs using a secure hashing algorithm like SHA-256.
Handling Large Transactions
- Challenge: Users may attempt to withdraw more than their available balance.
- Solution: Implement an overdraft protection feature or notify users of a low balance.
Expanded ATM Features
- Challenge: The current program lacks account management.
- Solution: Store multiple balances and accounts in a text file or database for better user management.
Thank you for reading! I hope you learned something valuable. See you next time!
Comments
Post a Comment