-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
499 lines (425 loc) · 14.6 KB
/
main.cpp
File metadata and controls
499 lines (425 loc) · 14.6 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
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
#include <iostream>
#include <mysql.h>
#include <mysqld_error.h>
#include <sstream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <unistd.h>
#include <ctime>
using namespace std;
const char* HOST = "mysql-db";
const char* USER = "root";
const char* PW = "root";
const char* DB = "library-db";
// Utility functions
void clearScreen() {
system("clear");
}
void pauseScreen() {
cout << "\nPress Enter to continue...";
cin.ignore();
cin.get();
}
// Classes
class Student {
private:
string Id;
string Name;
string Contact;
public:
Student() : Id(""), Name(""), Contact("") {}
void setId(string id) { Id = id; }
void setName(string name) { Name = name; }
void setContact(string contact) { Contact = contact; }
string getId() const { return Id; }
string getName() const { return Name; }
string getContact() const { return Contact; }
};
class Book {
private:
string Name;
string Author;
int Quantity;
string Category;
public:
Book() : Name(""), Author(""), Quantity(0), Category("") {}
void setName(string name) { Name = name; }
void setAuthor(string author) { Author = author; }
void setQuantity(int quantity) { Quantity = quantity; }
void setCategory(string category) { Category = category; }
string getName() const { return Name; }
string getAuthor() const { return Author; }
int getQuantity() const { return Quantity; }
string getCategory() const { return Category; }
};
class Transaction {
private:
string studentId;
string bookName;
string issueDate;
string returnDate;
bool returned;
public:
Transaction(string sid, string bname, string idate, string rdate, bool ret = false)
: studentId(sid), bookName(bname), issueDate(idate), returnDate(rdate), returned(ret) {}
string getStudentId() const { return studentId; }
string getBookName() const { return bookName; }
string getIssueDate() const { return issueDate; }
string getReturnDate() const { return returnDate; }
bool isReturned() const { return returned; }
void markReturned() { returned = true; }
};
// Database functions
bool executeQuery(MYSQL* conn, const string& query) {
if (mysql_query(conn, query.c_str())) {
cout << "Error: " << mysql_error(conn) << endl;
return false;
}
return true;
}
// Admin functions
void addBook(MYSQL* conn) {
Book b;
string name, author, category;
int quantity;
clearScreen();
cout << "=== ADD NEW BOOK ===" << endl;
cout << "Book Name: ";
cin.ignore();
getline(cin, name);
b.setName(name);
cout << "Author: ";
getline(cin, author);
b.setAuthor(author);
cout << "Category: ";
getline(cin, category);
b.setCategory(category);
cout << "Quantity: ";
cin >> quantity;
b.setQuantity(quantity);
stringstream ss;
ss << quantity;
string Sq = ss.str();
string query = "INSERT INTO lib (Name, Author, Category, Quantity) VALUES('" +
b.getName() + "', '" + b.getAuthor() + "', '" + b.getCategory() + "', '" + Sq + "')";
if (executeQuery(conn, query)) {
cout << "\nBook Added Successfully!" << endl;
}
pauseScreen();
}
void addStudent(MYSQL* conn) {
Student s;
string id, name, contact;
clearScreen();
cout << "=== ADD NEW STUDENT ===" << endl;
cout << "Student ID: ";
cin >> id;
s.setId(id);
cout << "Student Name: ";
cin.ignore();
getline(cin, name);
s.setName(name);
cout << "Contact Info: ";
getline(cin, contact);
s.setContact(contact);
string query = "INSERT INTO student (Id, Name, Contact) VALUES('" +
s.getId() + "', '" + s.getName() + "', '" + s.getContact() + "')";
if (executeQuery(conn, query)) {
cout << "\nStudent Registered Successfully!" << endl;
}
pauseScreen();
}
void viewAllBooks(MYSQL* conn) {
clearScreen();
cout << "=== AVAILABLE BOOKS ===" << endl;
cout << setw(20) << left << "Book Name"
<< setw(20) << "Author"
<< setw(15) << "Category"
<< setw(10) << "Quantity" << endl;
cout << string(65, '-') << endl;
string query = "SELECT * FROM lib";
if (executeQuery(conn, query)) {
MYSQL_RES* res = mysql_store_result(conn);
if (res) {
MYSQL_ROW row;
while ((row = mysql_fetch_row(res))) {
cout << setw(20) << left << row[0] // Name
<< setw(20) << row[1] // Author
<< setw(15) << row[2] // Category
<< setw(10) << row[3] << endl; // Quantity
}
mysql_free_result(res);
}
}
pauseScreen();
}
void viewAllStudents(MYSQL* conn) {
clearScreen();
cout << "=== REGISTERED STUDENTS ===" << endl;
cout << setw(15) << left << "Student ID"
<< setw(25) << "Name"
<< setw(20) << "Contact" << endl;
cout << string(60, '-') << endl;
string query = "SELECT * FROM student";
if (executeQuery(conn, query)) {
MYSQL_RES* res = mysql_store_result(conn);
if (res) {
MYSQL_ROW row;
while ((row = mysql_fetch_row(res))) {
cout << setw(15) << left << row[0] // ID
<< setw(25) << row[1] // Name
<< setw(20) << row[2] << endl; // Contact
}
mysql_free_result(res);
}
}
pauseScreen();
}
void viewBorrowedBooks(MYSQL* conn) {
clearScreen();
cout << "=== BORROWED BOOKS ===" << endl;
cout << setw(15) << left << "Student ID"
<< setw(20) << "Book Name"
<< setw(15) << "Issue Date"
<< setw(15) << "Return Date"
<< setw(10) << "Status" << endl;
cout << string(75, '-') << endl;
string query = "SELECT * FROM transactions";
if (executeQuery(conn, query)) {
MYSQL_RES* res = mysql_store_result(conn);
if (res) {
MYSQL_ROW row;
while ((row = mysql_fetch_row(res))) {
cout << setw(15) << left << row[0] // Student ID
<< setw(20) << row[1] // Book Name
<< setw(15) << row[2] // Issue Date
<< setw(15) << row[3] // Return Date
<< setw(10) << (atoi(row[4]) ? "Returned" : "Borrowed") << endl;
}
mysql_free_result(res);
}
}
pauseScreen();
}
void adminMenu(MYSQL* conn) {
bool exit = false;
while (!exit) {
clearScreen();
cout << "=== ADMINISTRATION MENU ===" << endl;
cout << "1. Add New Book" << endl;
cout << "2. View All Books" << endl;
cout << "3. Register New Student" << endl;
cout << "4. View All Students" << endl;
cout << "5. View Borrowed Books" << endl;
cout << "0. Back to Main Menu" << endl;
cout << "Enter Choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1: addBook(conn); break;
case 2: viewAllBooks(conn); break;
case 3: addStudent(conn); break;
case 4: viewAllStudents(conn); break;
case 5: viewBorrowedBooks(conn); break;
case 0: exit = true; break;
default: cout << "Invalid choice!" << endl; pauseScreen();
}
}
}
// User functions
bool studentExists(MYSQL* conn, const string& studentId) {
string query = "SELECT * FROM student WHERE Id = '" + studentId + "'";
if (executeQuery(conn, query)) {
MYSQL_RES* res = mysql_store_result(conn);
if (res) {
bool exists = mysql_num_rows(res) > 0;
mysql_free_result(res);
return exists;
}
}
return false;
}
bool bookExists(MYSQL* conn, const string& bookName) {
string query = "SELECT * FROM lib WHERE Name = '" + bookName + "'";
if (executeQuery(conn, query)) {
MYSQL_RES* res = mysql_store_result(conn);
if (res) {
bool exists = mysql_num_rows(res) > 0;
mysql_free_result(res);
return exists;
}
}
return false;
}
int getBookQuantity(MYSQL* conn, const string& bookName) {
string query = "SELECT Quantity FROM lib WHERE Name = '" + bookName + "'";
if (executeQuery(conn, query)) {
MYSQL_RES* res = mysql_store_result(conn);
if (res) {
MYSQL_ROW row = mysql_fetch_row(res);
int quantity = row ? atoi(row[0]) : 0;
mysql_free_result(res);
return quantity;
}
}
return 0;
}
void borrowBook(MYSQL* conn, const string& studentId) {
clearScreen();
viewAllBooks(conn);
string bookName;
cout << "\nEnter Book Name to Borrow: ";
cin.ignore();
getline(cin, bookName);
if (!bookExists(conn, bookName)) {
cout << "Book not found in the library!" << endl;
pauseScreen();
return;
}
int quantity = getBookQuantity(conn, bookName);
if (quantity <= 0) {
cout << "This book is currently out of stock!" << endl;
pauseScreen();
return;
}
// Get current date (Linux version)
time_t now = time(0);
tm* ltm = localtime(&now);
char issueDate[20], returnDate[20];
strftime(issueDate, sizeof(issueDate), "%Y-%m-%d", ltm);
// Calculate return date (1 month later)
ltm->tm_mon += 1;
mktime(ltm); // Normalize the time structure
strftime(returnDate, sizeof(returnDate), "%Y-%m-%d", ltm);
// Record transaction
string transaction = "INSERT INTO transactions (StudentId, BookName, IssueDate, ReturnDate, Returned) VALUES('" +
studentId + "', '" + bookName + "', '" + issueDate + "', '" + returnDate + "', '0')";
// Update book quantity
string updateBook = "UPDATE lib SET Quantity = Quantity - 1 WHERE Name = '" + bookName + "'";
if (executeQuery(conn, transaction) && executeQuery(conn, updateBook)) {
cout << "\nBook borrowed successfully! Please return by " << returnDate << endl;
}
pauseScreen();
}
void returnBook(MYSQL* conn, const string& studentId) {
clearScreen();
cout << "=== YOUR BORROWED BOOKS ===" << endl;
string query = "SELECT BookName, IssueDate, ReturnDate FROM transactions WHERE StudentId = '" +
studentId + "' AND Returned = '0'";
vector<string> borrowedBooks;
if (executeQuery(conn, query)) {
MYSQL_RES* res = mysql_store_result(conn);
if (res) {
MYSQL_ROW row;
int count = 0;
while ((row = mysql_fetch_row(res))) {
cout << ++count << ". " << row[0] << " (Issued: " << row[1] << ", Due: " << row[2] << ")" << endl;
borrowedBooks.push_back(row[0]);
}
mysql_free_result(res);
}
}
if (borrowedBooks.empty()) {
cout << "You have no books to return." << endl;
pauseScreen();
return;
}
cout << "\nEnter the number of the book to return: ";
int choice;
cin >> choice;
if (choice < 1 || choice > borrowedBooks.size()) {
cout << "Invalid choice!" << endl;
pauseScreen();
return;
}
string bookName = borrowedBooks[choice - 1];
// Update transaction
string updateTrans = "UPDATE transactions SET Returned = '1' WHERE StudentId = '" +
studentId + "' AND BookName = '" + bookName + "' AND Returned = '0'";
// Update book quantity
string updateBook = "UPDATE lib SET Quantity = Quantity + 1 WHERE Name = '" + bookName + "'";
if (executeQuery(conn, updateTrans) && executeQuery(conn, updateBook)) {
cout << "\nBook returned successfully!" << endl;
}
pauseScreen();
}
void userMenu(MYSQL* conn) {
clearScreen();
string studentId;
cout << "Enter Your Student ID: ";
cin >> studentId;
if (!studentExists(conn, studentId)) {
cout << "Student ID not found. Please register first." << endl;
pauseScreen();
return;
}
bool exit = false;
while (!exit) {
clearScreen();
cout << "=== USER MENU ===" << endl;
cout << "1. View Available Books" << endl;
cout << "2. Borrow a Book" << endl;
cout << "3. Return a Book" << endl;
cout << "0. Back to Main Menu" << endl;
cout << "Enter Choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1: viewAllBooks(conn); break;
case 2: borrowBook(conn, studentId); break;
case 3: returnBook(conn, studentId); break;
case 0: exit = true; break;
default: cout << "Invalid choice!" << endl; pauseScreen();
}
}
}
// Main function
int main() {
MYSQL* conn = mysql_init(NULL);
if (!mysql_real_connect(conn, HOST, USER, PW, DB, 3306, NULL, 0)) {
cout << "Error: " << mysql_error(conn) << endl;
return 1;
}
// Create tables if they don't exist
string createLibTable = "CREATE TABLE IF NOT EXISTS lib ("
"Name VARCHAR(100) PRIMARY KEY, "
"Author VARCHAR(100), "
"Category VARCHAR(50), "
"Quantity INT)";
string createStudentTable = "CREATE TABLE IF NOT EXISTS student ("
"Id VARCHAR(20) PRIMARY KEY, "
"Name VARCHAR(100), "
"Contact VARCHAR(50))";
string createTransTable = "CREATE TABLE IF NOT EXISTS transactions ("
"StudentId VARCHAR(20), "
"BookName VARCHAR(100), "
"IssueDate DATE, "
"ReturnDate DATE, "
"Returned BOOLEAN, "
"FOREIGN KEY (StudentId) REFERENCES student(Id), "
"FOREIGN KEY (BookName) REFERENCES lib(Name))";
executeQuery(conn, createLibTable);
executeQuery(conn, createStudentTable);
executeQuery(conn, createTransTable);
bool exit = false;
while (!exit) {
clearScreen();
cout << "=== LIBRARY MANAGEMENT SYSTEM ===" << endl;
cout << "1. Administration" << endl;
cout << "2. User" << endl;
cout << "0. Exit" << endl;
cout << "Enter Choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1: adminMenu(conn); break;
case 2: userMenu(conn); break;
case 0: exit = true; break;
default: cout << "Invalid choice!" << endl; pauseScreen();
}
}
mysql_close(conn);
cout << "Goodbye!" << endl;
return 0;
}