Repository navigation
Expand file tree
/
Copy pathin_memory_subsystem.cpp
More file actions
205 lines (170 loc) · 4.77 KB
/
Copy pathin_memory_subsystem.cpp
File metadata and controls
205 lines (170 loc) · 4.77 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
#include <iostream>
using namespace std;
const int MAX = 50;
struct Book {
int id;
string title;
string publishyear;
string author;
float price;
int quantity;
};
Book books[MAX];
int countBooks = 0;
// Functions
void addBook();
void displayBooks();
void searchBook();
void updateBook();
void deleteBook();
void countAllBooks();
void searchbybookname();
int main() {
int choice;
while (true) {
cout << "\n===== Library Management System =====\n";
cout << "1. Add Book\n";
cout << "2. Display All Books\n";
cout << "3. Search Book\n";
cout << "4. Update Book\n";
cout << "5. Delete Book\n";
cout << "6. Count Books\n";
cout << "7. Search by book name\n";
cout << "8. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: addBook(); break;
case 2: displayBooks(); break;
case 3: searchBook(); break;
case 4: updateBook(); break;
case 5: deleteBook(); break;
case 6: countAllBooks(); break;
case 7: searchbybookname(); break;
case 8: cout << "Exiting program...\n"; return 0;
default: cout << "Invalid choice!\n";
}
}
}
// ➕ Add Book
void addBook() {
if (countBooks >= MAX) {
cout << "Library is full!\n";
return;
}
int id;
cout << "Enter Book ID: ";
cin >> id;
// duplicate check
for (int i = 0; i < countBooks; i++) {
if (books[i].id == id) {
cout << "Book ID already exists!\n";
return;
}
}
books[countBooks].id = id;
cout << "Enter Title: ";
cin.ignore();
getline(cin, books[countBooks].title);
cout << "ENTER PUBLISH YEAR: ";
getline(cin, books[countBooks].publishyear);
cout << "Enter Author: ";
getline(cin, books[countBooks].author);
cout << "Enter Price: ";
cin >> books[countBooks].price;
cout << "Enter Quantity: ";
cin >> books[countBooks].quantity;
countBooks++;
cout << "Book Added Successfully!\n";
}
// 📄 Display Books
void displayBooks() {
if (countBooks == 0) {
cout << "No books available!\n";
return;
}
cout << "\nID\tTitle\t\tAuthor\t\tPrice\tQty\tPublish Year \n ";
cout << "--------------------------------------------------\n";
for (int i = 0; i < countBooks; i++) {
cout << books[i].id << "\t"
<< books[i].title << "\t\t"
<< books[i].publishyear << "\t\t"
<< books[i].author << "\t\t"
<< books[i].price << "\t"
<< books[i].quantity << endl;
}
}
// 🔍 Search Book
void searchBook() {
int id;
cout << "Enter Book ID to search: ";
cin >> id;
for (int i = 0; i < countBooks; i++) {
if (books[i].id == id) {
cout << "Book Found:\n";
cout << "Title: " << books[i].title <<endl;
cout << "Publish year:" << books[i].publishyear <<endl;
cout << "Author: " << books[i].author <<endl;
cout << "Price: " << books[i].price <<endl;
cout << "Quantity: " << books[i].quantity <<endl;
return;
}
}
cout << "Book not found!\n";
}
// ✏️ Update Book
void updateBook() {
int id;
cout << "Enter Book ID to update: ";
cin >> id;
for (int i = 0; i < countBooks; i++) {
if (books[i].id == id) {
cout << "Enter New Price: ";
cin >> books[i].price;
cout << "Enter New Quantity: ";
cin >> books[i].quantity;
cout << "Book Updated Successfully!\n";
return;
}
}
cout << "Book not found!\n";
}
// ❌ Delete Book
void deleteBook() {
int id;
cout << "Enter Book ID to delete: ";
cin >> id;
for (int i = 0; i < countBooks; i++) {
if (books[i].id == id) {
for (int j = i; j < countBooks - 1; j++) {
books[j] = books[j + 1];
}
countBooks--;
cout << "Book Deleted Successfully!\n";
return;
}
}
cout << "Book not found!\n";
}
// 🔢 Count Books
void countAllBooks() {
cout << "Total Books: " << countBooks << endl;
}
// Search by book name
void searchbybookname() {
string name;
cout << "Enter Book Name to search:";
cin.ignore();
getline(cin,name);
for (int i = 0; i <countBooks; i++){
if (books[i].title == name){
cout <<"book found:\n";
cout << "ID:" << books[i].id <<endl;
cout << "publish year:"<< books[i].publishyear <<endl;
cout << "author:" << books[i].author <<endl;
cout << "price:" << books[i].price <<endl;
cout << "quantity:" << books[i].quantity <<endl;
return;
}
}
}