Question

In: Computer Science

A Bookstore Application C++ Design the class Book. Each object of the class Book can hold...

A Bookstore Application C++

Design the class Book. Each object of the class Book can hold the following information about a book:

  • title,
  • authors,
  • publisher,
  • ISBN

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).

  • Write the definitions of the member functions of the class Book
  • Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide implementation for the following operations on books in the array with the given time-complexity
  • A starter code and a sample output are provided.
  • As shown in the sample output, books are kept sorted by ISBN. Each time a new book is inserted, it’ll be inserted in the sorted order.
  • You are not allowed to use any existing sorting and searching functions.

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();
}

Solutions

Expert Solution

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.


Related Solutions

Write a c++ class definition for an abstract data type describing a bookstore inventory. Each book...
Write a c++ class definition for an abstract data type describing a bookstore inventory. Each book has the following attributes: Book Title (character string); Book Author (character string); Book Price (Floating point number having two decimal places); Count of books on hand (int); The member functions are as follows: A constructor that is used to initialize all four elements of the structure to values inputted by the user; A function that displays in a readable tabular form the contents of...
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y),...
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y), where x and y are integers. Define methods to individually set the x and y coordinates of your new point and retrieve their values. Write an Objective-C program to implement your new class and test it (main section of the file). Your test program needs to create two instances of your class. Make sure your program test prints out the values that you have...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
Design an Inventory class that can hold information for an item in a retail store’s inventory.
C++! Becareful following the test case.7. Inventory ClassDesign an Inventory class that can hold information for an item in a retail store’s inventory.The class should have the following private member variables.Variable Name   DescriptionitemNumber   An int that holds the item’s number.quantity               An int that holds the quantity of the item on hand.cost    A double that holds the wholesale per-unit cost of the itemThe class should have the following public member functionsMember Function               Descriptiondefault constructor             Sets all the member variables...
Make in c++ this Programming Challenge 1. Design your own linked list class to hold a...
Make in c++ this Programming Challenge 1. Design your own linked list class to hold a series of integers. The class should have member functions for appending, inserting, and deleting nodes. Don’t forget to add a destructor that destroys the list. Demonstrate the class with a driver program. 2. List Print Modify the linked list class you created in Programming Challenge 1 to add a print member function. The function should display all the values in the linked list. Test...
C++ question: Design and implement your own linked list class to hold a sorted list of...
C++ question: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should have member functions to display the...
Must be in C#: 7. E-MAIL ADDRESS BOOK Create a Windows Forms Application with a class...
Must be in C#: 7. E-MAIL ADDRESS BOOK Create a Windows Forms Application with a class named PersonEntry. The PersonEntry class should have properties for a person’s name, e-mail address, and phone number. Also, create a text file that contains the names, e-mail addresses, and phone numbers for at least five people. When the application starts, it should read the data from the file and create a PersonEntry object for each person’s data. The PersonEntry objects should be added to...
Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
Create the logic for an application that instantiates an object of the ATM class. Prompt the...
Create the logic for an application that instantiates an object of the ATM class. Prompt the user for the ATM object data to display the user’s bank balance PROGRAMMING LOGIC AND DESIGN QUESTION JAVA LANGUAGE
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT