-
Notifications
You must be signed in to change notification settings - Fork 0
/
BookList.cpp
137 lines (99 loc) · 2.28 KB
/
BookList.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "BookList.h"
typedef struct e {
Book *b;
struct e *prox;
} Node;
struct bl {
int index;
Node *head;
};
BookList* bl_open() {
BookList *bl = (BookList*) malloc(sizeof(BookList));
if (bl == NULL) {
printf("Erro -- Unable to allocate memory");
return NULL;
}
bl->head = NULL;
bl->index = 0;
return bl;
}
void bl_close(BookList *bl) {
if (bl != NULL) {
Node *node_aux;
while (bl->head == NULL) {
node_aux = bl->head;
bl->head = bl->head->prox;
free(node_aux);
}
free(bl);
}
}
int addBook(BookList *bl, Book *b) {
if (bl == NULL) return 0;
Node *node_aux = bl->head;
Node *node = (Node*) malloc(sizeof(Node));
node->b = b;
node->prox = NULL;
if (node_aux == NULL) {
node_aux = node;
bl->head = node_aux;
} else {
while (node_aux->prox != NULL) {
if (strcmp(getBookTitle(node_aux->b),getBookTitle(b)) == 0) {
printf("Esse livro ja foi cadastrado\n");
return 0;
}
node_aux = node_aux->prox;
}
node_aux->prox = node;
}
bl->index++;
return 1;
}
int removeBook(BookList *bl, char *book_title, ReservationList *rl) {
if (bl == NULL || bl->head == NULL) return 0;
Node *node_ant, *node_aux = bl->head;
while (node_aux != NULL && strcmp(getBookTitle(node_aux->b), book_title) != 0) {
node_ant = node_aux;
node_aux = node_aux->prox;
}
if (node_aux == NULL)
return 0;
if (node_aux == bl->head)
bl->head = node_aux->prox;
else
node_ant->prox = node_aux->prox;
Reservation *r = getReservation(rl, node_aux->b);
if (r != NULL) {
Client *c = getReservationClient(getReservation(rl, node_aux->b));
ClientBookList *cbl = getClientBookList(c);
int i = 1;
while ( node_aux->b != getBookClientBookList(cbl, i) )
i++;
removeReservation(rl, c, i);
}
free(node_aux->b);
free(node_aux);
bl->index--;
return 1;
}
Book* getBook(BookList *bl, char *book_title) {
Node *node = bl->head;
while (node != NULL && strcmp(book_title, getBookTitle(node->b)) != 0)
node = node->prox;
return (node == NULL) ? NULL : node->b;
}
void printAllBooks(BookList *bl) {
Node *node_aux = bl->head;
int i = 1;
while (node_aux != NULL) {
printf("#%d\n", i);
printBook(node_aux->b);
printf("\n");
node_aux = node_aux->prox;
i++;
}
}