-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeAlpha_BankingSystem.c
More file actions
330 lines (275 loc) · 12 KB
/
Copy pathCodeAlpha_BankingSystem.c
File metadata and controls
330 lines (275 loc) · 12 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* ============================================================
* BANK ACCOUNT MANAGEMENT SYSTEM
* Language : C
* Features : Deposit | Withdraw | Balance Enquiry | Exit
* Storage : File Handling (accounts.dat)
* ============================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ─── Data Structure ─────────────────────────────────────── */
typedef struct {
int accNumber;
char name[50];
float balance;
} Account;
/* ─── File name constant ─────────────────────────────────── */
#define FILE_NAME "accounts.dat"
/* ─── Function Prototypes ────────────────────────────────── */
void createAccount();
void deposit();
void withdraw();
void balanceEnquiry();
void listAllAccounts();
int findAccount(int accNo, Account *acc);
void updateAccount(Account acc);
void displayMenu();
void clearScreen();
void printBanner();
void printDivider();
/* ══════════════════════════════════════════════════════════
MAIN
══════════════════════════════════════════════════════════ */
int main() {
int choice;
printBanner();
while (1) {
displayMenu();
printf(" Enter your choice : ");
if (scanf("%d", &choice) != 1) {
/* flush bad input */
while (getchar() != '\n');
printf("\n [!] Invalid input. Please enter a number.\n");
continue;
}
switch (choice) {
case 1: createAccount(); break;
case 2: deposit(); break;
case 3: withdraw(); break;
case 4: balanceEnquiry(); break;
case 5: listAllAccounts(); break;
case 6:
printf("\n Thank you for using the Bank System. Goodbye!\n\n");
exit(0);
default:
printf("\n [!] Invalid choice. Please try again.\n");
}
}
return 0;
}
/* ══════════════════════════════════════════════════════════
DISPLAY MENU
══════════════════════════════════════════════════════════ */
void displayMenu() {
printDivider();
printf(" MAIN MENU\n");
printDivider();
printf(" 1. Create New Account\n");
printf(" 2. Deposit Money\n");
printf(" 3. Withdraw Money\n");
printf(" 4. Balance Enquiry\n");
printf(" 5. List All Accounts\n");
printf(" 6. Exit\n");
printDivider();
}
/* ══════════════════════════════════════════════════════════
CREATE ACCOUNT
══════════════════════════════════════════════════════════ */
void createAccount() {
Account acc;
FILE *fp;
printDivider();
printf(" CREATE NEW ACCOUNT\n");
printDivider();
printf(" Enter Account Number : ");
scanf("%d", &acc.accNumber);
/* Check for duplicate */
Account temp;
if (findAccount(acc.accNumber, &temp)) {
printf("\n [!] Account %d already exists!\n", acc.accNumber);
return;
}
printf(" Enter Account Holder : ");
getchar(); /* flush newline */
fgets(acc.name, sizeof(acc.name), stdin);
acc.name[strcspn(acc.name, "\n")] = '\0'; /* strip newline */
printf(" Enter Initial Deposit : Rs. ");
scanf("%f", &acc.balance);
if (acc.balance < 0) {
printf("\n [!] Initial deposit cannot be negative.\n");
return;
}
fp = fopen(FILE_NAME, "ab");
if (!fp) {
printf("\n [!] Error opening file.\n");
return;
}
fwrite(&acc, sizeof(Account), 1, fp);
fclose(fp);
printf("\n [✔] Account created successfully!\n");
printf(" Account No : %d | Name : %s | Balance : Rs. %.2f\n",
acc.accNumber, acc.name, acc.balance);
}
/* ══════════════════════════════════════════════════════════
DEPOSIT
══════════════════════════════════════════════════════════ */
void deposit() {
int accNo;
float amount;
Account acc;
printDivider();
printf(" DEPOSIT MONEY\n");
printDivider();
printf(" Enter Account Number : ");
scanf("%d", &accNo);
if (!findAccount(accNo, &acc)) {
printf("\n [!] Account not found.\n");
return;
}
printf(" Current Balance : Rs. %.2f\n", acc.balance);
printf(" Enter Deposit Amount : Rs. ");
scanf("%f", &amount);
if (amount <= 0) {
printf("\n [!] Deposit amount must be positive.\n");
return;
}
acc.balance += amount;
updateAccount(acc);
printf("\n [✔] Rs. %.2f deposited successfully.\n", amount);
printf(" New Balance : Rs. %.2f\n", acc.balance);
}
/* ══════════════════════════════════════════════════════════
WITHDRAW
══════════════════════════════════════════════════════════ */
void withdraw() {
int accNo;
float amount;
Account acc;
printDivider();
printf(" WITHDRAW MONEY\n");
printDivider();
printf(" Enter Account Number : ");
scanf("%d", &accNo);
if (!findAccount(accNo, &acc)) {
printf("\n [!] Account not found.\n");
return;
}
printf(" Current Balance : Rs. %.2f\n", acc.balance);
printf(" Enter Withdraw Amount : Rs. ");
scanf("%f", &amount);
if (amount <= 0) {
printf("\n [!] Withdrawal amount must be positive.\n");
return;
}
if (amount > acc.balance) {
printf("\n [!] Insufficient funds! Available: Rs. %.2f\n", acc.balance);
return;
}
acc.balance -= amount;
updateAccount(acc);
printf("\n [✔] Rs. %.2f withdrawn successfully.\n", amount);
printf(" Remaining Balance : Rs. %.2f\n", acc.balance);
}
/* ══════════════════════════════════════════════════════════
BALANCE ENQUIRY
══════════════════════════════════════════════════════════ */
void balanceEnquiry() {
int accNo;
Account acc;
printDivider();
printf(" BALANCE ENQUIRY\n");
printDivider();
printf(" Enter Account Number : ");
scanf("%d", &accNo);
if (!findAccount(accNo, &acc)) {
printf("\n [!] Account not found.\n");
return;
}
printf("\n ┌─────────────────────────────────────┐\n");
printf(" │ Account No : %-20d │\n", acc.accNumber);
printf(" │ Name : %-20s │\n", acc.name);
printf(" │ Balance : Rs. %-16.2f │\n", acc.balance);
printf(" └─────────────────────────────────────┘\n");
}
/* ══════════════════════════════════════════════════════════
LIST ALL ACCOUNTS
══════════════════════════════════════════════════════════ */
void listAllAccounts() {
Account acc;
FILE *fp;
int count = 0;
printDivider();
printf(" ALL ACCOUNTS\n");
printDivider();
printf(" %-10s %-25s %s\n", "Acc. No", "Account Holder", "Balance (Rs.)");
printDivider();
fp = fopen(FILE_NAME, "rb");
if (!fp) {
printf(" No accounts found.\n");
return;
}
while (fread(&acc, sizeof(Account), 1, fp) == 1) {
printf(" %-10d %-25s %.2f\n", acc.accNumber, acc.name, acc.balance);
count++;
}
fclose(fp);
printDivider();
if (count == 0)
printf(" No accounts found.\n");
else
printf(" Total Accounts : %d\n", count);
}
/* ══════════════════════════════════════════════════════════
HELPER : Find Account by Number
Returns 1 if found, 0 otherwise. Fills *acc.
══════════════════════════════════════════════════════════ */
int findAccount(int accNo, Account *acc) {
FILE *fp = fopen(FILE_NAME, "rb");
if (!fp) return 0;
while (fread(acc, sizeof(Account), 1, fp) == 1) {
if (acc->accNumber == accNo) {
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
/* ══════════════════════════════════════════════════════════
HELPER : Update an Existing Account Record in File
══════════════════════════════════════════════════════════ */
void updateAccount(Account updated) {
FILE *fp, *temp;
Account acc;
fp = fopen(FILE_NAME, "rb");
temp = fopen("temp.dat", "wb");
if (!fp || !temp) {
printf("\n [!] File error during update.\n");
return;
}
while (fread(&acc, sizeof(Account), 1, fp) == 1) {
if (acc.accNumber == updated.accNumber)
fwrite(&updated, sizeof(Account), 1, temp);
else
fwrite(&acc, sizeof(Account), 1, temp);
}
fclose(fp);
fclose(temp);
remove(FILE_NAME);
rename("temp.dat", FILE_NAME);
}
/* ══════════════════════════════════════════════════════════
UI HELPERS
══════════════════════════════════════════════════════════ */
void printBanner() {
printf("\n");
printf(" ╔══════════════════════════════════════════╗\n");
printf(" ║ BANK ACCOUNT MANAGEMENT SYSTEM v1.0 ║\n");
printf(" ║ Built in C | File-Based ║\n");
printf(" ╚══════════════════════════════════════════╝\n\n");
}
void printDivider() {
printf(" ------------------------------------------\n");
}