-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatm.cpp
More file actions
163 lines (143 loc) · 3.7 KB
/
atm.cpp
File metadata and controls
163 lines (143 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <fstream>
using namespace std;
string accountNumber = "12345";
string pin = "0000";
double balance = 10000;
// Function declarations
void loadData();
void saveData();
bool login();
void menu();
void checkBalance();
void withdrawMoney();
void depositMoney();
void changePIN();
int main() {
loadData();
cout << "=====================================\n";
cout << " WELCOME TO ATM SYSTEM\n";
cout << "=====================================\n";
if (login()) {
menu();
} else {
cout << "\nToo many wrong attempts. Card blocked!\n";
}
return 0;
}
// Load data from file
void loadData() {
ifstream file("account.txt");
if (file) {
file >> accountNumber >> pin >> balance;
file.close();
}
}
// Save data to file
void saveData() {
ofstream file("account.txt");
file << accountNumber << " " << pin << " " << balance;
file.close();
}
// User login
bool login() {
string acc, enteredPin;
int attempts = 3;
while (attempts > 0) {
cout << "\nEnter Account Number: ";
cin >> acc;
cout << "Enter PIN: ";
cin >> enteredPin;
if (acc == accountNumber && enteredPin == pin) {
cout << "\nLogin Successful!\n";
return true;
} else {
attempts--;
cout << "Wrong credentials. Attempts left: " << attempts << endl;
}
}
return false;
}
// Main menu
void menu() {
int choice;
do {
cout << "\n========== MAIN MENU ==========\n";
cout << "1. Check Balance\n";
cout << "2. Withdraw Money\n";
cout << "3. Deposit Money\n";
cout << "4. Change PIN\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
checkBalance();
break;
case 2:
withdrawMoney();
break;
case 3:
depositMoney();
break;
case 4:
changePIN();
break;
case 5:
saveData();
cout << "\nThank you for using ATM!\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 5);
}
// Check balance
void checkBalance() {
cout << "\nYour current balance is: Rs. " << balance << endl;
}
// Withdraw money
void withdrawMoney() {
double amount;
cout << "\nEnter amount to withdraw: ";
cin >> amount;
if (amount <= 0) {
cout << "Invalid amount!\n";
} else if (amount > balance) {
cout << "Insufficient balance!\n";
} else {
balance -= amount;
cout << "Please collect your cash.\n";
cout << "Remaining balance: Rs. " << balance << endl;
saveData();
}
}
// Deposit money
void depositMoney() {
double amount;
cout << "\nEnter amount to deposit: ";
cin >> amount;
if (amount <= 0) {
cout << "Invalid amount!\n";
} else {
balance += amount;
cout << "Amount deposited successfully.\n";
cout << "Updated balance: Rs. " << balance << endl;
saveData();
}
}
// Change PIN
void changePIN() {
string oldPin, newPin;
cout << "\nEnter old PIN: ";
cin >> oldPin;
if (oldPin == pin) {
cout << "Enter new PIN: ";
cin >> newPin;
pin = newPin;
cout << "PIN changed successfully.\n";
saveData();
} else {
cout << "Wrong old PIN!\n";
}
}