-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
261 lines (220 loc) · 8.11 KB
/
main.cpp
File metadata and controls
261 lines (220 loc) · 8.11 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <iostream>
#include <string>
#include <vector>
#include <limits>
#include "Authentication.h"
#include "BankAccount.h"
// Function prototypes
void displayMainMenu(bool isLoggedIn);
void handleAuthentication(AuthenticationManager& authManager);
void handleBankingOperations(AuthenticationManager& authManager, Bank& bank);
void createAccount(AuthenticationManager& authManager, Bank& bank);
void deposit(Bank& bank, int userId);
void withdraw(Bank& bank, int userId);
void checkBalance(Bank& bank, int userId);
void displayUserAccounts(Bank& bank, int userId);
void clearInputBuffer();
int main() {
// Initialize authentication manager
AuthenticationManager authManager;
// Initialize bank system
Bank bank;
bank.loadAccounts();
bool running = true;
while (running) {
displayMainMenu(authManager.isLoggedIn());
int choice;
std::cout << "Enter your choice: ";
std::cin >> choice;
clearInputBuffer();
if (authManager.isLoggedIn()) {
// User is logged in, show banking options
switch (choice) {
case 1: // Create new account
createAccount(authManager, bank);
break;
case 2: // Deposit
deposit(bank, authManager.getCurrentUserId());
break;
case 3: // Withdraw
withdraw(bank, authManager.getCurrentUserId());
break;
case 4: // Check balance
checkBalance(bank, authManager.getCurrentUserId());
break;
case 5: // View all accounts
displayUserAccounts(bank, authManager.getCurrentUserId());
break;
case 6: // Logout
authManager.logout();
std::cout << "Logged out successfully.\n";
break;
case 7: // Exit
running = false;
break;
default:
std::cout << "Invalid option. Please try again.\n";
break;
}
} else {
// User is not logged in, show authentication options
switch (choice) {
case 1: { // Login
std::string username, password;
std::cout << "Enter username: ";
std::getline(std::cin, username);
std::cout << "Enter password: ";
std::getline(std::cin, password);
if (authManager.login(username, password)) {
std::cout << "Login successful!\n";
} else {
std::cout << "Login failed. Please check your credentials.\n";
}
break;
}
case 2: { // Register
std::string username, password;
std::cout << "Enter username: ";
std::getline(std::cin, username);
std::cout << "Enter password: ";
std::getline(std::cin, password);
if (authManager.registerUser(username, password)) {
std::cout << "Registration successful! You can now login.\n";
} else {
std::cout << "Registration failed. Username might already exist.\n";
}
break;
}
case 3: // Exit
running = false;
break;
default:
std::cout << "Invalid option. Please try again.\n";
break;
}
}
}
std::cout << "Thank you for using our Banking System. Goodbye!\n";
return 0;
}
void displayMainMenu(bool isLoggedIn) {
std::cout << "\n===== Banking System =====\n";
if (isLoggedIn) {
std::cout << "1. Create New Account\n";
std::cout << "2. Deposit\n";
std::cout << "3. Withdraw\n";
std::cout << "4. Check Balance\n";
std::cout << "5. View All Accounts\n";
std::cout << "6. Logout\n";
std::cout << "7. Exit\n";
} else {
std::cout << "1. Login\n";
std::cout << "2. Register\n";
std::cout << "3. Exit\n";
}
}
void clearInputBuffer() {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
void createAccount(AuthenticationManager& authManager, Bank& bank) {
if (!authManager.isLoggedIn()) {
std::cout << "You must be logged in to create an account.\n";
return;
}
std::string accountName;
double initialBalance;
std::cout << "Enter account name: ";
std::getline(std::cin, accountName);
std::cout << "Enter initial balance: $";
std::cin >> initialBalance;
clearInputBuffer();
if (initialBalance < 0) {
std::cout << "Initial balance cannot be negative.\n";
return;
}
// Generate a simple account ID
static int accountCounter = 1000;
std::string accountId = "ACC" + std::to_string(accountCounter++);
BankAccount newAccount(authManager.getCurrentUserId(), accountId, accountName, initialBalance);
bank.addAccount(newAccount);
std::cout << "Account created successfully!\n";
std::cout << "Account ID: " << accountId << "\n";
std::cout << "Account Name: " << accountName << "\n";
std::cout << "Initial Balance: $" << initialBalance << "\n";
}
void deposit(Bank& bank, int userId) {
std::string accountId;
double amount;
std::cout << "Enter account ID: ";
std::getline(std::cin, accountId);
std::cout << "Enter amount to deposit: $";
std::cin >> amount;
clearInputBuffer();
BankAccount* account = bank.findAccount(accountId);
if (!account) {
std::cout << "Account not found.\n";
return;
}
if (account->getUserId() != userId) {
std::cout << "You can only deposit to your own accounts.\n";
return;
}
if (account->deposit(amount)) {
bank.saveAccounts(); // Save the updated balance
std::cout << "Deposit successful! New balance: $" << account->getBalance() << "\n";
} else {
std::cout << "Deposit failed. Amount must be positive.\n";
}
}
void withdraw(Bank& bank, int userId) {
std::string accountId;
double amount;
std::cout << "Enter account ID: ";
std::getline(std::cin, accountId);
std::cout << "Enter amount to withdraw: $";
std::cin >> amount;
clearInputBuffer();
BankAccount* account = bank.findAccount(accountId);
if (!account) {
std::cout << "Account not found.\n";
return;
}
if (account->getUserId() != userId) {
std::cout << "You can only withdraw from your own accounts.\n";
return;
}
if (account->withdraw(amount)) {
bank.saveAccounts(); // Save the updated balance
std::cout << "Withdrawal successful! New balance: $" << account->getBalance() << "\n";
} else {
std::cout << "Withdrawal failed. Insufficient funds or invalid amount.\n";
}
}
void checkBalance(Bank& bank, int userId) {
std::string accountId;
std::cout << "Enter account ID: ";
std::getline(std::cin, accountId);
BankAccount* account = bank.findAccount(accountId);
if (!account) {
std::cout << "Account not found.\n";
return;
}
if (account->getUserId() != userId) {
std::cout << "You can only check balance of your own accounts.\n";
return;
}
account->displayBalance();
}
void displayUserAccounts(Bank& bank, int userId) {
std::vector<BankAccount> userAccounts = bank.findAccountsByUserId(userId);
if (userAccounts.empty()) {
std::cout << "No accounts found for this user.\n";
return;
}
std::cout << "\nYour Accounts:\n";
std::cout << "==============\n";
for (const auto& account : userAccounts) {
account.displayBalance();
}
}