-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathATM.cpp
215 lines (179 loc) · 6.92 KB
/
ATM.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h> // For Linux
// #include <windows.h> // Uncomment this line for Windows users and comment out 'unistd.h'
using namespace std;
class ATM {
private:
long int account_No;
string name;
int PIN;
double balance;
string mobile_No;
vector<string> transactionHistory;
bool isLocked = false;
int pinAttempts = 0;
public:
void setData(long int account_No_a, string name_a, int PIN_a, double balance_a, string mobile_No_a) {
account_No = account_No_a;
name = name_a;
PIN = PIN_a;
balance = balance_a;
mobile_No = mobile_No_a;
}
long int getAccountNo() { return account_No; }
string getName() { return name; }
int getPIN() { return PIN; }
double getBalance() { return balance; }
string getMobileNo() { return mobile_No; }
vector<string> getTransactionHistory() { return transactionHistory; }
bool getLockStatus() { return isLocked; }
void setMobile(string mob_prev, string mob_new) {
if (mob_prev == mobile_No) {
mobile_No = mob_new;
cout << endl << "Successfully Updated Mobile no.";
getchar();
} else {
cout << endl << "Incorrect Old Mobile no!";
getchar();
}
}
void cashWithDraw(int amount_a) {
if (amount_a > 0 && amount_a <= balance && amount_a <= 20000) { // Added the withdrawal limit condition
balance -= amount_a;
string transaction = "Withdrew: " + to_string(amount_a);
transactionHistory.push_back(transaction);
cout << endl << "Please Collect Your Cash";
cout << endl << "Available Balance: " << balance;
getchar();
} else if (amount_a > 20000) {
cout << endl << "Withdrawal limit exceeded! You can only withdraw up to 20000.";
getchar();
} else {
cout << endl << "Invalid Input or Insufficient Balance";
getchar();
}
}
void lockAccount() {
isLocked = true;
cout << endl << "Your account has been locked due to multiple incorrect attempts." << endl;
getchar();
}
void trackPinAttempts() {
pinAttempts++;
if (pinAttempts >= 3) {
lockAccount();
}
}
void resetPinAttempts() {
pinAttempts = 0;
}
void unlockAccount() {
isLocked = false;
pinAttempts = 0;
cout << endl << "Your account has been unlocked. Please try again with correct details.";
getchar();
}
};
int main() {
int choice = 0, enterPIN;
long int enterAccountNo;
system("clear");
ATM user1;
user1.setData(987654321, "Hardik", 1234, 50000, "9370054900");
do {
system("clear");
cout << endl << "****Welcome to ATM*****" << endl;
cout << endl << "Enter Your Account No: ";
cin >> enterAccountNo;
cout << endl << "Enter PIN: ";
cin >> enterPIN;
if ((enterAccountNo == user1.getAccountNo()) && (enterPIN == user1.getPIN())) {
user1.resetPinAttempts();
do {
int amount = 0;
string oldMobileNo, newMobileNo;
if (user1.getLockStatus()) {
cout << "Account is locked. Please contact support." << endl;
break;
}
system("clear");
cout << endl << "**** Welcome to ATM *****" << endl;
cout << endl << "Select an option: ";
cout << endl << "1. Check Balance";
cout << endl << "2. Cash Withdraw";
cout << endl << "3. Show User Details";
cout << endl << "4. Update Mobile No.";
cout << endl << "5. View Transaction History";
cout << endl << "6. Lock Account (for security purposes)";
cout << endl << "7. Exit" << endl;
cin >> choice;
switch (choice) {
case 1:
cout << endl << "Your Bank balance is: " << user1.getBalance();
getchar();
break;
case 2:
cout << endl << "Enter the amount: ";
cin >> amount;
user1.cashWithDraw(amount);
break;
case 3:
cout << endl << "*** User Details are: ***";
cout << endl << "-> Account No: " << user1.getAccountNo();
cout << endl << "-> Name: " << user1.getName();
cout << endl << "-> Balance: " << user1.getBalance();
cout << endl << "-> Mobile No.: " << user1.getMobileNo();
getchar();
break;
case 4:
cout << endl << "Enter Old Mobile No.: ";
cin >> oldMobileNo;
cout << endl << "Enter New Mobile No.: ";
cin >> newMobileNo;
user1.setMobile(oldMobileNo, newMobileNo);
break;
case 5:
{
vector<string> transactions = user1.getTransactionHistory();
cout << endl << "*** Transaction History ***";
for (const string& transaction : transactions) {
cout << endl << transaction;
}
getchar();
}
break;
case 6:
user1.lockAccount();
break;
case 7:
exit(0);
default:
cout << endl << "Enter Valid Data !!!";
break;
}
} while (true);
} else {
cout << endl << "User Details are Invalid !!! ";
user1.trackPinAttempts();
if (user1.getLockStatus()) {
char retryChoice;
cout << endl << "Would you like to try again? (y/n): ";
cin >> retryChoice;
if (retryChoice == 'y' || retryChoice == 'Y') {
cout << "Please wait for 30 seconds before retrying..." << endl;
// Wait for 30 seconds (using sleep function for Linux)
sleep(30); // Use Sleep(30000) on Windows instead
user1.unlockAccount();
continue; // Restart the loop for a new login attempt
} else {
cout << "Exiting. Please try again later.";
break; // Exit the program
}
}
getchar();
}
} while (true);
return 0;
}