-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.c
170 lines (132 loc) · 4.08 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ACCOUNTS 10
typedef struct {
int id;
char name[100];
double balance;
} Account;
void create_account(Account accounts[], int *num_accounts);
void deposit(Account accounts[], int num_accounts);
void withdraw(Account accounts[], int num_accounts);
void transfer(Account accounts[], int num_accounts);
int main() {
Account accounts[MAX_ACCOUNTS];
int num_accounts = 0;
int choice;
printf("Welcome to the banking system\n");
while (1) {
printf("\n1. Create account\n2. Deposit\n3. Withdraw\n4. Transfer\n5. Exit\n\nEnter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
create_account(accounts, &num_accounts);
break;
case 2:
deposit(accounts, num_accounts);
break;
case 3:
withdraw(accounts, num_accounts);
break;
case 4:
transfer(accounts, num_accounts);
break;
case 5:
printf("Exiting program...\n");
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
void create_account(Account accounts[], int *num_accounts) {
if (*num_accounts == MAX_ACCOUNTS) {
printf("Maximum number of accounts reached\n");
return;
}
Account account;
account.id = *num_accounts + 1;
printf("Enter account name: ");
scanf("%s", account.name);
printf("Enter initial balance: ");
scanf("%lf", &account.balance);
accounts[*num_accounts] = account;
(*num_accounts)++;
printf("Account created with ID %d\n", account.id);
}
void deposit(Account accounts[], int num_accounts) {
int id;
double amount;
printf("Enter account ID: ");
scanf("%d", &id);
for (int i = 0; i < num_accounts; i++) {
if (accounts[i].id == id) {
printf("Enter deposit amount: ");
scanf("%lf", &amount);
accounts[i].balance += amount;
printf("Deposit successful. New balance: $%.2lf\n", accounts[i].balance);
return;
}
}
printf("Account not found\n");
}
void withdraw(Account accounts[], int num_accounts) {
int id;
double amount;
printf("Enter account ID: ");
scanf("%d", &id);
for (int i = 0; i < num_accounts; i++) {
if (accounts[i].id == id) {
printf("Enter withdrawal amount: ");
scanf("%lf", &amount);
if (amount > accounts[i].balance) {
printf("Insufficient funds\n");
return;
}
accounts[i].balance -= amount;
printf("Withdrawal successful. New balance: $%.2lf\n", accounts[i].balance);
return;
}
}
printf("Account not found\n");
}
void transfer(Account accounts[], int num_accounts) {
int id1, id2;
double amount;
printf("Enter account ID to transfer from: ");
scanf("%d", &id1);
printf("Enter account ID to transfer to: ");
scanf("%d", &id2);
if (id1 == id2) {
printf("Cannot transfer to the same account\n");
return;
}
int index1 = -1, index2 = -1;
for (int i = 0; i < num_accounts; i++) {
if (accounts[i].id == id1) {
index1 = i;
}
if (accounts[i].id == id2) {
index2 = i;
}
if (index1 != -1 && index2 != -1) {
break;
}
}
if (index1 == -1 || index2 == -1) {
printf("One or both accounts not found\n");
return;
}
printf("Enter transfer amount: ");
scanf("%lf", &amount);
if (amount > accounts[index1].balance) {
printf("Insufficient funds\n");
return;
}
accounts[index1].balance -= amount;
accounts[index2].balance += amount;
printf("Transfer successful. New balance for account %d: $%.2lf\n", accounts[index1].id, accounts[index1].balance);
printf("New balance for account %d: $%.2lf\n", accounts[index2].id, accounts[index2].balance);
}