-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary Book Management System.cpp
More file actions
105 lines (86 loc) · 2.57 KB
/
Library Book Management System.cpp
File metadata and controls
105 lines (86 loc) · 2.57 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
#include <iostream>
using namespace std;
class Book {
private:
int id; // Book ID
string title; // Book title
string author; // Author name
public:
// Function to enter book details
void addBook() {
cout << "Enter Book ID: ";
cin >> id;
cout << "Enter Book Title: ";
cin >> title;
cout << "Enter Author Name: ";
cin >> author;
}
// Function to display one book's details
void display() {
cout << "\nBook ID: " << id;
cout << "\nTitle: " << title;
cout << "\nAuthor: " << author << endl;
}
// Function to check if this book matches the given ID
bool searchByID(int bookID) {
return id == bookID;
}
};
int main() {
Book library[100]; // Array to store up to 100 books
int count = 0; // Number of books added
int choice;
do {
cout << "\n===== LIBRARY MENU =====\n";
cout << "1. Add a Book\n";
cout << "2. Search Book by ID\n";
cout << "3. Display All Books\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
// Add a new book
cout << "\n--- Add New Book ---\n";
library[count].addBook();
count++;
cout << "Book added successfully!\n";
}
else if (choice == 2) {
// Search a book by ID
int bookID;
cout << "Enter Book ID to search: ";
cin >> bookID;
bool found = false;
// Loop through all stored books
for (int i = 0; i < count; i++) {
if (library[i].searchByID(bookID)) {
cout << "\nBook found!\n";
library[i].display();
found = true;
break;
}
}
if (!found) {
cout << "Book not found.\n";
}
}
else if (choice == 3) {
// Display all books
if (count == 0) {
cout << "No books in the library.\n";
} else {
cout << "\n--- List of All Books ---\n";
for (int i = 0; i < count; i++) {
library[i].display();
}
}
}
else if (choice == 4) {
cout << "Exiting... Goodbye!\n";
}
else {
cout << "Invalid choice! Please try again.\n";
}
} while (choice != 4); // Loop continues until user selects Exit
return 0;
}