Write a c++ program for a library automation which gets ISBN number, name, author and publication year of the books in the library. The status will be filled by the program as follows:
if publication year before 1985 then the book will not be added in the list. The information about the books should be stored inside a linked list. The program should have a menu and the user inserts, displays and deletes the elements from the menu selecting options.
The following menu should be used in the program
Press 1. to insert a book
Press 2. to display the book list
Press 3. to delete a book from list
Answers
Explanation:
if publication year before 1985 then the book will not be added in the list. The information about the books should be stored inside a linked list. The program should have a menu and the user inserts, displays and deletes the elements from the menu selecting options.
The following menu should be used in the program
Press 1. to insert a book
Press 2. to display the book list
Press 3. to delete a book from list
Program:
#include<bits/stdc++.h>
#include<iostream>
#include<string.h>
using namespace std;
class Node
{
public:
string ISBN, Name, Author;
int Year;
Node* next;
Node()
{
ISBN = "";
Name = "";
Author = "";
Year = 0;
next = NULL;
}
Node(string i , string n , string a , int y)
{
ISBN = i;
Name = n;
Author = a;
Year = y;
}
};
class SinglyLinkedList
{
public:
Node* head;
SinglyLinkedList()
{
head = NULL;
}
SinglyLinkedList(Node *n)
{
head = n;
}
Node* nodeExist(string n)
{
Node* temp = NULL;
Node* ptr = head;
while(ptr != NULL)
{
if(n.compare(ptr->Name) == 0)
{
temp = ptr;
}
ptr = ptr->next;
}
return temp;
}
Node* publication_year(int y)
{
Node* temp = NULL;
if(y < 1985)
{
temp = head;
}
return temp;
}
void prependNode(Node* n)
{
if(nodeExist(n->Name) != NULL)
{
cout<<"Node Already exist with the same book name : "<<n->Name<<". Add another node with different book name."<<endl;
}
else if(publication_year(n->Year) != NULL)
{
cout<<"Book cannto be added because publication year is before 1985"<<endl;
}
else
{
n->next = head;
head = n;
cout<<"Book Added"<<endl;
}
}
void deleteNode(string s)
{
if(head == NULL)
{
cout<<"Empty List! Nothing to be deleted"<<endl;
}
else
{
if(s.compare(head->Name) == 0)
{
head = head->next;
cout<<"Book deleted from the list"<<endl;
}
else
{
Node* temp = NULL;
Node* prevptr = head;
Node* currentptr = head->next;
while(currentptr != NULL)
{
if(s.compare(currentptr->Name) == 0)
{
temp = currentptr;
currentptr = NULL;
}
else
{
prevptr = prevptr->next;
currentptr = currentptr->next;
}
}
if(temp != NULL)
{
prevptr->next = temp->next;
cout<<"Book deleted from the list"<<endl;
}
else
{
cout<<"Book with such name does not exist"<<endl;
}
}
}
}
void display()
{
if(head == NULL)
{
cout<<"Empty List"<<endl;
}
else
{
cout<<endl<<"Books List "<<endl;
Node* temp = head;
while(temp != NULL)
{
cout<<temp->ISBN<<"\t"<<temp->Name<<"\t"<<temp->Author<<"\t"<<temp->Year<<endl;
temp = temp->next;
}
}
}
};
int main()
{
SinglyLinkedList s;
int option;
string isbn, name, author;
int year;
do
{
cout<<"\nWhat operation do you want to perform? Sellect Opton number.Enter 0 to exit."<<endl;
cout<<"Press 1. to insert a book"<<endl;
cout<<"Press 2. to display the book list"<<endl;
cout<<"Press 3. to delete a book from list"<<endl;
cin>>option;
Node* n1 = new Node();
switch(option)
{
case 0:
break;
case 1:
cout<<"Enter the book details \n";
cout<<"Enter ISBN : ";
getline(cin, isbn);
cout<<"Enter Book Name : ";
getline(cin,name);
cout<<"Enter Author Name : ";
getline(cin,author);
cout<<"Enter Publication Year : ";
cin>>year;
n1->ISBN = isbn;
n1->Name = name;
n1->Author = author;
n1->Year = year;
s.prependNode(n1);
break;
case 2:
s.display();
break;
case 3:
cout<<"Enter name of the book you want to delete : ";
getline(cin, name);
s. deleteNode(name);
break;
default:
cout<<"Invalid option"<<endl;
}
}
while(option != 0);
return 0;
}