-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
132 lines (125 loc) · 4.24 KB
/
main.cpp
File metadata and controls
132 lines (125 loc) · 4.24 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
// Function to search for a book by Title
void searchBookByTitle() {
string title;
cout << "Enter Book Title to search: ";
cin.ignore(); // To ignore any leftover newline characters
getline(cin, title);
for (const auto& book : books) {
if (book.title == title) {
cout << "ID: " << book.id << ", Title: " << book.title << ", Author: " << book.author << ", Status: "
<< (book.isIssued ? "Issued to " + book.issuedTo : "Available") << endl;
return;
}
}
cout << "Book not found." << endl;
}
// Function to issue a book to a student
void issueBook() {
int id;
string studentName;
cout << "Enter Book ID to issue: ";
cin >> id;
for (auto& book : books) {
if (book.id == id && !book.isIssued) {
cout << "Enter student name: ";
cin.ignore(); // To ignore any leftover newline characters
getline(cin, studentName);
book.isIssued = true; // Mark the book as issued
book.issuedTo = studentName; // Store the student's name
cout << "Book issued to " << studentName << endl;
return;
}
}
cout << "Book not available for issuing." << endl;
}
// Function to return a book
void returnBook() {
int id;
cout << "Enter Book ID to return: ";
cin >> id;
for (auto& book : books) {
if (book.id == id && book.isIssued) {
book.isIssued = false; // Mark the book as returned
book.issuedTo = ""; // Clear the student's name
returnQueue.push(id); // Add the book to the return queue
cout << "Book returned successfully!" << endl;
return;
}
}
cout << "Book not found or not issued." << endl;
}
// Function to list all books in the library
void listAllBooks() {
for (const auto& book : books) {
cout << "ID: " << book.id << ", Title: " << book.title
<< ", Author: " << book.author << ", Status: ";
if (book.isIssued) {
cout << "Issued to " << book.issuedTo << endl;
} else {
cout << "Available" << endl;
}
}
}
// Function to delete a book from the library
void deleteBook() {
int id;
cout << "Enter Book ID to delete: ";
cin >> id;
for (size_t i = 0; i < books.size(); ++i) {
if (books[i].id == id) {
books.erase(books.begin() + i); // Remove the book from vector
cout << "Book deleted successfully!" << endl;
return;
}
}
cout << "Book not found." << endl;
}
// Main function to run the Library Management System
int main() {
int choice;
do {
// Displaying the menu
cout << "Welcome to the Library Management System!" << endl;
cout << "1. Add Book" << endl;
cout << "2. Search Book by ID" << endl;
cout << "3. Search Book by Title" << endl;
cout << "4. Issue Book" << endl;
cout << "5. Return Book" << endl;
cout << "6. List All Books" << endl;
cout << "7. Delete Book" << endl;
cout << "8. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
// Handling user input for different functionalities
switch (choice) {
case 1:
addBook(); // Call the function to add a book
break;
case 2:
searchBookByID(); // Call the function to search by ID
break;
case 3:
searchBookByTitle(); // Call the function to search by Title
break;
case 4:
issueBook(); // Call the function to issue a book
break;
case 5:
returnBook(); // Call the function to return a book
break;
case 6:
listAllBooks(); // Call the function to list all books
break;
case 7:
deleteBook(); // Call the function to delete a book
break;
case 8:
cout << "Goodbye!" << endl; // Exit the program
break;
default:
cout << "Invalid choice. Please try again." << endl; // Invalid input handling
}
cout << endl;
} while (choice != 8); // Loop until the user selects the Exit option
return 0;
}