In: Computer Science
A Bookstore Application C++
Design the class Book. Each object of the class Book can hold the following information about a book:
Include the member functions to perform the various operations on the objects of Book. For example, the typical operations that can be performed on the title are to show the title, set the title. Add similar operations for the publisher, ISBN , and authors. Add the appropriate constructors and a destructor (if one is needed).
The Functionalities available in BookstoreManager
Function | Time-complexity |
---|---|
isEmpty() : returns true if the array is empty, otherwise false | O(1) |
isFull(): returns true if the array is full, otherwise false | O(1) |
listSize(): prints the number of books in the array | O(1) |
print(): prints the content of the array | O(n) |
insert(Book): asks user to enter new book info, and it adds the book to the array in sorted order. If array is full, it’ll double the size of the array | O(n) |
remove(Book): asks user to enter ISBN info, and it removes the book from the array; shifts the other elements up in the array. Prints “Not Found” if search fails | O(n) |
removePublisher(string): asks user to enter publisher name, and it removes all the books with the same publisher from the array; shifts the other elements up in the array. Prints “Not Found” if search fails. | O(n) |
search(Book): asks user to enter ISBN or title, and prints the content of the book. Prints “Not Found”, if book is not found | O(logn) |
Sample Output:
user inputs are given in bold
true
Enter book title: C++: Programming Basics
Enter authors: Nathan Clark
Enter ISBN: 154296
Enter publisher: CreateSpace
Enter book title: Data Structures & Algorithm
Analysis in C++
Enter authors: Mark Weiss
Enter ISBN: 132847
Enter publisher: Pearson
Enter book title: Introduction to Programming Using
Python
Enter authors: Daniel Liang
Enter ISBN: 147231
Enter publisher: Pearson
Enter book title: Introduction to
Algorithms
Enter authors: Thomas H. Cormen , Charles E.
Leiserson
Enter ISBN: 189352
Enter publisher: MIT
Data Structures & Algorithm Analysis in C++
by Mark Weiss
132847
Pearson
Introduction to Programming Using Python
Daniel Liang
147231
Pearson
C++: Programming Basics
by Nathan Clark
154296
CreateSpace
Introduction to Algorithms
Thomas H. Cormen , Charles E. Leiserson
189352
MIT
Searching…
ISBN: 147231
Introduction to Programming Using Python
Daniel Liang
147231
Pearson
Removing…
ISBN: 154296
Data Structures & Algorithm Analysis in C++
by Mark Weiss
132847
Pearson
Introduction to Programming Using Python
Daniel Liang
147231
Pearson
Introduction to Algorithms
Thomas H. Cormen , Charles E. Leiserson
189352
MIT
Removing all books for a publisher
Publisher: Pearson
Introduction to Algorithms
Thomas H. Cormen , Charles E. Leiserson
189352
MIT
1
Starting Code:
int main() { | |
BookstoreManager bookstoreManager; | |
//prints true if the bookstore is empty | |
bookstoreManager.isEmpty(); | |
//insert 4 books | |
string title, authors, publisher; | |
int isbn; | |
for(int i=0;i<4;i++){ | |
cout<<"Enter book title:"; | |
cin>>title; | |
cout<<"Enter authors:"; | |
cin>>authors; | |
cout<<"Enter isbn:"; | |
cin>>isbn; | |
cout<<"Enter publisher:"; | |
cin>>publisher; | |
Book aBook(title, isbn, authors, publisher); | |
bookstoreManager.insert(aBook); | |
cout<<endl; | |
} | |
//print bookstoreB | |
bookstoreManager.print(); | |
//search for books | |
cout<<"Searching…"<<endl; | |
cout<<"ISBN:"; | |
cin>>isbn; | |
Book b2(isbn); | |
bookstoreManager.search(b2); | |
//remove a book | |
cout<<"Removing…"<<endl; | |
cout<<"ISBN:"; | |
cin>>isbn; | |
Book b1(isbn); | |
bookstoreManager.remove(b1); | |
//print bookstore | |
bookstoreManager.print(); | |
//remove books from a particular publisher | |
cout<<"Removing publisher"<<endl; | |
cout<<"Publisher:"; | |
cin>>publisher; | |
bookstoreManager.removePublisher(publisher); | |
//print bookstore | |
bookstoreManager.print(); | |
//prints the number of books | |
bookstoreManager.listSize(); | |
} |
See the code below.
#include<iostream>
#include<cstring>
using namespace std;
// a book class for the different variable.
class Book
{
public:
string title;
string authors;
string publisher;
int ISBN;
// i have used different constructor base on the input.
Book(string ttle,string author,string pub,int IS )
{
title = ttle;
authors = author;
publisher = pub;
ISBN = IS;
}
Book(Book b)
{
title = b.title;
authors = b.authors;
publisher = b.publisher;
ISBN = b.ISBN;
}
// when only isbn is entered.
Book(int b)
{
title = " ";
authors = " ";
publisher = " ";
ISBN = b.ISBN;
}
};
class BookstoreManager
{
Book *Bookarray;
// capacity denote full capacity
int idx;
// size if no of element present
int size;
int capacity;
public:
// Constructor
BookstoreManager()
{
idx=0;
size = 0;
// in the starting capacity is 1.
capacity =1;
Bookarray = new Book[capacity];
}
void print()
{
// using renge base for loop came in c++11
for(auto x:Bookarray)
cout<<x.title<<endl<<x.authors<<endl<<x.publisher<<endl<<x.ISBN<<endl;
}
bool isEmpty()
{
if(size==0)
return 1;
else
return 0;
}
int listSize()
{
return size;
}
bool isFull()
{
if(size== capacity)
return 1;
else
return 0;
}
void insert(Book book)
{
// first checking the capacity of the array.
if(size == capacity)
{
// double the size of the array.
Book* temp = new Book[2*capacity];
capacity = 2*capacity;
Book* t = Bookarray;
// copying the data from previous array to new array.
for(int i=0;i<=idx;i++)
temp[i]= Bookarray[i];
Bookarray = temp;
// delete the previous array.
delete [] t;
}
// inserting the data in sorted array is it just like insertion sort.
for(int i=size;i>=0;i--)
{
if(Bookarray[i].ISBN>book.ISBN)
{
Bookarray[i+1] = Bookarray[i];
}
else
{
size++;
idx++;
Bookarray[i]=book;
break;
}
}
}
void remove(book b)
{
int i=0;
int flag =0;
// searching the data present or not.
for(i=0;i<=idx;i++)
{
if(Bookarray[i].ISBN==b.ISBN)
{
flag =1;
break;
}
}
if(flag ==0)
{
cout<<"Not Found"<<endl;
}
else{
for(int j=i;j<=idx-1;j++)
Bookarray[j]=Bookarray[j+1];
idx--;
size--;
}
}
void removePublisher(string publisher)
{
int i=0;
int flag =0;
for(i=0;i<=idx;i++)
{
if(Bookarray[i].publisher==publisher)
{
for(int j=i;j<=idx-1;j++)
// shifting the data left side.
Bookarray[j]=Bookarray[j+1];
idx--;
size--;
flag =1;
}
}
if(flag ==0)
{
cout<<"Not Found"<<endl;
}
return ;
}
void search(book b)
{
for(int i=0;i<=size;i++)
{
if(Bookarray[i]==b.ISBN)
{
cout<<"ISBN:"<<Bookarray[i].ISBN<<endl;
cout<<Bookarray[i].title<<endl;
cout<<Bookarray[i].authors<<endl;
return;
}
}
cout<<"Not found"<<endl;
return ;
}
};
int main() {
BookstoreManager bookstoreManager;
//prints true if the bookstore is empty
bookstoreManager.isEmpty();
//insert 4 books
string title, authors, publisher;
int isbn;
for(int i=0;i<4;i++){
cout<<"Enter book title:";
cin>>title;
cout<<"Enter authors:";
cin>>authors;
cout<<"Enter isbn:";
cin>>isbn;
cout<<"Enter publisher:";
cin>>publisher;
Book aBook(title, isbn, authors, publisher);
bookstoreManager.insert(aBook);
cout<<endl;
}
//print bookstoreB
bookstoreManager.print();
//search for books
cout<<"Searching…"<<endl;
cout<<"ISBN:";
cin>>isbn;
Book b2(isbn);
bookstoreManager.search(b2);
//remove a book
cout<<"Removing…"<<endl;
cout<<"ISBN:";
cin>>isbn;
Book b1(isbn);
bookstoreManager.remove(b1);
//print bookstore
bookstoreManager.print();
//remove books from a particular publisher
cout<<"Removing publisher"<<endl;
cout<<"Publisher:";
cin>>publisher;
bookstoreManager.removePublisher(publisher);
//print bookstore
bookstoreManager.print();
//prints the number of books
bookstoreManager.listSize();
return 0;
}
// Please use prople flag for running this program on different c++ compiler. (c++11 etc)
// Here i have explained the code .
// you can modified the code according the your need.
// Please upvote if it is helpful.