-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.cpp
147 lines (134 loc) · 4.03 KB
/
library.cpp
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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Book {
int id;
string title;
string author;
bool isAvailable;
};
class Library {
private:
vector<Book> books;
int nextId = 1;
public:
void addBook(const string& title, const string& author) {
Book newBook = {nextId++, title, author, true};
books.push_back(newBook);
cout << "Book added successfully!\n";
}
void displayBooks() {
if (books.empty()) {
cout << "No books available in the library.\n";
return;
}
cout << "List of Books:\n";
for (const auto& book : books) {
cout << "ID: " << book.id
<< ", Title: " << book.title
<< ", Author: " << book.author
<< ", Available: " << (book.isAvailable ? "Yes" : "No") << '\n';
}
}
void searchBook(const string& title) {
bool found = false;
for (const auto& book : books) {
if (book.title.find(title) != string::npos) {
cout << "Found Book - ID: " << book.id
<< ", Title: " << book.title
<< ", Author: " << book.author
<< ", Available: " << (book.isAvailable ? "Yes" : "No") << '\n';
found = true;
}
}
if (!found) {
cout << "No books found with the title \"" << title << "\".\n";
}
}
void issueBook(int id) {
for (auto& book : books) {
if (book.id == id) {
if (book.isAvailable) {
book.isAvailable = false;
cout << "Book issued successfully.\n";
} else {
cout << "Book is already issued.\n";
}
return;
}
}
cout << "Book with ID " << id << " not found.\n";
}
void returnBook(int id) {
for (auto& book : books) {
if (book.id == id) {
if (!book.isAvailable) {
book.isAvailable = true;
cout << "Book returned successfully.\n";
} else {
cout << "Book was not issued.\n";
}
return;
}
}
cout << "Book with ID " << id << " not found.\n";
}
};
int main() {
Library library;
int choice;
do {
cout << "\nLibrary Management System\n";
cout << "1. Add Book\n";
cout << "2. Display Books\n";
cout << "3. Search Book by Title\n";
cout << "4. Issue Book\n";
cout << "5. Return Book\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // To ignore leftover newline character from previous input
switch (choice) {
case 1: {
string title, author;
cout << "Enter title: ";
getline(cin, title);
cout << "Enter author: ";
getline(cin, author);
library.addBook(title, author);
break;
}
case 2:
library.displayBooks();
break;
case 3: {
string title;
cout << "Enter title to search: ";
getline(cin, title);
library.searchBook(title);
break;
}
case 4: {
int id;
cout << "Enter book ID to issue: ";
cin >> id;
library.issueBook(id);
break;
}
case 5: {
int id;
cout << "Enter book ID to return: ";
cin >> id;
library.returnBook(id);
break;
}
case 6:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 6);
return 0;
}