-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.c
546 lines (450 loc) · 15 KB
/
index.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// ! Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// ! Types Definitions
typedef struct
{
int active; // 0:inactive 1:active
char plate[8];
float amount; // amount payed
struct tm time_from; // date registered
struct tm time_to; // checkout date
} Vehicle;
struct Node
{
Vehicle *data; // pointer to vehicle data
struct Node *next; // pointer to the next node
};
typedef struct
{
struct Node *head; // pointer to the first node
struct Node *tail; // pointer to the last node
int actives; // number of active vehicles
int size; // number of vehicles, active or inactive
} List;
// ! Global Variables
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define WHITE "\033[37m"
#define BOLDRED "\033[1m\033[31m"
#define BOLDGREEN "\033[1m\033[32m"
#define BOLDWHITE "\033[1m\033[37m"
// ! Functions Declaration
// * File Functions
FILE *open_file(char *file_name, char *file_mode);
List *retrieve_list(char *list_file_name);
void save_list(List *list, char *list_file_name, int mode, int free_flag); // [mode] 0:save innactivated & 1:save all | [free_flag] 0:don't free | 1:free
// * Lists Functions
struct Node *create_node(Vehicle *vehicle_data);
void insert_node(List *list, Vehicle *vehicle_data);
Vehicle *get_vehicle_search(char *log_file_name, List *list, char *plate, float rate);
// * Systems Functions
int menu();
void status(List *vehicle_list, int garage_size);
void log_record(char *log_file_name, char *log_data);
void register_vehicle(char *log_file_name, char *vehicle_log_file_name, List *vehicle_list, int garage_size, int mode); // [mode] 0:garage && 1:waitlist
void checkout_vehicle(char *log_file_name, List *vehicle_list, float rate);
void checkout_vehicle_wait(char *log_file_name, List *vehicle_list, List *vehicle_wait_list, int garage_size);
void end_day(char *report_file_name, char *log_file_name, List *vehicle_list, float rate);
void print_vehicles(char *log_file_name, List *vehicle_list, float rate);
// ! Functions Definitions
// * System Functions
int main()
{
float rate = 5;
int garage_size = 12;
char *log_file_name = "log.txt";
char *vehicle_log_file_name = "vehicle_log.txt";
char *report_file_name = "report.txt";
char *vehicle_list_file_name = "vehicle_list.bin";
char *vehicle_wait_list_file_name = "vehicle_wait_list.bin";
List *vehicle_list = retrieve_list(vehicle_list_file_name);
List *vehicle_wait_list = retrieve_list(vehicle_wait_list_file_name);
if (vehicle_list == NULL)
{
vehicle_list = (List *)calloc(1, sizeof(List));
}
if (vehicle_wait_list == NULL)
{
vehicle_wait_list = (List *)calloc(1, sizeof(List));
}
int opt = 0;
while (opt != 10)
{
opt = menu();
switch (opt)
{
case 1:
status(vehicle_list, garage_size);
break;
case 2:
register_vehicle(log_file_name, vehicle_log_file_name, vehicle_list, garage_size, 0);
save_list(vehicle_list, vehicle_list_file_name, 0, 0);
break;
case 3:
checkout_vehicle(log_file_name, vehicle_list, rate);
save_list(vehicle_list, vehicle_list_file_name, 0, 0);
break;
case 4:
register_vehicle(log_file_name, vehicle_log_file_name, vehicle_wait_list, garage_size, 1);
save_list(vehicle_wait_list, vehicle_wait_list_file_name, 0, 0);
break;
case 5:
checkout_vehicle_wait(log_file_name, vehicle_list, vehicle_wait_list, garage_size);
save_list(vehicle_list, vehicle_list_file_name, 0, 0);
save_list(vehicle_wait_list, vehicle_wait_list_file_name, 0, 0);
break;
case 6:
end_day(report_file_name, log_file_name, vehicle_list, rate);
save_list(vehicle_list, vehicle_list_file_name, 0, 1);
save_list(vehicle_wait_list, vehicle_wait_list_file_name, 1, 1);
exit(0);
break;
case 7:
print_vehicles(log_file_name, vehicle_list, rate);
break;
case 8:
save_list(vehicle_wait_list, vehicle_wait_list_file_name, 0, 1);
save_list(vehicle_list, vehicle_list_file_name, 0, 1);
printf("\nPrograma Finalizado\n");
exit(0);
break;
default:
printf("\nOpción Incorrecta\n");
break;
}
}
return 0;
}
int menu()
{
int opt = 0;
printf(BOLDWHITE "\n\t\tMenu\n" RESET);
printf(BOLDWHITE "01)" RESET " Disponibilidad de garage\n");
printf(BOLDWHITE "02)" RESET " Registrar un Vehiculo\n");
printf(BOLDWHITE "03)" RESET " Cobrar un Vehiculo\n");
printf(BOLDWHITE "04)" RESET " Agregar auto a lista de espera\n");
printf(BOLDWHITE "05)" RESET " Mover auto de lista de espera\n");
printf(BOLDWHITE "06)" RESET " Terminar el dia y Generar Reporte\n");
printf(BOLDWHITE "07)" RESET " Mostrar Vehiculos en Garage\n");
printf(BOLDWHITE "08)" RESET " Cerrar el programa\n");
printf("\nIngrese una opción: ");
fflush(stdin);
scanf("%d", &opt);
system("clear");
return opt;
}
void status(List *vehicle_list, int garage_size)
{
if ((garage_size - vehicle_list->actives) > 0)
{
printf(BOLDGREEN "[Hay lugar disponible]\n" RESET);
printf("Espacios disponibles en garage: %d\n", (garage_size - vehicle_list->actives));
}
else
{
printf(BOLDRED "\n[No hay lugar disponible]\n" RESET);
}
printf("Autos en garage: %d\n", vehicle_list->actives);
return;
}
void log_record(char *log_file_name, char *log_data)
{
FILE *file_handler = open_file(log_file_name, "a");
time_t t;
struct tm *time_data;
time(&t);
time_data = localtime(&t);
fprintf(file_handler, "[%02d/%02d/%02d - %02d:%02d:%02d]: %s\n", time_data->tm_mday, time_data->tm_mon + 1, time_data->tm_year + 1900, time_data->tm_hour, time_data->tm_min, time_data->tm_sec, log_data);
fclose(file_handler);
return;
}
void register_vehicle(char *log_file_name, char *vehicle_log_file_name, List *vehicle_list, int garage_size, int mode) // [mode] 0:garage | 1:waitlist
{
if (mode == 0 && vehicle_list->actives >= garage_size)
{
printf("\nNo se puede agregar otro auto, garage lleno\n");
return;
}
Vehicle *new_vehicle = (Vehicle *)calloc(1, sizeof(Vehicle));
if (new_vehicle == NULL)
{
printf("\nError de alocamiento de memoria\n");
exit(-1);
}
printf("\nIngrese Placa: ");
fflush(stdin);
gets(new_vehicle->plate);
if (mode == 0)
{
time_t currentTime;
time(¤tTime);
struct tm *time_data;
time_data = localtime(¤tTime);
new_vehicle->time_from = *time_data;
new_vehicle->active = 1;
}
else if (mode == 1)
{
new_vehicle->active = 0;
}
insert_node(vehicle_list, new_vehicle);
if (mode)
{
log_record(log_file_name, "Se registro un nuevo vehiculo en la lista de espera");
}
else
{
log_record(log_file_name, "Se registro un nuevo vehiculo en el garaje");
log_record(vehicle_log_file_name, new_vehicle->plate);
}
return;
}
void checkout_vehicle(char *log_file_name, List *vehicle_list, float rate)
{
time_t currentTime;
time(¤tTime);
struct tm *time_data = localtime(¤tTime);
char plate[8];
printf("\nIngrese Placa: ");
fflush(stdin);
scanf("%s", plate);
Vehicle *vehicle_data = get_vehicle_search(log_file_name, vehicle_list, plate, rate);
if (vehicle_data == NULL)
{
printf("\nNo se encontro el auto\n");
return;
}
int time_diff = (int)difftime(mktime(time_data), mktime(&vehicle_data->time_from));
printf("\nPlaca: %s\n", vehicle_data->plate);
printf("Fecha de Ingreso: %d/%d/%d\n", vehicle_data->time_from.tm_mday, vehicle_data->time_from.tm_mon + 1, vehicle_data->time_from.tm_year + 1900);
if (time_diff < 61)
{
printf("Tiempo de estancia: %d segundos\n", time_diff);
}
else if ((time_diff / 60) < 61)
{
printf("Tiempo de estancia: %d minutos\n", time_diff / 60);
}
else if ((time_diff / 3600) < 24)
{
printf("Tiempo de estancia: %d horas\n", time_diff / 3600);
}
else
{
printf("Tiempo de estancia: %d dias\n", time_diff / 86400);
}
printf("Costo: $%.2f\n", (time_diff / 60) * rate);
printf(BOLDWHITE "\nSe ha realizado el pago?\n" RESET);
printf("1) Si\n");
printf("2) No\n");
printf("Seleccione una opción: ");
int opt = 0;
scanf("%d", &opt);
if (opt == 1)
{
vehicle_data->time_to = *time_data;
vehicle_data->amount = (float)(difftime(mktime(time_data), mktime(&vehicle_data->time_from)) / 60) * rate;
vehicle_data->active = 0;
vehicle_list->actives--;
log_record(log_file_name, "Se retiro un auto");
}
return;
}
void end_day(char *report_file_name, char *log_file_name, List *vehicle_list, float rate)
{
FILE *file_handler = open_file(report_file_name, "a");
float total_amount = 0;
fprintf(file_handler, "\n\n\t\t~ ~ Reporte del dia ~ ~\n");
// Genera Reporte de fin de dia
struct Node *current = (struct Node *)calloc(1, sizeof(struct Node));
current = vehicle_list->head;
for (int i = 0; i < vehicle_list->size; i++)
{
if (current->data->active == 0)
{
total_amount += current->data->amount;
fprintf(file_handler, "[%d/%d/%d] %s : $%.2f\n", current->data->time_from.tm_mday, current->data->time_from.tm_mon + 1, current->data->time_from.tm_year + 1900, current->data->plate, current->data->amount);
}
current = current->next;
}
fprintf(file_handler, "\n\nCierre del dia: $%.2f", total_amount);
printf("\n\nCierre del dia: $%.2f\n\n", total_amount);
fclose(file_handler);
exit(1);
}
void checkout_vehicle_wait(char *log_file_name, List *vehicle_list, List *vehicle_wait_list, int garage_size)
{
if (vehicle_list->actives >= garage_size)
{
printf(BOLDRED "\nNo hay lugar disponible en el garage para mover un vehiculo de la lista de espera\n" RESET);
return;
}
if (vehicle_wait_list->size == 0)
{
printf(BOLDRED "\nNo hay autos en la lista de espera\n" RESET);
return;
}
time_t currentTime;
time(¤tTime);
struct tm *time_data;
time_data = localtime(¤tTime);
vehicle_wait_list->head->data->time_from = *time_data;
vehicle_wait_list->head->data->active = 1;
insert_node(vehicle_list, vehicle_wait_list->head->data);
log_record(log_file_name, "Se registro un nuevo auto");
struct Node *previous = vehicle_wait_list->head;
vehicle_wait_list->head = previous->next;
free(previous->data);
free(previous);
log_record(log_file_name, "Se movio un vehiculo de la lista de espera");
return;
}
// * Lists Functions
struct Node *create_node(Vehicle *vehicle_data)
{
struct Node *new_node = (struct Node *)calloc(1, sizeof(struct Node));
if (new_node == NULL)
{
printf("\nError al crear nodo: Memoria Insuficiente\n");
exit(-1);
}
new_node->data = vehicle_data;
new_node->next = NULL;
return new_node;
}
void insert_node(List *list, Vehicle *vehicle_data)
{
struct Node *new_node = create_node(vehicle_data);
if (list->size == 0)
{
list->head = new_node;
list->tail = new_node;
}
else
{
list->tail->next = new_node;
list->tail = new_node;
}
list->size++;
list->actives++;
return;
}
Vehicle *get_vehicle_search(char *log_file_name, List *list, char *plate, float rate)
{
struct Node *current = list->head;
while (current != NULL)
{
if (strcmp(current->data->plate, plate) == 0)
{
return current->data;
}
current = current->next;
}
printf("\nNo se ha encontrado al auto que se desea desactivar del garage\n");
return NULL;
}
void print_vehicles(char *log_file_name, List *vehicle_list, float rate)
{
if (vehicle_list->size == 0)
{
printf(BOLDRED "\nNo hay autos registrados\n" RESET);
log_record(log_file_name, "Se trato de imprimir los autos en garage, pero esta vacio");
return;
}
printf(BOLDWHITE "\n\n\t\t~ Lista de autos en el garage ~\n" RESET);
time_t currentTime;
time(¤tTime);
struct tm *time_data = localtime(¤tTime);
struct Node *current = vehicle_list->head;
for (int i = 0; i < vehicle_list->size; i++)
{
printf(WHITE "\n\t [Vehiculo]\n" RESET);
printf("Placa: %s\n", current->data->plate);
printf("Fecha de entrada: %d/%d/%d\n", current->data->time_from.tm_mday, current->data->time_from.tm_mon + 1, current->data->time_from.tm_year + 1900);
if (current->data->active)
{
printf("Monto hasta el momento: $%.2f\n", (difftime(mktime(time_data), mktime(¤t->data->time_from)) / 60) * rate);
}
else
{
printf("Monto Cobrado: $%.2f\n", current->data->amount);
}
printf("Estado de activo: %d\n", current->data->active);
current = current->next;
}
log_record(log_file_name, "Se imprimieron los autos en el garage");
return;
}
// * File Functions
FILE *open_file(char *file_name, char *file_mode)
{
FILE *file = fopen(file_name, file_mode);
if (file == NULL)
{
printf("\nError al abrir el archivo %s en el modo %s\n", file_name, file_mode);
exit(1);
}
return file;
}
List *retrieve_list(char *list_file_name)
{
FILE *file_handler = fopen(list_file_name, "rb");
if (file_handler == NULL)
{
return NULL;
}
List *list = (List *)calloc(1, sizeof(List));
int flag_var = 1;
while (flag_var)
{
Vehicle *vehicle_data = (Vehicle *)calloc(1, sizeof(Vehicle));
flag_var = fread(vehicle_data, sizeof(Vehicle), 1, file_handler);
if (flag_var)
{
insert_node(list, vehicle_data);
}
else
{
free(vehicle_data);
}
}
fclose(file_handler);
return list;
}
void save_list(List *list, char *list_file_name, int mode, int free_flag) // [mode] 0:save innactivated & 1:save all | [free_flag] 0:don't free | 1:free
{
if (list->actives == 0 && mode == 0)
{
return;
}
else if (list->size == 0 && mode == 1)
{
return;
}
FILE *file_handler = open_file(list_file_name, "wb");
struct Node *current = list->head;
struct Node *previous = NULL;
while (current != NULL)
{
if (current->data->active == 1 || mode == 0)
{
fwrite(current->data, sizeof(Vehicle), 1, file_handler);
}
previous = current;
current = current->next;
if (free_flag)
{
free(previous->data);
free(previous);
}
}
if (free_flag)
{
free(list);
}
fclose(file_handler);
}