-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.c
More file actions
executable file
·382 lines (307 loc) · 11.1 KB
/
Copy pathconsole.c
File metadata and controls
executable file
·382 lines (307 loc) · 11.1 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
void add_member(MYSQL *conn){
char name[100], address[200], phone[20], email[100];
char query[1000];
printf("Enter member name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = 0;
printf("Enter address: ");
fgets(address, sizeof(address), stdin);
address[strcspn(address, "\n")] = 0;
printf("Enter phone number: ");
fgets(phone, sizeof(phone), stdin);
phone[strcspn(phone, "\n")] = 0;
printf("Enter email: ");
fgets(email, sizeof(email), stdin);
email[strcspn(email, "\n")] = 0;
sprintf(query, "INSERT INTO Members (Name, Address, Phone, Email) VALUES ('%s', '%s', '%s', '%s')", name, address, phone, email);
if(mysql_query(conn, query)){
fprintf(stderr, "%s\n", mysql_error(conn));
} else {
printf("Member added successfully!\n");
}
}
void add_book(MYSQL *conn){
char title[100], author[100], publisher[100], year[10], isbn[20], genre[50];
char query[1000];
printf("Enter book title: ");
fgets(title, sizeof(title), stdin);
title[strcspn(title, "\n")] = 0;
printf("Enter author_id: ");
fgets(author, sizeof(author), stdin);
author[strcspn(author, "\n")] = 0;
printf("Enter publisher: ");
fgets(publisher, sizeof(publisher), stdin);
publisher[strcspn(publisher, "\n")] = 0;
printf("Enter year of publication (year only): ");
fgets(year, sizeof(year), stdin);
year[strcspn(year, "\n")] = 0;
printf("Enter ISBN: ");
fgets(isbn, sizeof(isbn), stdin);
isbn[strcspn(isbn, "\n")] = 0;
printf("Enter genre: ");
fgets(genre, sizeof(genre), stdin);
genre[strcspn(genre, "\n")] = 0;
sprintf(query, "INSERT INTO Books (Title, author_id, publisher_id, year_published, ISBN, Genre) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')", title, author, publisher, year, isbn, genre);
if(mysql_query(conn, query)){
fprintf(stderr, "%s\n", mysql_error(conn));
} else {
printf("Book added successfully!\n");
}
}
void add_staff(MYSQL *conn){
char name[100], position[100], phone[20], email[100];
char query[1000];
printf("Enter staff name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = 0;
printf("Enter position: ");
fgets(position, sizeof(position), stdin);
position[strcspn(position, "\n")] = 0;
printf("Enter phone number: ");
fgets(phone, sizeof(phone), stdin);
phone[strcspn(phone, "\n")] = 0;
printf("Enter email: ");
fgets(email, sizeof(email), stdin);
email[strcspn(email, "\n")] = 0;
sprintf(query, "INSERT INTO Staff (Name, role, Phone, Email) VALUES ('%s', '%s', '%s', '%s')", name, position, phone, email);
if(mysql_query(conn, query)){
fprintf(stderr, "%s\n", mysql_error(conn));
} else {
printf("Staff added successfully!\n");
}
}
void add_author(MYSQL *conn){
char name[100], bio[100],query[1000];
printf("Enter author name ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = 0;
printf("Enter bio");
fgets(bio, sizeof(bio), stdin);
bio[strcspn(bio, "\n")] = 0;
sprintf(query, "INSERT INTO Authors (name,bio) VALUES ( '%s', '%s')",name, bio);
if(mysql_query(conn, query)){
fprintf(stderr, "%s\n", mysql_error(conn));
} else {
printf("Author added successfully!\n");
}
}
void add_publisher(MYSQL *conn){
char name[100], address[100], contact[100],query[1000];
printf("Enter publisher name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = 0;
printf("Enter address: ");
fgets(address, sizeof(address), stdin);
address[strcspn(address, "\n")] = 0;
printf("Enter contact info: ");
fgets(contact, sizeof(contact), stdin);
contact[strcspn(contact, "\n")] = 0;
sprintf(query, "INSERT INTO Publishers (name,address, contact_info) VALUES ('%s', '%s', '%s')", name,address,contact);
if(mysql_query(conn, query)){
fprintf(stderr, "%s\n", mysql_error(conn));
} else {
printf("Publisher added successfully!\n");
}
}
void add_fine(MYSQL *conn){
char member_id[10], amount[10], reason[200];
char query[1000];
printf("Enter Member ID: ");
fgets(member_id, sizeof(member_id), stdin);
member_id[strcspn(member_id, "\n")] = 0;
printf("Enter amount: ");
fgets(amount, sizeof(amount), stdin);
amount[strcspn(amount, "\n")] = 0;
printf("Enter date(YYYY-MM-DD): ");
fgets(reason, sizeof(reason), stdin);
reason[strcspn(reason, "\n")] = 0;
sprintf(query, "INSERT INTO Fines (member_id, Amount,date_paid) VALUES ('%s', '%s', '%s')", member_id, amount, reason);
if(mysql_query(conn, query)){
fprintf(stderr, "%s\n", mysql_error(conn));
} else {
printf("Fine added successfully!\n");
}
}
void add_borrowing(MYSQL *conn){
char book_id[100], member_id[100], borrow_date[100], due_date[100], return_date[100], staff_id[100],query[1000];
MYSQL_RES *res;
MYSQL_ROW row;
printf("Enter book_id:");
fgets(book_id,sizeof(book_id),stdin);
book_id[strcspn(book_id,"\n")]=0;
printf("Enter member Id: ");
fgets(member_id,sizeof(member_id),stdin);
member_id[strcspn(member_id,"\n")]=0;
printf("Enter Date (YYYY-MM-DD): ");
fgets(borrow_date,sizeof(borrow_date),stdin);
borrow_date[strcspn(borrow_date,"\n")]=0;
printf("Enter due_date (YYYY-MM-DD):");
fgets(due_date,sizeof(due_date),stdin);
due_date[strcspn(due_date,"\n")]=0;
printf("Enter return date (YYYY-MM-DD): ");
fgets(return_date,sizeof(return_date),stdin);
return_date[strcspn(return_date,"\n")]=0;
printf("Enter your staff Id: ");
fgets(staff_id,sizeof(staff_id),stdin);
staff_id[strcspn(staff_id,"\n")]=0;
sprintf(query, "INSERT INTO Borrowings (book_id, member_id,borrow_date, due_date, return_date, staff_id) VALUES ('%s', '%s', '%s','%s', '%s', '%s')", book_id, member_id,borrow_date, due_date, return_date, staff_id);
if(mysql_query(conn, query)){
fprintf(stderr, "%s\n", mysql_error(conn));
} else {
printf("Borrowed book recorded successfully!\n");
}
}
void list_books(MYSQL *conn){
MYSQL_RES *res;
MYSQL_ROW row;
if(mysql_query(conn, "SELECT * FROM Books")){
fprintf(stderr, "%s\n", mysql_error(conn));
return;
}
res = mysql_store_result(conn);
printf("Here's what we've got\n");
while((row = mysql_fetch_row(res)) != NULL)
printf("ID %s, Title: %s, Genre: %s\n", row[0], row[1], row[5]);
mysql_free_result(res);
}
void list_members(MYSQL *conn){
MYSQL_RES *res;
MYSQL_ROW row;
if(mysql_query(conn, "SELECT * FROM Members")){
fprintf(stderr, "%s\n", mysql_error(conn));
return;
}
res = mysql_store_result(conn);
printf("Member List\n");
while((row = mysql_fetch_row(res)) != NULL){
printf("ID %s, Name: %s, Phone: %s, Email: %s\n", row[0], row[1], row[3], row[4]);
}
mysql_free_result(res);
}
void list_staff(MYSQL *conn){
MYSQL_RES *res;
MYSQL_ROW row;
if(mysql_query(conn, "SELECT * FROM Staff")){
fprintf(stderr, "%s\n", mysql_error(conn));
return;
}
res = mysql_store_result(conn);
printf("Staff List\n");
while((row = mysql_fetch_row(res)) != NULL){
printf("ID %s, Name: %s, Position: %s, Email: %s\n", row[0], row[1], row[2], row[4]);
}
mysql_free_result(res);
}
void list_fines(MYSQL *conn){
MYSQL_RES *res;
MYSQL_ROW row;
if(mysql_query(conn, "SELECT * FROM Fines")){
fprintf(stderr, "%s\n", mysql_error(conn));
return;
}
res = mysql_store_result(conn);
printf("Fines List\n");
while((row = mysql_fetch_row(res)) != NULL){
printf("Fine ID %s, Member ID: %s, Amount: %s\n", row[0], row[1], row[2]);
}
mysql_free_result(res);
}
void list_authors(MYSQL *conn){
MYSQL_RES *res;
MYSQL_ROW row;
if(mysql_query(conn, "SELECT * FROM Authors")){
fprintf(stderr, "%s\n", mysql_error(conn));
return;
}
res = mysql_store_result(conn);
printf("Fines List\n");
while((row = mysql_fetch_row(res)) != NULL){
printf("Author ID: %s, name: %s, bio: %s\n", row[0], row[1], row[2]);
}
mysql_free_result(res);
}
void list_publisher(MYSQL *conn){
MYSQL_RES *res;
MYSQL_ROW row;
if(mysql_query(conn, "SELECT * FROM Publishers ")){
fprintf(stderr, "%s\n", mysql_error(conn));
return;
}
res = mysql_store_result(conn);
printf("Publishers List\n");
while((row = mysql_fetch_row(res)) != NULL){
printf("Author ID: %s, name: %s, bio: %s\n", row[0], row[1], row[2]);
}
mysql_free_result(res);
}
void list_borrowings(MYSQL *conn){
MYSQL_RES *res;
MYSQL_ROW row;
if(mysql_query(conn, "SELECT * FROM Borrowings")){
fprintf(stderr,"%s\n",mysql_error(conn));
return;
}
res=mysql_store_result(conn);
printf("Borrowings list\n");
while((row=mysql_fetch_row(res))!=NULL){
printf("Num: %s Book number: %s Member number: %s Date Borrowed: %s Due date: %s, Return_date: %s Staff number: %s\n",row[0],row[1],row[2],row[3],row[4],row[5],row[6]);
}
mysql_free_result(res);
}
int main(){
MYSQL *conn = mysql_init(NULL);
char choice[10];
if(conn == NULL){
fprintf(stderr, "mysql_init() failed\n");
return EXIT_FAILURE;
}
if(mysql_real_connect(conn, "localhost", "root", "", "library", 0, NULL, 0) == NULL){
fprintf(stderr, "%s\n", mysql_error(conn));
mysql_close(conn);
return EXIT_FAILURE;
}
while(1){
printf("\nChoose an option:\n");
printf("1. Add Member\n");
printf("2. Add Book\n");
printf("3. Add Staff\n");
printf("4. Add Fine\n");
printf("5. List Books\n");
printf("6. List Members\n");
printf("7. List Staff\n");
printf("8. List fines\n");
printf("9. Add author\n");
printf("10.List author\n");
printf("11.Add publisher\n");
printf("12.List publishers\n");
printf("13.Add Borrowings\n");
printf("14.List Borrowings\n");
printf("0. Exit\n");
printf("Enter choice: ");
fgets(choice, sizeof(choice), stdin);
switch(atoi(choice)){
case 1: add_member(conn); break;
case 2: add_book(conn); break;
case 3: add_staff(conn); break;
case 4: add_fine(conn); break;
case 5: list_books(conn); break;
case 6: list_members(conn); break;
case 7: list_staff(conn); break;
case 8: list_fines(conn); break;
case 9: add_author(conn); break;
case 10: list_authors(conn);break;
case 11: add_publisher(conn);break;
case 12: list_publisher(conn);break;
case 13: add_borrowing(conn);break;
case 14: list_borrowings(conn);break;
case 0: mysql_close(conn); exit(0);
default: printf("Invalid choice. Try again.\n"); break;
}
}
mysql_close(conn);
return EXIT_SUCCESS;
}