-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileName.cpp
More file actions
202 lines (176 loc) · 5.05 KB
/
FileName.cpp
File metadata and controls
202 lines (176 loc) · 5.05 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
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct User
{
string username;
string pin;
double balance;
};
User currentUser; // Declare currentUser as a global variable
void saveUser(const User& user)
{
ofstream file("users.txt", ios::app);
if (file.is_open())
{
file << user.username << "," << user.pin << "," << user.balance << "\n";
file.close();
cout << "User account created successfully.\n";
}
else {
cerr << "Unable to open the file.\n";
}
}
bool userExists(const string& username)
{
ifstream file("users.txt");
if (file.is_open())
{
string line;
while (getline(file, line))
{
size_t pos = line.find(",");
string savedUsername = line.substr(0, pos);
if (username == savedUsername)
{
file.close();
return true;
}
}
file.close();
}
else {
cout << "Unable to open the file.\n";
}
return false;
}
bool authenticateUser(const string& username, const string& pin, User& user) {
ifstream file("users.txt");
if (file.is_open())
{
string line;
while (getline(file, line))
{
int pos = line.find(",");
string savedUsername = line.substr(0, pos);
line.erase(0, pos + 1);
pos = line.find(",");
string savedPin = line.substr(0, pos);
line.erase(0, pos + 1);
double savedBalance = stod(line);
if (username == savedUsername && pin == savedPin)
{
user.username = savedUsername;
user.pin = savedPin;
user.balance = savedBalance;
file.close();
return true;
}
}
file.close();
}
else {
cout << "Unable to open the file.\n";
}
return false;
}
void displayMenu(User& user)
{
int choice;
double amount;
do {
cout << "\nATM Menu:\n";
cout << "\n----------------\n";
cout << "1. Withdraw\n";
cout << "2. Deposit\n";
cout << "3. Check Balance\n";
cout << "4. Exit\n";
cout << "----------------\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the amount to withdraw: ";
cin >> amount;
if (amount > 0 && amount <= currentUser.balance)
{
currentUser.balance -= amount;
cout << "Withdrawal successful. Current balance: $" << currentUser.balance << endl;
}
else {
cout << "Invalid amount or insufficient funds.\n";
}
break;
case 2:
cout << "Enter the amount to deposit: ";
cin >> amount;
if (amount > 0) {
currentUser.balance += amount;
cout << "Deposit successful. Current balance: $" << currentUser.balance << endl;
}
else {
cout << "Invalid amount.\n";
}
break;
case 3:
cout << "Current balance: $" << currentUser.balance << endl;
break;
case 4:
cout << "Exiting ATM.\n";
break;
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
} while (choice != 4);
}
int main()
{
int choice;
system("Color 30");
while (true) {
cout << "\n\t\t\t\t\t\tWelcome to the ATM!\n";
cout << "\n----------------\n";
cout << "1. Log In\n";
cout << "2. Create Account\n";
cout << "3. Exit\n";
cout << "----------------\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter Username: ";
cin >> currentUser.username;
cout << "Enter PIN: ";
cin >> currentUser.pin;
if (userExists(currentUser.username) && authenticateUser(currentUser.username, currentUser.pin, currentUser)) {
cout << "Authentication successful. Welcome, " << currentUser.username << "!\n";
displayMenu(currentUser);
}
else {
cout << "Authentication failed. Invalid username or PIN.\n";
}
break;
case 2:
cout << "Enter Username: ";
cin >> currentUser.username;
cout << "Enter PIN: ";
cin >> currentUser.pin;
cout << "Enter deposit amount : ";
cin >> currentUser.balance;
// Set balance to zero only if it's a new account
// currentUser.balance = 0.0;
saveUser(currentUser);
break;
case 3:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
if (choice == 3)
break;
}
return 0;
}