This repository was archived by the owner on Jun 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibraryManagement.cpp
More file actions
312 lines (265 loc) · 8.55 KB
/
libraryManagement.cpp
File metadata and controls
312 lines (265 loc) · 8.55 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// Student structure
struct Student {
int roll;
string name;
double balance;
};
// Book structure
struct Book {
string title;
string author;
int isbn;
bool available;
};
// Global variables
Student students[20];
Book books[15];
int student_count = 0;
int book_count = 0;
// Function prototypes
void create_account();
void display_student(int roll);
void deposit_amount(int roll, double amount);
void issue_book(int roll);
void display_all_students();
int find_student(int roll);
int find_book(int isbn);
void add_book();
void edit_book();
void view_books();
int main() {
// Initialization
// Add initial 15 books to the library
for (int i = 0; i < 15; i++) {
books[i] = {"Book " + to_string(i+1), "Author " + to_string(i+1), 1000+i, true};
}
book_count = 15;
while (true) {
cout << "\nLibrary Management System\n";
cout << "1. Admin Login\n";
cout << "2. Student Login\n";
cout << "0. Exit\n";
cout << "Choose option: ";
int option;
cin >> option;
if (option == 0) break;
if (option == 1 || option == 2) {
cout << "Enter password: ";
string password;
cin >> password;
if (password != "password") {
cout << "Incorrect password!\n";
continue;
}
if (option == 1) { // Admin menu
cout << "\nAdmin Menu\n";
cout << "1. Add Book\n";
cout << "2. Edit Book\n";
cout << "3. View Books\n";
cout << "4. View All Students\n";
cout << "5. View Student Details\n";
cout << "Choose option: ";
cin >> option;
switch(option) {
case 1: add_book(); break;
case 2: edit_book(); break;
case 3: view_books(); break;
case 4: display_all_students(); break;
case 5: {
int roll;
cout << "Enter roll number: ";
cin >> roll;
display_student(roll);
break;
}
}
}
else { // Student menu
int roll;
cout << "Enter roll number: ";
cin >> roll;
int index = find_student(roll);
if (index == -1) {
cout << "Account not found. Create new account? (1=Yes, 0=No): ";
cin >> option;
if (option == 1) create_account();
}
else {
cout << "\nStudent Menu\n";
cout << "1. View Details\n";
cout << "2. Deposit Amount\n";
cout << "3. Issue Book\n";
cout << "Choose option: ";
cin >> option;
switch(option) {
case 1: display_student(roll); break;
case 2: {
double amount;
cout << "Enter amount: $";
cin >> amount;
deposit_amount(roll, amount);
break;
}
case 3: issue_book(roll); break;
}
}
}
}
}
return 0;
}
void create_account() {
if (student_count >= 20) {
cout << "Maximum students reached!\n";
return;
}
Student s;
cout << "Enter roll number: ";
cin >> s.roll;
if (find_student(s.roll) {
cout << "Account already exists!\n";
return;
}
cout << "Enter name: ";
cin.ignore();
getline(cin, s.name);
double deposit;
cout << "Enter initial deposit (minimum $50): $";
cin >> deposit;
if (deposit < 50) {
cout << "Deposit too low!\n";
return;
}
s.balance = deposit - 50; // $50 security deposit
students[student_count++] = s;
cout << "Account created successfully!\n";
}
void display_student(int roll) {
int index = find_student(roll);
if (index == -1) {
cout << "Student not found!\n";
return;
}
cout << "\nStudent Details\n";
cout << "Roll: " << students[index].roll << endl;
cout << "Name: " << students[index].name << endl;
cout << fixed << setprecision(2);
cout << "Balance: $" << students[index].balance << endl;
}
void deposit_amount(int roll, double amount) {
int index = find_student(roll);
if (index == -1) {
cout << "Student not found!\n";
return;
}
students[index].balance += amount;
cout << "New balance: $" << students[index].balance << endl;
}
void issue_book(int roll) {
int student_index = find_student(roll);
if (student_index == -1) {
cout << "Student not found!\n";
return;
}
if (students[student_index].balance < 2) {
cout << "Insufficient balance ($2 required)!\n";
return;
}
cout << "\nAvailable Books:\n";
for (int i = 0; i < book_count; i++) {
if (books[i].available) {
cout << i+1 << ". " << books[i].title << " by " << books[i].author
<< " (ISBN: " << books[i].isbn << ")\n";
}
}
cout << "Enter book number (0 to cancel): ";
int choice;
cin >> choice;
if (choice == 0) return;
if (choice < 1 || choice > book_count || !books[choice-1].available) {
cout << "Invalid selection!\n";
return;
}
books[choice-1].available = false;
students[student_index].balance -= 2;
cout << "Book issued! New balance: $" << students[student_index].balance << endl;
}
void display_all_students() {
// Simple bubble sort by roll number
for (int i = 0; i < student_count; i++) {
for (int j = i+1; j < student_count; j++) {
if (students[i].roll > students[j].roll) {
swap(students[i], students[j]);
}
}
}
cout << "\nAll Students (" << student_count << ")\n";
for (int i = 0; i < student_count; i++) {
cout << students[i].roll << " - " << students[i].name
<< " - Balance: $" << fixed << setprecision(2) << students[i].balance << endl;
}
}
int find_student(int roll) {
for (int i = 0; i < student_count; i++) {
if (students[i].roll == roll) return i;
}
return -1;
}
int find_book(int isbn) {
for (int i = 0; i < book_count; i++) {
if (books[i].isbn == isbn) return i;
}
return -1;
}
void add_book() {
if (book_count >= 15) {
cout << "Maximum books reached!\n";
return;
}
Book b;
cout << "Enter book title: ";
cin.ignore();
getline(cin, b.title);
cout << "Enter author: ";
getline(cin, b.author);
cout << "Enter ISBN: ";
cin >> b.isbn;
if (find_book(b.isbn) != -1) {
cout << "Book already exists!\n";
return;
}
b.available = true;
books[book_count++] = b;
cout << "Book added successfully!\n";
}
void edit_book() {
int isbn;
cout << "Enter ISBN of book to edit: ";
cin >> isbn;
int index = find_book(isbn);
if (index == -1) {
cout << "Book not found!\n";
return;
}
cout << "Current title: " << books[index].title << endl;
cout << "Enter new title: ";
cin.ignore();
getline(cin, books[index].title);
cout << "Current author: " << books[index].author << endl;
cout << "Enter new author: ";
getline(cin, books[index].author);
cout << "Book updated successfully!\n";
}
void view_books() {
cout << "\nAll Books (" << book_count << ")\n";
for (int i = 0; i < book_count; i++) {
cout << "Title: " << books[i].title << endl;
cout << "Author: " << books[i].author << endl;
cout << "ISBN: " << books[i].isbn << endl;
cout << "Available: " << (books[i].available ? "Yes" : "No") << endl << endl;
}
}