-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibrary.c
More file actions
275 lines (227 loc) · 7.22 KB
/
Copy pathlibrary.c
File metadata and controls
275 lines (227 loc) · 7.22 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
#define MAX_MEMBERS 100
// Structures
typedef struct {
int id;
char title[50];
char author[50];
int quantity;
int available;
} Book;
typedef struct {
int id;
char name[50];
char phone[15];
} Member;
typedef struct {
int bookId;
int memberId;
char issueDate[11];
char returnDate[11];
int isReturned;
} Transaction;
// Global variables
Book books[MAX_BOOKS];
Member members[MAX_MEMBERS];
Transaction transactions[MAX_BOOKS];
int bookCount = 0, memberCount = 0, transactionCount = 0;
// Function prototypes
void addBook();
void displayBooks();
void addMember();
void displayMembers();
void issueBook();
void returnBook();
void saveDataToFile();
void loadDataFromFile();
int findBookById(int id);
int findMemberById(int id);
// Main menu
int main() {
int choice;
loadDataFromFile(); // Load data from file at the start
do {
printf("\nLibrary Management System\n");
printf("1. Add Book\n");
printf("2. Display All Books\n");
printf("3. Add Member\n");
printf("4. Display All Members\n");
printf("5. Issue Book\n");
printf("6. Return Book\n");
printf("7. Save and Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: addBook(); break;
case 2: displayBooks(); break;
case 3: addMember(); break;
case 4: displayMembers(); break;
case 5: issueBook(); break;
case 6: returnBook(); break;
case 7: saveDataToFile(); printf("Data saved. Exiting...\n"); break;
default: printf("Invalid choice! Try again.\n");
}
} while (choice != 7);
return 0;
}
// Function definitions
void addBook() {
if (bookCount >= MAX_BOOKS) {
printf("Library is full! Cannot add more books.\n");
return;
}
printf("Enter book ID: ");
scanf("%d", &books[bookCount].id);
printf("Enter book title: ");
getchar(); // Consume newline character
fgets(books[bookCount].title, 50, stdin);
books[bookCount].title[strcspn(books[bookCount].title, "\n")] = '\0'; // Remove newline
printf("Enter author name: ");
fgets(books[bookCount].author, 50, stdin);
books[bookCount].author[strcspn(books[bookCount].author, "\n")] = '\0';
printf("Enter quantity: ");
scanf("%d", &books[bookCount].quantity);
books[bookCount].available = books[bookCount].quantity;
bookCount++;
printf("Book added successfully!\n");
}
void displayBooks() {
if (bookCount == 0) {
printf("No books available in the library.\n");
return;
}
printf("\nBooks in the Library:\n");
printf("ID\tTitle\t\tAuthor\t\tQuantity\tAvailable\n");
for (int i = 0; i < bookCount; i++) {
printf("%d\t%s\t\t%s\t\t%d\t\t%d\n", books[i].id, books[i].title, books[i].author, books[i].quantity, books[i].available);
}
}
void addMember() {
if (memberCount >= MAX_MEMBERS) {
printf("Cannot add more members.\n");
return;
}
printf("Enter member ID: ");
scanf("%d", &members[memberCount].id);
printf("Enter member name: ");
getchar(); // Consume newline character
fgets(members[memberCount].name, 50, stdin);
members[memberCount].name[strcspn(members[memberCount].name, "\n")] = '\0';
printf("Enter member phone: ");
fgets(members[memberCount].phone, 15, stdin);
members[memberCount].phone[strcspn(members[memberCount].phone, "\n")] = '\0';
memberCount++;
printf("Member added successfully!\n");
}
void displayMembers() {
if (memberCount == 0) {
printf("No members found.\n");
return;
}
printf("\nMembers:\n");
printf("ID\tName\t\tPhone\n");
for (int i = 0; i < memberCount; i++) {
printf("%d\t%s\t\t%s\n", members[i].id, members[i].name, members[i].phone);
}
}
void issueBook() {
int bookId, memberId;
printf("Enter book ID to issue: ");
scanf("%d", &bookId);
int bookIndex = findBookById(bookId);
if (bookIndex == -1) {
printf("Book not found.\n");
return;
}
if (books[bookIndex].available == 0) {
printf("No copies of the book are available.\n");
return;
}
printf("Enter member ID: ");
scanf("%d", &memberId);
int memberIndex = findMemberById(memberId);
if (memberIndex == -1) {
printf("Member not found.\n");
return;
}
printf("Enter issue date (DD/MM/YYYY): ");
getchar();
fgets(transactions[transactionCount].issueDate, 11, stdin);
transactions[transactionCount].issueDate[strcspn(transactions[transactionCount].issueDate, "\n")] = '\0';
transactions[transactionCount].bookId = bookId;
transactions[transactionCount].memberId = memberId;
transactions[transactionCount].isReturned = 0;
books[bookIndex].available--;
transactionCount++;
printf("Book issued successfully!\n");
}
void returnBook() {
int bookId, memberId;
printf("Enter book ID to return: ");
scanf("%d", &bookId);
int bookIndex = findBookById(bookId);
if (bookIndex == -1) {
printf("Book not found.\n");
return;
}
printf("Enter member ID: ");
scanf("%d", &memberId);
int memberIndex = findMemberById(memberId);
if (memberIndex == -1) {
printf("Member not found.\n");
return;
}
for (int i = 0; i < transactionCount; i++) {
if (transactions[i].bookId == bookId && transactions[i].memberId == memberId && transactions[i].isReturned == 0) {
printf("Enter return date (DD/MM/YYYY): ");
getchar();
fgets(transactions[i].returnDate, 11, stdin);
transactions[i].returnDate[strcspn(transactions[i].returnDate, "\n")] = '\0';
transactions[i].isReturned = 1;
books[bookIndex].available++;
printf("Book returned successfully!\n");
return;
}
}
printf("No record found for this book and member.\n");
}
int findBookById(int id) {
for (int i = 0; i < bookCount; i++) {
if (books[i].id == id) return i;
}
return -1;
}
int findMemberById(int id) {
for (int i = 0; i < memberCount; i++) {
if (members[i].id == id) return i;
}
return -1;
}
void saveDataToFile() {
FILE *file = fopen("library_data.bin", "wb");
if (file == NULL) {
printf("Error saving data.\n");
return;
}
fwrite(&bookCount, sizeof(int), 1, file);
fwrite(books, sizeof(Book), bookCount, file);
fwrite(&memberCount, sizeof(int), 1, file);
fwrite(members, sizeof(Member), memberCount, file);
fwrite(&transactionCount, sizeof(int), 1, file);
fwrite(transactions, sizeof(Transaction), transactionCount, file);
fclose(file);
}
void loadDataFromFile() {
FILE *file = fopen("library_data.bin", "rb");
if (file == NULL) return; // No file to load
fread(&bookCount, sizeof(int), 1, file);
fread(books, sizeof(Book), bookCount, file);
fread(&memberCount, sizeof(int), 1, file);
fread(members, sizeof(Member), memberCount, file);
fread(&transactionCount, sizeof(int), 1, file);
fread(transactions, sizeof(Transaction), transactionCount, file);
fclose(file);
}