Library Management System
Library Management System
#include<string>
#include<vector>
using namespace std;
class Book {
protected:
string Title;
string Arthor;
string ISBN;
bool Avalibility;
public:
Book() :Avalibility(true) {}
Book(string T = "N/A", string A = "N/A", string isbn = "N/A") :Title(T),
Arthor(A), ISBN(isbn), Avalibility(true) {}
string get_Title() {
return Title;
}
string get_Arthor() {
return Arthor;
}
string get_ISBN() {
return ISBN;
}
bool get_Avalibility() {
return Avalibility;
}
void Borrow_Book() {
Avalibility = false;
}
void Return_Book() {
Avalibility = true;
}
void Display() {
cout << "Book Details " << endl
<< "Book Title : " << Title << endl
<< "Book Arthor : " << Arthor << endl
<< "Avalability : " << (Avalibility ? "Avalable" : "Not
Avalable") << endl << endl;
}
};
class Arthor {
string Name;
string Biography;
vector<Book> Written_Books;
public:
Arthor() {}
Arthor(string name, string bio = "N/A") :Name(name), Biography(bio) {}
void Arthor_List(Book B) {
if (Find(B) == -1) {
Written_Books.push_back(B);
}
else
cout << "Book " << B.get_Title() << " already Added to Author
List" << endl << endl;
}
string get_Name() {
return Name;
}
int Find(Book B) {
for (size_t i = 0; i < Written_Books.size(); ++i) {
if (Written_Books[i].get_Title() == B.get_Title()) {
return i;
}
}
return -1;
}
void Display() {
cout << "Arthor Details " << endl
<< "Name : " << Name << endl
<< "Biography : " << Biography << endl
<<"No of Books Writteen : "<<Written_Books.size()<<endl;
if (Written_Books.size() != 0) {
cout << "List of Books Written " << endl;
for (int i = 0; i < Written_Books.size(); ++i) {
int k = i + 1;
cout << k << ") " << Written_Books[i].get_Title() << endl
<< endl;
}
}
else
cout << "No Book Written" << endl << endl;
}
};
class Borrower {
string Name;
int Borrower_id;
vector<Book> Borrow_Books;
public:
Borrower() :Borrower_id(111) {}
Borrower(string name, int id) :Name(name), Borrower_id(id) {}
string get_Name() { return Name; }
int get_Borrower_id() { return Borrower_id; }
void Borrow(Book &B) {
if (B.get_Avalibility()) {
Borrow_Books.push_back(B);
B.Borrow_Book();
}
else
cout << "Book not Avalable" << endl;
}
int Find(Book &B) {
for (size_t i = 0; i < Borrow_Books.size(); ++i) {
if (Borrow_Books[i].get_Title() == B.get_Title()) {
return i;
}
}
return -1;
}
void Return(Book B) {
if (Find(B) != -1) {
Borrow_Books.erase(Borrow_Books.begin() + Find(B));
B.Return_Book();
}
else
cout << "This Book is not Borroweb by the user" << endl << endl;
}
void Display() {
cout << "Borower Details " << endl
<< "Name : " << Name << endl
<< "Biography : " << Borrower_id << endl;
if (Borrow_Books.size() != 0) {
cout << "List of Books Borrowed : " << endl;
for (size_t i = 0; i < Borrow_Books.size(); ++i) {
int k = i + 1;
cout << k << ") " << Borrow_Books[i].get_Title() << endl <<
endl;
}
}
else
cout << "No Book Borrowed" << endl;
}
};
void Borrow_Book(vector<Borrower> &B, vector<Book> &A) {
bool found;
do {
int id;
found = false;
string Title;
cout << "Enter Borrower id: ";
cin >> id;
cout << "Enter Book Title you want to borow: ";
cin >> Title;
for (int i = 0; i < B.size(); i++) {
if (B[i].get_Borrower_id() == id) {
for (int j = 0; j < A.size(); j++) {
if (A[j].get_Title() == Title) {
found = true;
B[i].Borrow(A[j]);
break;
}
}
}
}
} while (!found);
}
void Return_Book(vector<Borrower>& B, vector<Book>& A) {
bool found;
do {
int id;
found = false;
string Title;
cout << "Enter Borrower id: ";
cin >> id;
cout << "Enter Book Title you want to return: ";
cin >> Title;
for (int i = 0; i < B.size(); i++) {
if (B[i].get_Borrower_id() == id) {
for (int j = 0; j < A.size(); j++) {
if (A[j].get_Title() == Title) {
found = true;
B[i].Return(A[j]);
break;
}
}
}
}
} while (!found);
}
Book Create_Book() {
string Title;
string A;
string isbn;
cout << "Enter Book Title: ";
cin >> Title;
cout << "Enter Book Arthor: ";
cin >> A;
cout << "Enter Book ISBN: ";
cin >> isbn;
Book B(Title, A, isbn);
cout << B.get_Title() << " successfully Added in Library" << endl;
B.Display();
cout << endl;
return B;
}
Borrower Create_Borrower_acc() {
static int ID = 241000;
string name;
cout << "Enter Your Name: ";
cin >> name;
Borrower B(name, ID);
cout << name << " your Acc successfully created" << endl
<< "Your Browwer Id is " << ID << endl << endl;
++ID;
return B;
}
Arthor Assign_Aurthor(Book B) {
Arthor A(B.get_Arthor());
A.Arthor_List(B);
return A;
}
int Find(vector<Arthor> A, Book B) {
for (int j = 0; j < A.size(); j++) {
if (B.get_Arthor() == A[j].get_Name()) {
A[0].Arthor_List(B);
return j;
}
}
return -1;
}
int main() {
vector<Book> Books;
vector<Arthor> Arthors;
vector<Borrower> Borrowers;
int choice;
cout << "WELCOME TO LIBRAYR" << endl << endl << endl;
do {
cout << "Press 1 to Add a new Book to Library" << endl
<< "Press 2 to Create a Borrower Account in Library" << endl
<<"Press 3 to display all the Books details in the Library" <<
endl
<< "Press 4 to display all the aurthors details whose Books are
present in the library" << endl
<< "Press 5 to see all Borrowers Details" << endl
<< "Press 6 to Borrow a Book" << endl
<< "Press 7 to Return a Book" << endl
<< "Press 8 to Exit the Library" << endl;
bool error = 0;
cin >> choice;
switch (choice) {
case 1:
Books.push_back(Create_Book());
if (Find(Arthors, Books[Books.size() - 1]) == -1) {
Arthors.push_back(Assign_Aurthor(Books[Books.size() - 1]));
}
else {
Arthors[Find(Arthors, Books[Books.size() -
1])].Arthor_List(Books[Books.size() - 1]);
}
break;
case 2:
Borrowers.push_back(Create_Borrower_acc());
break;
case 3:
if (Books.size() != 0) {
for (int i = 0; i < Books.size(); i++) {
cout << i+1 << ")";
Books[i].Display();
cout << endl;
}
}
else
cout << "There are no books in the library"<<endl;
break;
case 4:
for (int i = 0; i < Arthors.size(); i++) {
cout << i + 1 << ")";
Arthors[i].Display();
cout << endl;
}
break;
case 5:
for (int i = 0; i < Borrowers.size(); i++) {
cout << i + 1 << ")";
Borrowers[i].Display();
cout << endl;
}
break;
case 6:
Borrow_Book(Borrowers, Books);
break;
case 7:
Return_Book(Borrowers, Books);
break;
case 8:
cout << "Thanks For visiting Us" << endl;
break;
default:
cout << "Invalid Input";
}
} while (choice!=8);