-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuaibank.c
370 lines (286 loc) · 9.08 KB
/
uaibank.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <time.h>
typedef struct {
int id;
char *name;
int age;
float current_balance;
} User;
int search_index_by_id(int id, User users[], int users_qnty);
void write_users_to_file(User *users, int users_qnty) {
FILE *file = fopen("file.txt", "w");
if (file == NULL) {
printf("Erro ao abrir o arquivo.\n");
return;
}
for (int i = 0; i < users_qnty; i++) {
User user = users[i];
fprintf(file, "%d, %s, %d, %.2f\n", user.id, user.name, user.age, user.current_balance);
}
fclose(file);
}
int random_number() {
srand(time(NULL));
int random_number = rand() % 1000 + 1;
return random_number;
}
int get_new_user_id(User *users, int users_qnty) {
int id = random_number();
if (search_index_by_id(id, users, users_qnty) != -1) {
return get_new_user_id(users, users_qnty);
}
return id;
}
User* parse_user_line(char *line) {
User *user = (User*)(malloc(sizeof(User)));
char *id = (char*)(malloc(strlen(line) + 1));
char *name = (char*)(malloc(101));
char *age = (char*)(malloc(4));
char *current_balance = (char*)(malloc(strlen(line) + 1));
user->id = 0;
strcpy(name, "");
user->age = 0;
user->current_balance = 0.0;
int count = 0;
int size = 0;
for (int i = 0; i < strlen(line); i++) {
if (line[i] == ',' && line[i + 1] == ' ') {
count++;
i += 2;
}
switch (count) {
case 0:
id[i] = line[i];
id[i + 1] = '\0';
break;
case 1:
name[i - strlen(id) - 2] = line[i];
name[i - strlen(id) - 1] = '\0';
break;
case 2:
age[i - strlen(id) - strlen(name) - 4] = line[i];
age[i - strlen(id) - strlen(name) - 3] = '\0';
break;
case 3:
current_balance[i - strlen(id) - strlen(name) - strlen(age) - 6] = line[i];
current_balance[i - strlen(id) - strlen(name) - strlen(age) - 5] = '\0';
break;
default:
break;
};
}
user->id = atoi(id);
user->name = (char*)(malloc(101 * sizeof(char)));
snprintf(user->name, 101 * sizeof(char), name);
user->age = atoi(age);
user->current_balance = atof(current_balance);
free(name);
free(age);
free(current_balance);
free(id);
return user;
}
int parse_users(User **users) {
char line[256];
int index = 0;
User *tempUsers = NULL;
FILE *file = fopen("file.txt", "r");
if (file == NULL) {
file = fopen("file.txt", "w");
}
printf("Carregando banco de dados...\n");
while(fgets(line, sizeof(line), file) != NULL) {
line[strcspn(line, "\n")] = 0;
if (line[0] == '\0') {
continue;
}
User *user = parse_user_line(line);
tempUsers = (User*)(realloc(*users, sizeof(User) * (index + 1)));
*users = tempUsers;
memcpy(&(*users)[index], user, sizeof(User));
free(user);
index++;
}
printf("[%d usuários carregados!]\n", index);
fclose(file);
return index;
}
User* new_user(int users_qnty, User *users) {
char *name = (char*)malloc(101 * sizeof(char));
int age = 0;
float current_balance = 0.0;
User *user = (User*)malloc(sizeof(User));
if (user == NULL || name == NULL) {
printf("Error: memory allocation for user failed");
free(name);
return NULL;
}
do {
printf("Digite o nome do usuário: \n-> ");
scanf(" %[^\n]%*c", name);
if (strlen(name) > 100) {
printf("O nome deve ter menos que 100 caracteres. Tente novamente.\n ");
}
} while (strlen(name) + 1 > 100);
printf("Digite a idade do usuário: \n-> ");
scanf(" %d", &age);
while (age < 0) {
printf("Digite a idade do usuário: \n-> ");
scanf(" %d", &age);
printf("A idade deve ser maior de zero.\n");
}
printf("Digite o saldo atual do usuário: \n-> R$ ");
scanf(" %f", ¤t_balance);
while (current_balance < 0) {
printf("Saldo deve ser maior ou igual a zero.\n");
printf("Digite o saldo atual do usuário: \n-> R$ ");
scanf(" %f", ¤t_balance);
}
user->id = get_new_user_id(users, users_qnty);
user->name = (char*)malloc(101 * sizeof(char));
snprintf(user->name, 101 * sizeof(char), name);
user->age = age;
user->current_balance = current_balance;
printf("\nUsuário %d adicionado com sucesso\n", user->id);
return user;
}
int search_index_by_id(int id, User users[], int users_qnty) {
for (int i = 0; i < users_qnty; i++) {
if (users[i].id == id) {
return i;
}
}
return -1;
}
void transfer_money(int id_origin, int id_destination, User users[], int users_qnty, float amount) {
int user_origin_index = search_index_by_id(id_origin, users, users_qnty);
int user_destination_index = search_index_by_id(id_destination, users, users_qnty);
if(user_origin_index == -1 || user_destination_index == -1){
printf("Algum dos usuários não foi encontrado.\n");
return;
} else if (users[user_origin_index].current_balance - amount < 0) {
printf("Saldo insuficiente.\n");
return;
};
users[user_origin_index].current_balance -= amount;
users[user_destination_index].current_balance += amount;
printf("Transferência entre usuários feita com sucesso!\n");
return;
}
void remove_user(int searching_id, User **users, int *users_qnty) {
int user_index = search_index_by_id(searching_id, *users, *users_qnty);
if (user_index == -1) {
printf("Erro: Usuário %d não encontrado.\n", searching_id);
return;
}
for (int j = user_index; j < *users_qnty - 1; j++) {
(*users)[j] = (*users)[j + 1];
}
(*users_qnty) -= 1;
User *new_users = realloc(*users, sizeof(User) * (*users_qnty));
if (new_users == NULL && *users_qnty > 0) {
printf("Erro ao realocar memória.\n");
return;
}
*users = new_users;
printf("Usuario %d deletado com sucesso\n", searching_id);
}
int main() {
setlocale(LC_ALL, "Portuguese_Brasil.1252");
User *users = (User*) malloc(sizeof(User));
int added_users = parse_users(&users);
if (users == NULL) {
printf("Error: memory allocation for users failed");
return 1;
}
int opt;
int users_qnty = added_users;
printf("############ BEM-VIND@ AO UAIBANK! ############\n");
do {
printf("\nSelecione uma opção: \n[1] - Criar um novo usuário\n[2] - Criar multiplos novos usuários\n[3] - Buscar um usuário por ID\n[4] - Transferência de saldo entre usuários\n[5] - Deletar um usuário por ID\n[qualquer outra tecla] - Sair\n-> ");
scanf(" %d", &opt);
switch (opt) {
case 1:
printf("\n------- CRIAR NOVO USUARIO -------\n");
users = (User*) realloc(users, sizeof(User) * (users_qnty + 1));
User *user = new_user(users_qnty, users);
if(user == NULL) {
free(users);
printf("Error: memory allocation for user failed");
break;
}
users[users_qnty] = *user;
write_users_to_file(users, users_qnty);
free(user);
users_qnty++;
break;
case 2:
printf("\n------- CRIAR MULTIPLOS USUARIOS -------\n");
int users_qnty_in_row = 0;
printf("Digite a quantidade de usuários: \n-> ");
scanf("%d", &users_qnty_in_row);
for (int i = users_qnty_in_row; i > 0; i--) {
users = (User*) realloc(users, sizeof(User) * (users_qnty + 1));
User *user = new_user(users_qnty, users);
if(user == NULL) {
free(users);
printf("Error: memory allocation for user failed");
break;
}
users[users_qnty] = *user;
write_users_to_file(users, users_qnty);
free(user);
users_qnty++;
}
printf("Operação concluída. %d usuários adicionados\n", users_qnty_in_row);
break;
case 3:
printf("\n------- PROCURAR USUARIO -------\n");
int id;
printf("Digite o id do usuário que deseja buscar:\n-> ");
scanf("%d", &id);
int user_id = search_index_by_id(id, users, users_qnty);
if (user_id == -1) {
printf("Erro: Usuário %d não encontrado.\n", id);
break;
}
User user_found = users[user_id];
printf("\nID: %03d\nNome: %s\nIdade: %d\nSaldo atual: R$ %.2f\n", user_found.id, user_found.name, user_found.age, user_found.current_balance);
break;
case 4:
printf("\n------- TRANSFERENCIA -------\n");
int id_origin, id_destination;
float amount = 0.0;
printf("Digite o ID de origem\n-> ");
scanf("%d", &id_origin);
printf("Digite o ID de destino\n-> ");
scanf("%d", &id_destination);
if (id_origin < 0 || id_destination < 0) {
printf("Digite um ID valido.\n");
break;
}
printf("Digite a quantia que deseja transferir\n-> R$ ");
scanf("%f", &amount);
transfer_money(id_origin, id_destination, users, users_qnty, amount);
break;
case 5: {
printf("\n------- DELETAR USUARIO -------\n");
int id;
printf("Digite o ID do usuário que deseja remover:\n-> ");
scanf("%d", &id);
remove_user(id, &users, &users_qnty);
write_users_to_file(users, users_qnty);
break;
}
default:
break;
}
} while (opt <= 5 && opt > 0);
write_users_to_file(users, users_qnty);
printf("Dados gravados com sucesso em file.txt\n\n");
free(users);
return 0;
}