-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.cpp
More file actions
628 lines (532 loc) · 19.1 KB
/
library.cpp
File metadata and controls
628 lines (532 loc) · 19.1 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include <iostream>
#include <string>
#include <vector>
#include <limits> // For numeric_limits
#include <algorithm> // For std::transform, std::remove, std::remove_if
#include <fstream> // For file handling (CSV)
#include <sstream> // For string stream parsing (CSV)
#include <cctype> // For ::tolower
using namespace std;
// --- Utility Functions ---
// Utility function to handle the input stream state after using cin >> int
void clearInputBuffer() {
// Uses numeric_limits to ignore the maximum possible number of characters up to the newline
if (cin.fail()) {
cin.clear();
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
// Utility function to convert a string to lowercase
string toLower(const string& str) {
string data = str;
// Use std::tolower from <cctype>
transform(data.begin(), data.end(), data.begin(),
[](unsigned char c){ return std::tolower(c); });
return data;
}
// --- Book Class Definition ---
class Book {
private:
string bookID;
string title;
string author;
bool isIssued;
public:
// Constructor
Book(string id, string t, string a)
: bookID(id), title(t), author(a), isIssued(false) {}
// Getters
string getBookID() const { return bookID; }
string getTitle() const { return title; }
string getAuthor() const { return author; }
bool getIsIssued() const { return isIssued; }
// Mutator methods for status
bool issueBook() {
if (isIssued) {
return false;
}
isIssued = true;
return true;
}
void returnBook() {
isIssued = false;
}
// Setter for loading data from file
void setIsIssued(bool issued) {
isIssued = issued;
}
void display() const {
cout << " --- Book Details ---\n";
cout << " ID: " << bookID << "\n";
cout << " Title: " << title << "\n";
cout << " Author: " << author << "\n";
cout << " Status: " << (isIssued ? "Issued" : "Available") << "\n";
}
};
// --- Member Class Definition ---
class Member {
private:
string memberID;
string name;
vector<string> issuedBookIDs;
public:
// Constructor
Member(string id, string n) : memberID(id), name(n) {}
// Overload for loading from CSV with issued books
Member(string id, string n, const vector<string>& issuedIDs)
: memberID(id), name(n), issuedBookIDs(issuedIDs) {}
// Getters
string getMemberID() const { return memberID; }
string getName() const { return name; }
const vector<string>& getIssuedBookIDs() const { return issuedBookIDs; }
void issueBook(string bookID) {
issuedBookIDs.push_back(bookID);
}
void returnBook(const string& bookID) {
// Use erase-remove idiom for efficient and correct vector removal
auto it = std::remove(issuedBookIDs.begin(), issuedBookIDs.end(), bookID);
issuedBookIDs.erase(it, issuedBookIDs.end());
}
void display() const {
cout << " --- Member Details ---\n";
cout << " Member ID: " << memberID << "\n";
cout << " Name: " << name << "\n";
cout << " Books Issued: " << issuedBookIDs.size() << "\n";
if (!issuedBookIDs.empty()) {
cout << " Issued Book IDs: ";
for (const string& id : issuedBookIDs) {
cout << id << " ";
}
cout << "\n";
}
}
};
// --- Library Class Definition ---
class Library {
private:
vector<Book> books;
vector<Member> members;
// Helper functions (const and non-const versions)
Book* findBookByID(const string& bookID) {
for (Book& b : books) {
if (b.getBookID() == bookID) {
return &b;
}
}
return nullptr;
}
Member* findMemberByID(const string& memberID) {
for (Member& m : members) {
if (m.getMemberID() == memberID) {
return &m;
}
}
return nullptr;
}
const Member* findMemberByIDConst(const string& memberID) const {
for (const Member& m : members) {
if (m.getMemberID() == memberID) {
return &m;
}
}
return nullptr;
}
bool memberHasBook(const Member* member, const string& bookID) const {
for (const string& id : member->getIssuedBookIDs()) {
if (id == bookID) {
return true;
}
}
return false;
}
public:
// --- Core Management Functions ---
void addBook() {
string id, title, author;
cout << "Enter Book ID: ";
getline(cin, id);
if (findBookByID(id) != nullptr) {
cout << "\nError: A Book with ID '" << id << "' already exists. Addition failed.\n";
return;
}
cout << "Enter Title: ";
getline(cin, title);
cout << "Enter Author: ";
getline(cin, author);
books.emplace_back(id, title, author);
cout << "\nSuccess: Book '" << title << "' added.\n";
}
void removeBook() {
string id;
cout << "Enter Book ID to remove: ";
getline(cin, id);
// Find and erase the book using erase-remove idiom with a lambda
auto it = std::remove_if(books.begin(), books.end(),
[&](const Book& b) {
return b.getBookID() == id;
});
if (it != books.end()) {
string title = it->getTitle();
books.erase(it, books.end());
// Remove book ID from all members' issued lists to maintain consistency
for (Member& m : members) {
m.returnBook(id);
}
cout << "\nSuccess: Book '" << title << "' (ID: " << id << ") removed.\n";
} else {
cout << "\nError: Book with ID '" << id << "' not found.\n";
}
}
void addMember() {
string id, name;
cout << "Enter Member ID: ";
getline(cin, id);
if (findMemberByID(id) != nullptr) {
cout << "\nError: A Member with ID '" << id << "' already exists. Registration failed.\n";
return;
}
cout << "Enter Member Name: ";
getline(cin, name);
members.emplace_back(id, name);
cout << "\nSuccess: Member '" << name << "' added.\n";
}
void removeMember() {
string id;
cout << "Enter Member ID to remove: ";
getline(cin, id);
// Find the member iterator before removal
auto member_it = std::find_if(members.begin(), members.end(),
[&](const Member& m) { return m.getMemberID() == id; });
if (member_it != members.end()) {
string name = member_it->getName();
const vector<string>& issuedIDs = member_it->getIssuedBookIDs();
if (!issuedIDs.empty()) {
cout << "\nError: Member '" << name << "' still has " << issuedIDs.size() << " book(s) issued. Please return them first.\n";
return;
}
members.erase(member_it);
cout << "\nSuccess: Member '" << name << "' (ID: " << id << ") removed.\n";
} else {
cout << "\nError: Member with ID '" << id << "' not found.\n";
}
}
void issueBook() {
string memberID, bookID;
cout << "Enter Member ID: ";
getline(cin, memberID);
cout << "Enter Book ID: ";
getline(cin, bookID);
Member* member = findMemberByID(memberID);
if (member == nullptr) {
cout << "\nError: Member not found with ID " << memberID << ".\n";
return;
}
Book* book = findBookByID(bookID);
if (book == nullptr) {
cout << "\nError: Book not found with ID " << bookID << ".\n";
return;
}
if (book->issueBook()) {
member->issueBook(bookID);
cout << "\nSuccess: Book '" << book->getTitle() << "' issued to " << member->getName() << ".\n";
} else {
cout << "\nError: Book '" << book->getTitle() << "' is already issued.\n";
}
}
void returnBook() {
string memberID, bookID;
cout << "Enter Member ID: ";
getline(cin, memberID);
cout << "Enter Book ID: ";
getline(cin, bookID);
Member* member = findMemberByID(memberID);
if (member == nullptr) {
cout << "\nError: Member not found with ID " << memberID << ".\n";
return;
}
Book* book = findBookByID(bookID);
if (book == nullptr) {
cout << "\nError: Book not found with ID " << bookID << ".\n";
return;
}
if (!book->getIsIssued()) {
cout << "\nError: Book '" << book->getTitle() << "' is not currently marked as issued.\n";
return;
}
// Note: Even if the member doesn't explicitly list the book, we perform the return
// to correct the book's status and clean the member's list if possible.
book->returnBook();
member->returnBook(bookID);
cout << "\nSuccess: Book '" << book->getTitle() << "' returned by " << member->getName() << ".\n";
}
// --- Display and Search Functions ---
void searchBooks() const {
string query;
cout << "Enter title or author search query: ";
getline(cin, query);
string lowerQuery = toLower(query);
vector<const Book*> results;
for (const Book& b : books) {
string lowerTitle = toLower(b.getTitle());
string lowerAuthor = toLower(b.getAuthor());
if (lowerTitle.find(lowerQuery) != string::npos ||
lowerAuthor.find(lowerQuery) != string::npos) {
results.push_back(&b);
}
}
cout << "\n--- Search Results (" << results.size() << " found) ---\n";
if (results.empty()) {
cout << "No books matched your query: '" << query << "'.\n";
} else {
for (const Book* b : results) {
b->display();
cout << "----------------------------\n";
}
}
}
void showAllBooks() const {
if (books.empty()) {
cout << "\n--- Library Book Catalog ---\n";
cout << "No books in the library.\n";
return;
}
cout << "\n--- Library Book Catalog (" << books.size() << " total) ---\n";
for (const Book& b : books) {
b.display();
cout << "----------------------------\n";
}
}
void showAllMembers() const {
if (members.empty()) {
cout << "\n--- Library Member List ---\n";
cout << "No members registered in the library.\n";
return;
}
cout << "\n--- Library Member List (" << members.size() << " total) ---\n";
for (const Member& m : members) {
m.display();
cout << "----------------------------\n";
}
}
// --- CSV Save/Load Functionality ---
void saveBooksToCSV() const {
ofstream outFile("books.csv");
if (!outFile.is_open()) {
cout << "\nError: Could not open 'books.csv' for saving.\n";
return;
}
// Header
outFile << "BookID,Title,Author,IsIssued\n";
for (const Book& b : books) {
outFile << b.getBookID() << ","
<< b.getTitle() << ","
<< b.getAuthor() << ","
<< (b.getIsIssued() ? 1 : 0) << "\n";
}
outFile.close();
cout << "\nSuccess: " << books.size() << " books saved to 'books.csv'.\n";
}
void loadBooksFromCSV() {
ifstream inFile("books.csv");
if (!inFile.is_open()) {
cout << "\nWarning: 'books.csv' not found or could not be opened. No book data loaded.\n";
return;
}
string line;
// Skip header line
getline(inFile, line);
int loadedCount = 0;
int duplicateCount = 0;
while (getline(inFile, line)) {
stringstream ss(line);
string id, title, author, issuedStatusStr;
int issuedInt;
if (getline(ss, id, ',') &&
getline(ss, title, ',') &&
getline(ss, author, ',') &&
(ss >> issuedInt)) {
if (findBookByID(id) == nullptr) {
Book newBook(id, title, author);
if (issuedInt == 1) {
newBook.setIsIssued(true);
}
books.push_back(newBook);
loadedCount++;
} else {
duplicateCount++;
}
}
}
inFile.close();
cout << "\nSuccess: Loaded " << loadedCount << " new books from 'books.csv'. "
<< (duplicateCount > 0 ? to_string(duplicateCount) + " duplicates skipped." : "") << "\n";
}
void saveMembersToCSV() const {
ofstream outFile("members.csv");
if (!outFile.is_open()) {
cout << "\nError: Could not open 'members.csv' for saving.\n";
return;
}
// Header
outFile << "MemberID,Name,IssuedBookIDs\n";
for (const Member& m : members) {
outFile << m.getMemberID() << ","
<< m.getName() << ",";
// Join all issued book IDs with a semicolon (;)
const vector<string>& issuedIDs = m.getIssuedBookIDs();
for (size_t i = 0; i < issuedIDs.size(); ++i) {
outFile << issuedIDs[i];
if (i < issuedIDs.size() - 1) {
outFile << ";";
}
}
outFile << "\n";
}
outFile.close();
cout << "\nSuccess: " << members.size() << " members saved to 'members.csv'.\n";
}
void loadMembersFromCSV() {
ifstream inFile("members.csv");
if (!inFile.is_open()) {
cout << "\nWarning: 'members.csv' not found or could not be opened. No member data loaded.\n";
return;
}
string line;
// Skip header line
getline(inFile, line);
int loadedCount = 0;
int duplicateCount = 0;
while (getline(inFile, line)) {
stringstream ss(line);
string id, name, issuedIDsStr;
if (getline(ss, id, ',') &&
getline(ss, name, ',') &&
getline(ss, issuedIDsStr)) {
// 1. Parse Issued Book IDs by splitting the string stream by ';'
vector<string> issuedIDs;
stringstream issuedSS(issuedIDsStr);
string bookID;
while (getline(issuedSS, bookID, ';')) {
if (!bookID.empty()) {
issuedIDs.push_back(bookID);
}
}
// 2. Add Member (check for duplicates)
if (findMemberByID(id) == nullptr) {
members.emplace_back(id, name, issuedIDs);
loadedCount++;
} else {
duplicateCount++;
}
}
}
inFile.close();
cout << "\nSuccess: Loaded " << loadedCount << " new members from 'members.csv'. "
<< (duplicateCount > 0 ? to_string(duplicateCount) + " duplicates skipped." : "") << "\n";
}
};
// Displays the main menu options
void showMenu() {
cout << "\n===== Library Management System =====\n";
cout << "1. Add New Book\n";
cout << "2. Register New Member\n";
cout << "3. Issue a Book\n";
cout << "4. Return a Book\n";
cout << "5. Show All Books\n";
cout << "6. Show All Members\n";
cout << "7. Search Books (Title/Author)\n";
cout << "8. Remove Book\n";
cout << "9. Remove Member\n";
cout << "10. Save Books to CSV\n";
cout << "11. Load Books from CSV\n";
cout << "12. Save Members to CSV\n";
cout << "13. Load Members from CSV\n";
cout << "14. Exit\n";
cout << "=====================================\n";
cout << "Enter your choice (1-14): ";
}
// --- Main Program Loop ---
int main() {
// Ensure the console starts clean
cout << "Welcome! Library Management System is ready.\n";
// Create the library object
Library myLibrary;
int choice;
cout << "\nAttempting to load persistent data...\n";
myLibrary.loadBooksFromCSV();
myLibrary.loadMembersFromCSV();
cout << "Data loading complete.\n";
while (true) {
showMenu();
// Use temporary stream variable to handle potential bad input before cin >> choice
if (!(cin >> choice)) {
clearInputBuffer(); // Clear the bad input
cout << "\nError: Invalid input. Please enter a number (1-14).\n";
continue;
}
// Clear the buffer after successful int read to prepare for subsequent getline() calls
clearInputBuffer();
switch (choice) {
case 1:
cout << "\n--- Add New Book ---\n";
myLibrary.addBook();
break;
case 2:
cout << "\n--- Register New Member ---\n";
myLibrary.addMember();
break;
case 3:
cout << "\n--- Issue a Book ---\n";
myLibrary.issueBook();
break;
case 4:
cout << "\n--- Return a Book ---\n";
myLibrary.returnBook();
break;
case 5:
myLibrary.showAllBooks();
break;
case 6:
myLibrary.showAllMembers();
break;
case 7:
cout << "\n--- Search Library ---\n";
myLibrary.searchBooks();
break;
case 8:
cout << "\n--- Remove Book ---\n";
myLibrary.removeBook();
break;
case 9:
cout << "\n--- Remove Member ---\n";
myLibrary.removeMember();
break;
case 10:
cout << "\n--- Saving Book Data ---\n";
myLibrary.saveBooksToCSV();
break;
case 11:
cout << "\n--- Loading Book Data ---\n";
myLibrary.loadBooksFromCSV();
break;
case 12:
cout << "\n--- Saving Member Data ---\n";
myLibrary.saveMembersToCSV();
break;
case 13:
cout << "\n--- Loading Member Data ---\n";
myLibrary.loadMembersFromCSV();
break;
case 14:
cout << "\nSaving all data before exit...\n";
myLibrary.saveBooksToCSV();
myLibrary.saveMembersToCSV();
cout << "Thank you for using the Library Management System. Goodbye!\n";
return 0;
default:
cout << "\nError: Invalid choice. Please select from 1-14.\n";
break;
}
}
return 0;
}