-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.c
208 lines (167 loc) · 5.68 KB
/
book.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
// book.c
#include <stdio.h>
#include <string.h>
#include "book.h"
struct Book library[100];
int bookCount = 0;
void saveBook(struct Book book) {
FILE *file = fopen("books.txt", "a");
if (file != NULL) {
fprintf(file, "%s %s %d %d\n", book.title, book.author, book.isAvailable, book.userID);
fclose(file);
} else {
printf("Error: Unable to save book data.\n");
}
}
void loadBooks() {
FILE *file = fopen("books.txt", "r");
if (file != NULL) {
while (fscanf(file, "%s %s %d %d", library[bookCount].title, library[bookCount].author,
&library[bookCount].isAvailable, &library[bookCount].userID) != EOF) {
library[bookCount].bookID = bookCount;
bookCount++;
}
fclose(file);
}
}
// void addBook() {
// struct Book newBook;
// printf("Enter book title: ");
// scanf("%s", newBook.title);
// printf("Enter book author: ");
// scanf("%s", newBook.author);
// newBook.isAvailable = 1;
// newBook.userID = -1;
// newBook.bookID = bookCount;
// library[bookCount++] = newBook;
// saveBook(newBook);
// printf("Book added successfully!\n");
// }
void addBook() {
struct Book newBook;
int numCopies;
printf("Enter book title: ");
scanf("%s", newBook.title);
printf("Enter book author: ");
scanf("%s", newBook.author);
printf("Enter the number of copies: ");
scanf("%d", &numCopies);
for (int i = 0; i < numCopies; ++i) {
newBook.isAvailable = 1;
newBook.userID = -1;
newBook.bookID = bookCount; // Assign a unique book ID
library[bookCount++] = newBook;
saveBook(newBook);
}
printf("%d copies of the book added successfully!\n", numCopies);}
void deleteBook() {
int bookID;
printf("Enter book ID to delete: ");
scanf("%d", &bookID);
if (bookID >= 0 && bookID < bookCount) {
// Move all subsequent books one position back
for (int i = bookID; i < bookCount - 1; ++i) {
library[i] = library[i + 1];
}
bookCount--;
// Update the file with the modified book list
FILE *file = fopen("books.txt", "w");
if (file != NULL) {
for (int i = 0; i < bookCount; ++i) {
fprintf(file, "%s %s %d %d\n", library[i].title, library[i].author,
library[i].isAvailable, library[i].userID);
}
fclose(file);
} else {
printf("Error: Unable to update book data.\n");
}
printf("Book deleted successfully!\n");
} else {
printf("Invalid book ID.\n");
}
}
void issueBook() {
int bookID, userID;
printf("Enter book ID to issue: ");
scanf("%d", &bookID);
if (bookID >= 0 && bookID < bookCount) {
if (library[bookID].isAvailable) {
printf("Enter user ID: ");
scanf("%d", &userID);
library[bookID].isAvailable = 0;
library[bookID].userID = userID;
// Update the file with the modified book list
FILE *file = fopen("books.txt", "w");
if (file != NULL) {
for (int i = 0; i < bookCount; ++i) {
fprintf(file, "%s %s %d %d\n", library[i].title, library[i].author,
library[i].isAvailable, library[i].userID);
}
fclose(file);
} else {
printf("Error: Unable to update book data.\n");
}
printf("Book issued successfully!\n");
} else {
printf("Book is not available for issuing.\n");
}
} else {
printf("Invalid book ID.\n");
}
}
void returnBook() {
int bookID;
printf("Enter book ID to return: ");
scanf("%d", &bookID);
if (bookID >= 0 && bookID < bookCount) {
if (!library[bookID].isAvailable) {
library[bookID].isAvailable = 1;
library[bookID].userID = -1;
// Update the file with the modified book list
FILE *file = fopen("books.txt", "w");
if (file != NULL) {
for (int i = 0; i < bookCount; ++i) {
fprintf(file, "%s %s %d %d\n", library[i].title, library[i].author,
library[i].isAvailable, library[i].userID);
}
fclose(file);
} else {
printf("Error: Unable to update book data.\n");
}
printf("Book returned successfully!\n");
} else {
printf("Book is already available.\n");
}
} else {
printf("Invalid book ID.\n");
}
}
void displayBookAvailability() {
printf("Available Books:\n");
for (int i = 0; i < bookCount; ++i) {
if (library[i].isAvailable) {
printf("ID: %d, Title: %s, Author: %s\n", library[i].bookID, library[i].title, library[i].author);
}
}
}
void displayIssuedBooks() {
printf("Issued Books:\n");
for (int i = 0; i < bookCount; ++i) {
if (!library[i].isAvailable) {
printf("ID: %d, Title: %s, Author: %s, Issued to User ID: %d\n", library[i].bookID,
library[i].title, library[i].author, library[i].userID);
}
}
}
void searchBook() {
char searchTitle[100];
printf("Enter book title to search: ");
scanf("%s", searchTitle);
printf("Search Results:\n");
for (int i = 0; i < bookCount; ++i) {
if (strstr(library[i].title, searchTitle) != NULL) {
printf("ID: %d, Title: %s, Author: %s, Available: %s\n", library[i].bookID,
library[i].title, library[i].author, library[i].isAvailable ? "Yes" : "No");
}
}
}