-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_project.cpp
More file actions
616 lines (537 loc) · 15.4 KB
/
Copy pathfinal_project.cpp
File metadata and controls
616 lines (537 loc) · 15.4 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
#include <iostream>
#include <map>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class LibraryItem
{
public:
virtual void displayDetails() const = 0;
};
class Book : public LibraryItem
{
private:
int bookId;
string title;
string author;
string genre;
int totalCopies;
int availableCopies;
int borrowCount;
float averageRating;
public:
Book(int id, string title, string author, string genre, int copies)
: bookId(id), title(title), author(author), genre(genre),
totalCopies(copies), availableCopies(copies), borrowCount(0), averageRating(0.0) {}
Book() : bookId(0), totalCopies(0), availableCopies(0), borrowCount(0), averageRating(0.0) {}
int getBookID() const { return bookId; }
string getTitle() const { return title; }
string getAuthor() const { return author; }
string getGenre() const { return genre; }
int getTotalCopies() const { return totalCopies; }
int getAvailableCopies() const { return availableCopies; }
int getBorrowCount() const { return borrowCount; }
float getAverageRating() const { return averageRating; }
void incrementBorrowCount() { borrowCount++; }
void addCopies(int count);
void issueBook();
void returnBook();
void displayDetails() const override;
friend ofstream &operator<<(ofstream &os, const Book &book);
friend ifstream &operator>>(ifstream &is, Book &book);
};
class Member
{
protected:
int memberID;
string name;
string contactInfo;
public:
Member(int memberID, string name, string contactInfo)
: memberID(memberID), name(name), contactInfo(contactInfo) {}
Member() : memberID(0) {}
int getMemberID() const { return memberID; }
string getName() const { return name; }
string getContactInfo() const { return contactInfo; }
virtual void displayMemberInfo() const;
virtual string getType() const { return "Member"; }
friend ofstream &operator<<(ofstream &os, const Member &member);
friend ifstream &operator>>(ifstream &is, Member &member);
};
class Student : public Member
{
private:
float finePerDay;
int daysLate;
public:
Student(int memberID, string name, string contactInfo, float finePerDay = 1.0, int daysLate = 0)
: Member(memberID, name, contactInfo), finePerDay(finePerDay), daysLate(daysLate) {}
Student() : finePerDay(1.0), daysLate(0) {}
void setDaysLate(int days) { daysLate = days; }
float calculateFine() const;
void displayMemberInfo() const override;
string getType() const override { return "Student"; }
friend ofstream &operator<<(ofstream &os, const Student &student);
friend ifstream &operator>>(ifstream &is, Student &student);
};
class Library
{
private:
vector<Book> books;
vector<Member *> members;
map<int, int> issuedBooks;
void saveBooksToFile();
void saveMembersToFile();
void loadBooksFromFile();
void loadMembersFromFile();
public:
Library();
~Library();
void addBook(const Book &book);
void addMember(Member *member);
void displayAllBooks() const;
void displayAllMembers() const;
void issueBook(int bookID, int memberId);
void returnBook(int bookID, int memberId);
void showMenu();
Member *findMemberByID(int memberID) const
{
for (const auto &member : members)
{
if (member->getMemberID() == memberID)
{
return member;
}
}
return nullptr;
}
};
int main()
{
Library library;
library.showMenu();
return 0;
}
void Library::showMenu()
{
while (true)
{
cout << "\n--- Library Menu ---\n";
cout << "1. Add Book\n2. Add Member\n3. Display All Books\n4. Display All Members\n5. Issue Book\n6. Return Book\n7. Exit\n";
cout << "Enter your choice: ";
int choice;
cin >> choice;
switch (choice)
{
case 1:
{
int id, copies;
string title, author, genre;
cout << "Enter Book ID: ";
cin >> id;
cin.ignore();
cout << "Enter Title: ";
getline(cin, title);
cout << "Enter Author: ";
getline(cin, author);
cout << "Enter Genre: ";
getline(cin, genre);
cout << "Enter Total Copies: ";
cin >> copies;
addBook(Book(id, title, author, genre, copies));
break;
}
case 2:
{
int id;
string name, contactInfo, type;
cout << "Enter Member ID: ";
cin >> id;
cin.ignore();
cout << "Enter Name: ";
getline(cin, name);
cout << "Enter Contact Info: ";
getline(cin, contactInfo);
cout << "Enter Member Type (Member/Student): ";
cin >> type;
if (type == "Student")
{
addMember(new Student(id, name, contactInfo));
}
else
{
addMember(new Member(id, name, contactInfo));
}
break;
}
case 3:
displayAllBooks();
break;
case 4:
displayAllMembers();
break;
case 5:
{
int bookID, memberID;
cout << "Enter Book ID to issue: ";
cin >> bookID;
cout << "Enter Member ID to issue the book to: ";
cin >> memberID;
issueBook(bookID, memberID);
break;
}
case 6:
{
int bookID, memberID;
cout << "Enter Book ID to return: ";
cin >> bookID;
cout << "Enter Member ID returning the book: ";
cin >> memberID;
returnBook(bookID, memberID);
break;
}
case 7:
{
cout << "Exiting the library system. Goodbye!" << endl;
return;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
}
}
// Book class definitions
void Book::addCopies(int count)
{
totalCopies += count;
availableCopies += count;
}
void Book::issueBook()
{
if (availableCopies > 0)
{
availableCopies--;
incrementBorrowCount();
cout << "Book \"" << title << "\" has been issued successfully." << endl;
}
else
{
cout << "All copies of \"" << title << "\" are currently unavailable." << endl;
}
}
void Book::returnBook()
{
if (availableCopies < totalCopies)
{
availableCopies++;
}
else
{
cout << "All copies of \"" << title << "\" are already in the library." << endl;
}
}
void Book::displayDetails() const
{
cout << "\n--- Book Details ---\n";
cout << "Book ID: " << bookId << "\nTitle: " << title << "\nAuthor: " << author
<< "\nGenre: " << genre
<< "\nTotal Copies: " << totalCopies
<< "\nAvailable Copies: " << availableCopies
<< "\nTimes Borrowed: " << borrowCount
<< "\nAverage Rating: " << averageRating << endl;
}
ofstream &operator<<(ofstream &os, const Book &book)
{
os << book.bookId << "|" << book.title << "|" << book.author << "|"
<< book.genre << "|" << book.totalCopies << "|" << book.availableCopies << "|"
<< book.borrowCount << "|" << book.averageRating << endl;
return os;
}
ifstream &operator>>(ifstream &is, Book &book)
{
string line;
if (getline(is, line))
{
istringstream ss(line);
string temp;
getline(ss, temp, '|');
book.bookId = stoi(temp);
getline(ss, book.title, '|');
getline(ss, book.author, '|');
getline(ss, book.genre, '|');
getline(ss, temp, '|');
book.totalCopies = stoi(temp);
getline(ss, temp, '|');
book.availableCopies = stoi(temp);
getline(ss, temp, '|');
book.borrowCount = stoi(temp);
getline(ss, temp, '|');
book.averageRating = stof(temp);
}
return is;
}
// Member class definitions
void Member::displayMemberInfo() const
{
cout << "\n--- Member Details ---\n";
cout << "Member ID: " << memberID << "\nName: " << name << "\nContact Info: " << contactInfo << endl;
}
ofstream &operator<<(ofstream &os, const Member &member)
{
os << member.memberID << "|" << member.name << "|" << member.contactInfo << "|"
<< member.getType() << endl;
return os;
}
ifstream &operator>>(ifstream &is, Member &member)
{
string line;
if (getline(is, line))
{
istringstream ss(line);
string temp;
getline(ss, temp, '|');
member.memberID = stoi(temp);
getline(ss, member.name, '|');
getline(ss, member.contactInfo, '|');
}
return is;
}
// Student class definitions
float Student::calculateFine() const
{
return daysLate * finePerDay;
}
void Student::displayMemberInfo() const
{
Member::displayMemberInfo();
cout << "Fine Per Day: " << finePerDay << "\nDays Late: " << daysLate
<< "\nCalculated Fine: " << calculateFine() << endl;
}
ofstream &operator<<(ofstream &os, const Student &student)
{
os << student.memberID << "|" << student.name << "|" << student.contactInfo << "|"
<< student.finePerDay << "|" << student.daysLate << "|Student" << endl;
return os;
}
ifstream &operator>>(ifstream &is, Student &student)
{
string line;
if (getline(is, line))
{
istringstream ss(line);
string temp;
getline(ss, temp, '|');
student.memberID = stoi(temp);
getline(ss, student.name, '|');
getline(ss, student.contactInfo, '|');
getline(ss, temp, '|');
student.finePerDay = stof(temp);
getline(ss, temp, '|');
student.daysLate = stoi(temp);
}
return is;
}
// Library class definitions
Library::Library()
{
loadBooksFromFile();
loadMembersFromFile();
}
Library::~Library()
{
saveBooksToFile();
saveMembersToFile();
// Optionally save issuedBooks to a file
}
void Library::saveBooksToFile()
{
ofstream outFile("books.txt");
for (const auto &book : books)
{
outFile << book;
}
}
void Library::saveMembersToFile()
{
ofstream outFile("members.txt");
for (const auto &member : members)
{
outFile << *member;
}
}
void Library::loadBooksFromFile()
{
ifstream inFile("books.txt");
if (!inFile)
return;
Book book;
while (inFile >> book)
{
books.push_back(book);
}
}
void Library::loadMembersFromFile()
{
ifstream inFile("members.txt");
if (!inFile)
{
return;
}
string line;
while (getline(inFile, line))
{
istringstream ss(line);
string memberType;
getline(ss, memberType, '|');
if (memberType == "Student")
{
string memberID, name, contactInfo, finePerDay, daysLate;
if (getline(ss, memberID, '|') &&
getline(ss, name, '|') &&
getline(ss, contactInfo, '|') &&
getline(ss, finePerDay, '|') &&
getline(ss, daysLate, '|'))
{
int id = stoi(memberID);
float fine = stof(finePerDay);
int late = stoi(daysLate);
Student *student = new Student(id, name, contactInfo, fine, late);
members.push_back(student);
}
else
{
cerr << "Error: Invalid Student data format in line: " << line << endl;
}
}
else if (memberType == "Member")
{
string memberID, name, contactInfo;
if (getline(ss, memberID, '|') &&
getline(ss, name, '|') &&
getline(ss, contactInfo, '|'))
{
int id = stoi(memberID);
Member *member = new Member(id, name, contactInfo);
members.push_back(member);
}
else
{
cerr << "Error: Invalid Member data format in line: " << line << endl;
}
}
else
{
cerr << "Error: Unknown member type in line: " << line << endl;
}
}
}
void Library::addBook(const Book &book)
{
books.push_back(book);
saveBooksToFile();
}
void Library::addMember(Member *member)
{
members.push_back(member);
saveMembersToFile();
}
void Library::displayAllBooks() const
{
for (const auto &book : books)
{
book.displayDetails();
}
}
void Library::displayAllMembers() const
{
for (const auto &member : members)
{
if (member->getMemberID() == 'Member')
{
member->displayMemberInfo();
}
else
{
Student *student = dynamic_cast<Student *>(member);
student->displayMemberInfo();
}
}
}
void Library::issueBook(int bookID, int memberID)
{
Member *member = findMemberByID(memberID);
if (!member)
{
cout << "Error: Member with ID " << memberID << " does not exist." << endl;
return;
}
auto bookIt = find_if(books.begin(), books.end(), [bookID](const Book &book)
{ return book.getBookID() == bookID; });
if (bookIt != books.end())
{
if (issuedBooks.find(bookID) == issuedBooks.end())
{
bookIt->issueBook();
issuedBooks[bookID] = memberID;
saveBooksToFile();
cout << "Book with ID " << bookID << " issued to Member ID " << memberID << "." << endl;
}
else
{
cout << "Book with ID " << bookID << " is already issued to Member ID " << issuedBooks[bookID] << "." << endl;
}
}
else
{
cout << "Book with ID " << bookID << " not found." << endl;
}
}
void Library::returnBook(int bookID, int memberID)
{
Member *member = findMemberByID(memberID);
if (!member)
{
cout << "Error: Member with ID " << memberID << " does not exist." << endl;
return;
}
auto bookIt = find_if(books.begin(), books.end(), [bookID](const Book &book)
{ return book.getBookID() == bookID; });
if (bookIt != books.end())
{
auto issueIt = issuedBooks.find(bookID);
if (issueIt != issuedBooks.end() && issueIt->second == memberID)
{
bookIt->returnBook();
issuedBooks.erase(issueIt);
saveBooksToFile();
if (member->getType() == "Student")
{
Student *student = dynamic_cast<Student *>(member);
if (student)
{
int daysLate;
cout << "Enter the number of days late: ";
cin >> daysLate;
student->setDaysLate(daysLate);
cout << "Fine for late return: Rs." << student->calculateFine() << endl;
}
}
cout << "Book with ID " << bookID << " returned by Member ID " << memberID << "." << endl;
}
else if (issueIt == issuedBooks.end())
{
cout << "Book with ID " << bookID << " is not issued to any member." << endl;
}
else
{
cout << "Book with ID " << bookID << " was not issued to Member ID " << memberID << "." << endl;
}
}
else
{
cout << "Book with ID " << bookID << " not found." << endl;
}
}