Repository navigation
Expand file tree
/
Copy pathproject3.cpp
More file actions
104 lines (90 loc) · 2.2 KB
/
Copy pathproject3.cpp
File metadata and controls
104 lines (90 loc) · 2.2 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
#include <iostream>
#include <vector>
using namespace std;
class Book {
public:
string title, author;
int id;
Book(int id, string title, string author) {
this->id = id;
this->title = title;
this->author = author;
}
void display() {
cout << "Book ID: " << id << "\nTitle: " << title << "\nAuthor: " << author << "\n\n";
}
};
class Library {
private:
vector<Book> books;
public:
void addBook() {
int id;
string title, author;
cout << "Enter Book ID: ";
cin >> id;
cin.ignore();
cout << "Enter Book Title: ";
getline(cin, title);
cout << "Enter Author Name: ";
getline(cin, author);
books.push_back(Book(id, title, author));
cout << "Book added successfully!\n";
}
void displayBooks() {
if (books.empty()) {
cout << "Library is empty.\n";
return;
}
cout << "--- Book List ---\n";
for (Book b : books)
b.display();
}
void searchBook() {
string searchTitle;
cin.ignore();
cout << "Enter the title to search: ";
getline(cin, searchTitle);
bool found = false;
for (Book b : books) {
if (b.title == searchTitle) {
cout << "Book found!\n";
b.display();
found = true;
break;
}
}
if (!found)
cout << "Book not found!\n";
}
};
int main() {
Library lib;
int choice;
do {
cout << "\n--- Library Menu ---\n";
cout << "1. Add Book\n";
cout << "2. Display All Books\n";
cout << "3. Search Book\n";
cout << "4. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
lib.addBook();
break;
case 2:
lib.displayBooks();
break;
case 3:
lib.searchBook();
break;
case 4:
cout << "Exiting Library System...\n";
break;
default:
cout << "Invalid choice. Try again.\n";
}
} while (choice != 4);
return 0;
}