-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank_system.c
332 lines (329 loc) · 7.45 KB
/
bank_system.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
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
331
332
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
typedef struct
{
unsigned int accid;
char name[16];
char surname[16];
double balance;
} Account;
typedef struct
{
unsigned int lastid;
bool success;
} Config;
bool open_file(FILE**, const char*, const char*);
char file_check(const char*);
bool check_files();
bool save_last_id(Config);
Config get_last_id();
unsigned int insert_customer(Account*);
unsigned int delete_customer(unsigned int);
unsigned int add_money(unsigned int, double);
Account* select_customer(unsigned int);
bool list_all(bool);
bool open_file(FILE** fpt, const char* name, const char* mode)
{
*fpt = fopen(name, mode);
return *fpt == NULL ? false : true;
}
char file_check(const char* name)
{
FILE* fpt;
char state = open_file(&fpt, name, "rb");
if (!state)
{
state = open_file(&fpt, name, "wb");
if (state) state = -1;
}
if (state)
fclose(fpt);
return state;
}
bool check_files()
{
char customers, confings;
customers = file_check("customers.dat");
confings = file_check("confings.dat");
if (customers && confings)
{
if (confings < 0 || customers < 0)
{
FILE* fpt;
if (confings > 0)
{
open_file(&fpt, "confings.dat", "wb");
fclose(fpt);
}
if (customers > 0)
{
open_file(&fpt, "customers.dat", "wb");
fclose(fpt);
}
Config cfg = { 0, true };
if (!save_last_id(cfg))
return false;
}
return true;
}
else
return false;
}
bool save_last_id(Config cfg)
{
FILE* fpt;
bool success = false;
if (open_file(&fpt, "confings.dat", "rb+"))
{
if (!fseek(fpt, 0, SEEK_SET))
success = fwrite(&cfg, sizeof(cfg), 1, fpt);
fclose(fpt);
}
return success;
}
Config get_last_id()
{
FILE* fpt;
Config cfg = { 0, false };
bool success = false;
if (open_file(&fpt, "confings.dat", "rb"))
{
if (!fseek(fpt, 0, SEEK_SET))
success = fread(&cfg, sizeof(cfg), 1, fpt);
fclose(fpt);
}
return cfg;
}
unsigned int insert_customer(Account* acc)
{
bool state = false;
Config cfg = get_last_id();
if (cfg.success)
cfg.lastid++;
if (cfg.lastid)
{
cfg.success = false;
if (save_last_id(cfg))
{
acc->accid = cfg.lastid;
FILE* fpt;
if (open_file(&fpt, "customers.dat", "rb+"))
{
if (!fseek(fpt, (acc->accid - 1) * sizeof(Account), SEEK_SET))
{
if (fwrite(acc, sizeof(Account), 1, fpt))
{
cfg.success = true;
save_last_id(cfg);
state = true;
}
}
fclose(fpt);
}
}
}
return state ? acc->accid : 0;
}
unsigned int delete_customer(unsigned int id)
{
FILE* fpt;
bool state = false;
unsigned int deleted;
Config cfg = get_last_id();
if ((cfg.lastid -= !cfg.success) > 0 && id <= cfg.lastid)
{
if (open_file(&fpt, "customers.dat", "rb+"))
{
if (!fseek(fpt, (id - 1) * sizeof(Account), SEEK_SET))
{
deleted = id;
if (fread(&id, sizeof(id), 1, fpt) && id)
{
fseek(fpt, (id - 1) * sizeof(Account), SEEK_SET);
id = 0;
if (fwrite(&id, sizeof(id), 1, fpt))
state = true;
}
}
fclose(fpt);
}
}
return state ? deleted : 0;
}
unsigned int add_money(unsigned int id, double amount)
{
FILE* fpt;
Account acc;
bool state = false;
Config cfg = get_last_id();
if ((cfg.lastid -= !cfg.success) > 0 && id <= cfg.lastid)
{
if (open_file(&fpt, "customers.dat", "rb+"))
{
if (!fseek(fpt, (id - 1) * sizeof(Account), SEEK_SET))
{
if (fread(&acc, sizeof(Account), 1, fpt) && acc.accid)
{
acc.balance += amount;
fseek(fpt, (id - 1) * sizeof(Account), SEEK_SET);
if (fwrite(&acc, sizeof(Account), 1, fpt))
state = true;
}
}
fclose(fpt);
}
}
return state ? id : 0;
}
Account* select_customer(unsigned int id)
{
FILE* fpt;
static Account acc;
bool state = false;
Config cfg = get_last_id();
if ((cfg.lastid -= !cfg.success) > 0 && id <= cfg.lastid)
{
if (open_file(&fpt, "customers.dat", "r"))
{
if (!fseek(fpt, (id - 1) * sizeof(Account), SEEK_SET))
if (fread(&acc, sizeof(Account), 1, fpt) && acc.accid)
state = true;
fclose(fpt);
}
}
return state ? &acc : NULL;
}
bool list_all(bool withdel)
{
FILE* fpt, * fpt_out;
bool state = false;
Account acc;
Config cfg = get_last_id();
if ((cfg.lastid -= !cfg.success) > 0)
{
if (open_file(&fpt, "customers.dat", "rb"))
{
if (open_file(&fpt_out, "list.txt", "w"))
{
if (fprintf(fpt_out, "%-16s%-16s%-16s%-16s\n", "ID", "NAME", "SURNAME", "BALANCE"))
{
size_t i;
for (i = 0; i < cfg.lastid; i++)
if (!fseek(fpt, i * sizeof(Account), SEEK_SET))
if (fread(&acc, sizeof(Account), 1, fpt))
{
if (acc.accid || (!acc.accid && withdel))
if (!fprintf(fpt_out, "%-16u%-16s%-16s%-14.2lf\n", acc.accid, acc.name, acc.surname, acc.balance))
break;
}
else
break;
else break;
if (i == cfg.lastid)
state = true;
}
fclose(fpt_out);
}
fclose(fpt);
}
}
return state;
}
int main()
{
puts("-------BANK SYSTEM-------");
if (check_files())
puts("files have checked.");
else
{
puts("files dont exist.");
exit(1);
}
char input[48] = { 0 }, cmd[16] = { 0 }, param1[16] = { 0 }, param2[16] = { 0 };
while (true)
{
printf("\n>>> ");
scanf("%47[^\n]s", input);
sscanf(input, "%15s%15s%15s", cmd, param1, param2);
getchar();
if (!strcmp("insert", cmd))
{
Account acc;
printf(">>> (name, surname, balance): ");
if (scanf("%s %s %lf", acc.name, acc.surname, &acc.balance) == 3)
if (insert_customer(&acc))
puts("the account has saved successful.\n");
else
puts("the account could not saved.\n");
else
puts("invalid input\n");
getchar();
}
else if (!strcmp("delete", cmd))
{
unsigned int id = atol(param1);
if (id > 0)
if (select_customer(id))
if (delete_customer(id))
puts("the account has deleted successful.\n");
else
puts("the account could not delete.\n");
else
puts("the account could not find.\n");
else
puts("invalid input\n");
}
else if (!strcmp("add", cmd))
{
unsigned int id = atol(param1);
double amount = atof(param2);
if (id > 0 && amount)
if (select_customer(id))
if (add_money(id, amount))
puts("the account has updated successful.\n");
else
puts("the account could not update.\n");
else
puts("the account could not find.\n");
else
puts("invalid input\n");
}
else if (!strcmp("select", cmd))
{
unsigned int id = atol(param1);
if (id > 0)
{
Account* acc = select_customer(id);
if (acc != NULL)
{
printf("%-16s%-16s%-16s%-16s\n", "ID", "NAME", "SURNAME", "BALANCE");
printf("%-16u%-16s%-16s%-14.2lf\n", acc->accid, acc->name, acc->surname, acc->balance);
}
else
puts("the account could not find.\n");
}
else
puts("invalid input\n");
}
else if (!strcmp("list", cmd))
{
char wdel = atoi(param1);
if (list_all(wdel))
puts("accounts have listed.\n");
else
puts("accounts could not list.\n");
}
else if (!strcmp("exit", cmd))
break;
else if (!strcmp("help", cmd))
puts("insert\ndelete [id]\nselect [id]\nadd [id] [amount]\nlist\nexit\n");
else
puts("invalid command.\n");
cmd[0] = 0;
param1[0] = 0;
param2[0] = 0;
}
return 0;
}