In: Computer Science
C++ Question
The aim of this assignment is to design and implement a computerized “Library Management System”. The system will be used in the back-office to manage the books in the library catalog and to keep track of the various users (borrowers) of the library. The system provides the following key functionalities:
Books are characterized by a call number, a title, and a flag that indicates whether the book is currently ‘on-shelf’ (in the library) or ‘on-loan’.
-Adding and removing books to and from the catalog.
-Add 10 books to the library
#include <iostream>
#include <vector>
#define ON_SHELF 1
#define ON_LOAN 2
using namespace std;
class Book
{
private:
int callNumber;
string title;
int flag;
public:
Book()
{
this->callNumber = 0;
this->title = "";
this->flag = 0;
}
Book(int callNumber, string title)
{
this->callNumber = callNumber;
this->title = title;
this->flag = ON_SHELF;
}
int getCallNumber(){return this->callNumber;}
string getTitle(){return this->title;}
int getFlag(){return this->flag;}
void setFlag(int flag)
{
this->flag = flag;
}
string getStatus()
{
if(getFlag() == ON_SHELF)
return "On Shelf";
else
return "Borrowed";
}
};
void printMenu();
void addBook(vector<Book> );
void borrowBook(vector<Book> );
void removeBook(vector<Book> );
void listAllBooks(vector<Book> );
int main()
{
int choice;
vector<Book> books;
// initially 8 books are added
books.push_back(Book(190, "A Clokwork Orange"));
books.push_back(Book(456, "The Elephant Tree"));
books.push_back(Book(110, "3 Mistakes Of My Life"));
books.push_back(Book(167, "The Zombie Room"));
books.push_back(Book(971, "Requiem for a Dream"));
books.push_back(Book(23, "A Splendid Thousand Suns"));
books.push_back(Book(846, "And There Were None"));
books.push_back(Book(287, "The Grapes Of Wrath"));
cout << endl << "<< WELCOME TO THE LIBRARY MANAGEMENT SYSTEM >>\n----------------------------------------------" << endl;
do
{
printMenu();
cin >> choice;
switch(choice)
{
case 1:
{
addBook(books);
break;
}
case 2:
{
borrowBook(books);
break;
}
case 3:
{
removeBook(books);
break;
}
case 4:
{
listAllBooks(books);
break;
}
case 5:
{
cout << endl << "Thank you!\nGood Bye.\n";
exit(0);
}
default:
cout << endl << "Invalid choice" << endl;
}
}while(choice != 5);
}
void printMenu()
{
cout << "1. Add a book\n2. Borrow a book\n3. Remove a book\n4. List all books\n5. Exit\n\nEnter choice: ";
}
void addBook(vector<Book> books)
{
int callNum;
string title;
cout << "Enter call number: ";
cin >> callNum;
cin.ignore();
bool found = false;
for(Book book : books)
{
if(book.getCallNumber() == callNum)
{
found = true;
break;
}
}
if(found)
cout << endl << "Book with the same call number already exists!" << endl << endl;
else
{
cout << "Enter title: ";
getline(cin, title);
books.push_back(Book(callNum, title));
cout << endl << "Book with title " << title << " is added successfully!" << endl << endl;
listAllBooks(books);
}
}
void borrowBook(vector<Book> books)
{
int callNum;
cout << "Enter the call number of the book: ";
cin >> callNum;
bool found = false;
int index = 0;
for(int i = 0; i < books.size(); i++)
{
if(books[i].getCallNumber() == callNum)
{
found = true;
index = i;
break;
}
}
books[index].setFlag(ON_LOAN);
if(!found)
cout << endl << "No books found for call number: " << callNum << endl;
else
{
cout << endl << "Book borrowed:" << endl << "Title: " << books[index].getTitle() << ", Status: " << books[index].getStatus() << endl << endl;
listAllBooks(books);
}
}
void removeBook(vector<Book> books)
{
int callNum;
cout << "Enter the call number of the book: ";
cin >> callNum;
bool found = false;
int index = 0;
for(int i = 0; i < books.size(); i++)
{
if(books[i].getCallNumber() == callNum)
{
found = true;
index = i;
break;
}
}
if(!found)
cout << endl << "No books found for call number: " << callNum << endl << endl;
else
{
Book book = books[index];
books.erase(books.begin() + index);
cout << endl << "Book deleted successfully:" << endl << "Title: " << book.getTitle() << endl << endl;
listAllBooks(books);
}
}
void listAllBooks(vector<Book> books)
{
cout << endl << "*** ALL BOOKS ***\n------------------" << endl;
for(Book book : books)
{
cout << "Title: " << book.getCallNumber() << ", Title: " << book.getTitle() << ", Status: " << book.getStatus() << endl;
}
cout << endl;
}
***************************************************************** SCREENSHOT **********************************************************