Library Managment system project c++ using 4 classes
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Item{
protected:
int borrowed;
string name;
string borrowedBy;
string year;
int quantity;
public:
Item() {
this->borrowed = 0;
}
Item(int borrowed,string name,string borrowedBy,string year,int quantity) {
this->borrowed = borrowed;
this->name = name;
this->borrowedBy = borrowedBy;
this->year = year;
this->quantity = quantity;
}
virtual void displayDetails()const=0{
cout<<"Name: "<<name<<endl;
if(this->borrowed==0){
cout<<"Borrowed by: NONE"<<endl;
}else{
cout<<"Borrowed by: "<<borrowedBy<<endl;
}
cout<<"Year: "<<year<<endl;
cout<<"Quantity: "<<quantity<<endl;
}
void borrowItem(string borrowedBy){
this->borrowed =1;
this->borrowedBy =borrowedBy;
this->quantity--;
}
void returnItem(){
this->borrowed=0;
this->borrowedBy="NONE";
this->quantity++;
}
int getQuantity(){
return quantity;
}
string getBorrowerName(){
return borrowedBy;
}
};
class MusicAlbum:public Item{
private:
string artist;
string recordLabel;
public:
MusicAlbum():Item(){}
MusicAlbum(int borrowed,string name,string borrowedBy,string year,int quantity,string artist,string recordLabel):
Item(borrowed,name,borrowedBy,year,quantity){
this->artist = artist;
this->recordLabel = recordLabel;
}
void MusicAlbum::displayDetails()const {
cout << "Music album"<<endl;
Item::displayDetails();
cout<<"Artist: "<<artist<<endl;
cout<<"Record label: "<<recordLabel<<endl;
}
};
class Book:public Item{
private:
string author;
string publisher;
string edition;
public:
Book():Item(){}
Book(int borrowed,string name,string borrowedBy,string year,int quantity,string author,string publisher,string edition):
Item(borrowed,name,borrowedBy,year,quantity){
this->author = author;
this->publisher = publisher;
this->edition = edition;
}
//display information about Book
void displayDetails() const{
cout << "Book"<<endl;
Item::displayDetails();
cout<<"Author: "<<author<<endl;
cout<<"Publisher: "<<publisher<<endl;
cout<<"Edition: "<<edition<<endl;
}
};
class Film:public Item{
private:
string director;
string language;
public:
Film():Item(){}
Film(int borrowed,string name,string borrowedBy,string year,int quantity,string director,string language):
Item(borrowed,name,borrowedBy,year,quantity){
this->director = director;
this->language = language;
}
void displayDetails()const{
cout << "Film"<<endl;
Item::displayDetails();
cout<<"Director: "<<director<<endl;
cout<<"Language: "<<language<<endl;
}
};
int mainMenu();
void addItem(vector<Item*>* items);
void deleteSelectedItem(vector<Item*>* items);
void displayAllItems(vector<Item*> items);
void borrowItem(vector<Item*>* items);
void returnItem(vector<Item*>* items);
int searchItemByBorrower(vector<Item*> items,string borrowerName);
int main(){
vector<Item*> items;
int ch=-1;
while(ch!=6){
ch=mainMenu();
switch(ch){
case 1:
addItem(&items);
break;
case 2:
deleteSelectedItem(&items);
break;
case 3:
displayAllItems(items);
break;
case 4:
borrowItem(&items);
break;
case 5:
returnItem(&items);
break;
case 6:
//exit
break;
default:
cout<<endl<<"Select correct menu item.\n\n";
break;
}
}
return 0;
}
//This function displays main menu and allows the user to select menu item.
int mainMenu(){
int choice=-1;
cout<<"1. Add new item"<<endl;
cout<<"2. Delete selected item"<<endl;
cout<<"3. Display all items"<<endl;
cout<<"4. Borrow the item"<<endl;
cout<<"5. Return the item"<<endl;
cout<<"6. Exit"<<endl;
cout<<"Your choice: ";
cin>>choice;
return choice;
}
//This method allows to add new item to database
void addItem(vector<Item*>* items){
int ch=-1;
while(ch!=4){
ch=-1;
cout<<"1. Add new music album"<<endl;
cout<<"2. Add new book"<<endl;
cout<<"3. Add new film"<<endl;
cout<<"4. Exit to main menu"<<endl;
while(ch<1 || ch>4){
cout<<"Your choice: ";
cin>>ch;
}
int borrowed;
string name;
string year;
int quantity;
//MusicAlbum
string artist;
string recordLabel;
//book
string author;
string publisher;
string edition;
//film
string director;
string language;
getline(cin,name);
if(ch==1){
cout<<"Enter name of music album: ";
getline(cin,name);
cout<<"Enter year of music album: ";
getline(cin,year);
cout<<"Enter number of copies: ";
cin>>quantity;
cout<<"Enter artist of music album: ";
getline(cin,artist);
getline(cin,artist);
cout<<"Enter record label of music album: ";
getline(cin,recordLabel);
MusicAlbum* musicAlbum=new MusicAlbum(0,name,"",year,quantity,artist,recordLabel);
items->push_back(musicAlbum);
}
if(ch==2){
cout<<"Enter name of book: ";
getline(cin,name);
cout<<"Enter year of book: ";
getline(cin,year);
cout<<"Enter number of copies: ";
cin>>quantity;
cout<<"Enter author of book: ";
getline(cin,author);
getline(cin,author);
cout<<"Enter publisher of book: ";
getline(cin,publisher);
cout<<"Enter edition of book: ";
getline(cin,edition);
Book* book=new Book(0,name,"",year,quantity,author,publisher,edition);
items->push_back(book);
}
if(ch==3){
cout<<"Enter name of film: ";
getline(cin,name);
cout<<"Enter year of film: ";
getline(cin,year);
cout<<"Enter number of copies: ";
cin>>quantity;
cout<<"Enter director of film: ";
getline(cin,director);
getline(cin,director);
cout<<"Enter language of film: ";
getline(cin,language);
Film* film=new Film(0,name,"",year,quantity,director,language);
items->push_back(film);
}
}
}
//This method deletes selected item from database
void deleteSelectedItem(vector<Item*>* items){
if(items->size()>0){
int index=0;
displayAllItems(*items);
while(index<1 || index>items->size()){
cout<<"Select item to delete: ";
cin>>index;
}
index--;
items->erase(items->begin() + index);
cout<<endl<<"Selected item has been deleted!"<<endl<<endl;
}else{
cout<<endl<<"No items in database!"<<endl<<endl;
}
}
//This method allows to borrow item
void borrowItem(vector<Item*>* items){
if(items->size()>0){
int index=0;
displayAllItems(*items);
while(index<1 || index>items->size()){
cout<<"Select item to borrow: ";
cin>>index;
}
index--;
if(items->at(index)->getQuantity()>0){
string borrowerName;
cout<<"Enter name of library member: ";
getline(cin,borrowerName);
getline(cin,borrowerName);
items->at(index)->borrowItem(borrowerName);
cout<<endl<<"Selected item has been borrowed!"<<endl<<endl;
}else{
cout<<endl<<"No items in database! Number of copies = 0!!!"<<endl<<endl;
}
}else{
cout<<endl<<"No items in database!"<<endl<<endl;
}
}
//This method allows to return item by borrower name
void returnItem(vector<Item*>* items){
if(items->size()>0){
string borrowerName;
cout<<"Enter the name of library member: ";
getline(cin,borrowerName);
getline(cin,borrowerName);
int index=searchItemByBorrower(*items,borrowerName);
if(index!=-1){
items->at(index)->returnItem();
cout<<endl<<"Selected item has been returned!"<<endl<<endl;
}else{
cout<<endl<<"Selected borrower has not items!!!"<<endl<<endl;
}
}else{
cout<<endl<<"No items in database!"<<endl<<endl;
}
}
//This method allows to search item index by borrower name
int searchItemByBorrower(vector<Item*> items,string borrowerName){
if(items.size()>0){
for(int i=0;i<items.size();i++){
if(items[i]->getBorrowerName().compare(borrowerName)==0){
return i;//return index of item
}
}
}
return -1;
}
//This methods displays all items in database
void displayAllItems(vector<Item*> items){
if(items.size()>0){
cout<<endl;
for(int i=0;i<items.size();i++){
cout<<"Item: "<<(i+1)<<endl;
items[i]->displayDetails();
cout<<"----------------------------------------"<<endl;
}
}else{
cout<<endl<<"No items in database!"<<endl<<endl;
}
}
Comments
Leave a comment