-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
219 lines (205 loc) · 6.41 KB
/
Copy pathcode
File metadata and controls
219 lines (205 loc) · 6.41 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
#include <iostream>
#include <string>
using namespace std;
const int MAX_BOOKS = 100;
struct Book {
int id;
string title;
string author;
bool available;
string borrower;
};
Book library[MAX_BOOKS];
int numBooks = 0;
void addBook();
void searchBook();
void issueBook();
void returnBook();
void listBooks();
void deleteBook();
void sortBooksByID();
int main() {
int choice;
do {
cout << "\n===== Library Management System Menu =====\n";
cout << "1. Add a new book\n";
cout << "2. Search for a book\n";
cout << "3. Issue a book\n";
cout << "4. Return a book\n";
cout << "5. List all books\n";
cout << "6. Delete a book\n";
cout << "7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: addBook(); break;
case 2: searchBook(); break;
case 3: issueBook(); break;
case 4: returnBook(); break;
case 5: listBooks(); break;
case 6: deleteBook(); break;
case 7: cout << "Exiting program.\n"; break;
default: cout << "Invalid choice. Please enter a number between 1 and 7.\n"; break;
}
} while (choice != 7);
return 0;
}
void addBook() {
if (numBooks < MAX_BOOKS) {
cout << "\nEnter book details:\n";
cout << "ID: ";
cin >> library[numBooks].id;
cout << "Title: ";
cin.ignore();
getline(cin, library[numBooks].title);
cout << "Author: ";
getline(cin, library[numBooks].author);
library[numBooks].available = true;
numBooks++;
sortBooksByID();
cout << "Book added successfully!\n";
} else {
cout << "Cannot add more books. Library is full.\n";
}
}
void sortBooksByID() {
for (int i = 1; i < numBooks; ++i) {
Book key = library[i];
int j = i - 1;
while (j >= 0 && library[j].id > key.id) {
library[j + 1] = library[j];
j = j - 1;
}
library[j + 1] = key;
}
}
void searchBook() {
int searchId;
string searchTitle;
bool found = false;
cout << "\nSearch by:\n";
cout << "1. ID\n";
cout << "2. Title\n";
cout << "Enter your choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << "Enter ID to search: ";
cin >> searchId;
for (int i = 0; i < numBooks; ++i) {
if (library[i].id == searchId) {
cout << "Book found:\n";
cout << "ID: " << library[i].id << endl;
cout << "Title: " << library[i].title << endl;
cout << "Author: " << library[i].author << endl;
cout << "Status: " << (library[i].available ? "Available" : "Issued") << endl;
if (!library[i].available)
cout << "Borrower: " << library[i].borrower << endl;
found = true;
break;
}
}
break;
case 2:
cout << "Enter title to search: ";
cin.ignore();
getline(cin, searchTitle);
for (int i = 0; i < numBooks; ++i) {
if (library[i].title == searchTitle) {
cout << "Book found:\n";
cout << "ID: " << library[i].id << endl;
cout << "Title: " << library[i].title << endl;
cout << "Author: " << library[i].author << endl;
cout << "Status: " << (library[i].available ? "Available" : "Issued") << endl;
if (!library[i].available)
cout << "Borrower: " << library[i].borrower << endl;
found = true;
break;
}
}
break;
default:
cout << "Invalid choice.\n";
break;
}
if (!found) cout << "Book not found.\n";
}
void issueBook() {
int issueId;
bool found = false;
cout << "Enter ID of the book to issue: ";
cin >> issueId;
for (int i = 0; i < numBooks; ++i) {
if (library[i].id == issueId) {
if (library[i].available) {
library[i].available = false;
cout << "Book '" << library[i].title << "' issued successfully.\n";
cout << "Enter borrower's name: ";
cin.ignore();
getline(cin, library[i].borrower);
} else {
cout << "Book is already issued.\n";
}
found = true;
break;
}
}
if (!found) cout << "Book not found.\n";
}
void returnBook() {
int returnId;
bool found = false;
cout << "Enter ID of the book to return: ";
cin >> returnId;
for (int i = 0; i < numBooks; ++i) {
if (library[i].id == returnId) {
if (!library[i].available) {
library[i].available = true;
library[i].borrower = "";
cout << "Book '" << library[i].title << "' returned successfully.\n";
} else {
cout << "Book is already available.\n";
}
found = true;
break;
}
}
if (!found) cout << "Book not found.\n";
}
void listBooks() {
if (numBooks == 0) {
cout << "No books in the library.\n";
return;
}
cout << "\nList of books in the library:\n";
cout << "---------------------------------\n";
cout << "ID\tTitle\t\tAuthor\tStatus\n";
cout << "---------------------------------\n";
for (int i = 0; i < numBooks; ++i) {
cout << library[i].id << "\t" << library[i].title << "\t\t" << library[i].author << "\t";
if (library[i].available)
cout << "Available\n";
else
cout << "Issued to " << library[i].borrower << endl;
}
cout << "---------------------------------\n";
}
void deleteBook() {
int deleteId;
bool found = false;
cout << "Enter ID of the book to delete: ";
cin >> deleteId;
for (int i = 0; i < numBooks; ++i) {
if (library[i].id == deleteId) {
for (int j = i; j < numBooks - 1; ++j) {
library[j] = library[j + 1];
}
numBooks--;
cout << "Book deleted successfully.\n";
found = true;
break;
}
}
if (!found) cout << "Book not found.\n";
}