Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code in C++ Part I – Build a Book Class Build a Book class that has the followin

ID: 3696776 • Letter: C

Question

Code in C++

Part I – Build a Book Class Build a Book class that has the following properties: ISBN, name, author, publisher, price and number of pages. Your class should have 2 constructors, set and get functions as well as a display function. You may want to build a simple program with main to test out this Book class. Create one object, fill it with data and print out that data.

Part II – Build a Program to Add new Books to a File of Books Using the book class we built in Part I, create a program that when run will ask a user to input all pieces of data for a book, the program will then put this data into the Book object. Lastly, the program will store the data for this book in a file called “Books.txt”. Use a loop to ask the user if they want to enter another book or not.

Part III – Build a Program to Display all the Books in a File Using the book class we built in Part I, create another program that when run, will display all the books that are stored in the File “Books.txt”.

Explanation / Answer

Part I:


#include<iostream>
#include<fstream>

using namespace std;

class Book{
  
   private:
       string isbn;
       string name;
       string author;
       string publisher;
       double price;
       int pages;
      
   public:
       // constructor that takes isbn number
       Book(string isbn){
           this->isbn = isbn;
          
       }
      
       // constructor that takes all parameters
       Book(string isbn,string name, string author, string publisher,
               double price, int pages){
           this->isbn = isbn;
           this->name = name;
           this->author = author;
           this->publisher = publisher;
           this->price = price;
           this->pages = pages;
       }
      
       // getter setter for all members
       void setIsbn(string isbn){
           this->isbn = isbn;
       }
      
       void setName(string name){
           this->name = name;
       }
      
       void setAuthor(string author){
           this->author = author;
       }
      
       void setPrice(double price){
           this->price = price;
       }
      
       void setPages(int pages){
           this->pages= pages;
       }
      
       void setPublisher(string publisher){
           this->publisher = publisher;
       }
      
       string getIsbn(){
           return isbn;
       }
      
       string getName(){
           return name;
       }
      
       string getAuthor(){
           return author;
       }
      
       string getPublisher(){
           return publisher;
       }
      
       double getPrice(){
           return price;
       }
      
       int getPages(){
           return pages;
       }
      
       // display function
       void display(){
           cout<<"ISBN No.: "<<isbn<<endl;
           cout<<"Name: "<<name<<endl;
           cout<<"Author: "<<author<<endl;
           cout<<"Publisher: "<<publisher<<endl;
           cout<<"Number of Pages: "<<pages<<endl;
           cout<<"Price: "<<price<<endl<<endl;
       }
};


int main(){
  
   Book b1("IND123"); // creating a book object with isbn number
  
   // setting other data members
   b1.setName("Network");
   b1.setAuthor("Robert Krus");
   b1.setPublisher("Lizard Sar");
   b1.setPages(300);
   b1.setPrice(543.12);
  
   // displaying
   b1.display();
  
   return 0;
}

/*

Output:

ISBN No.: IND123
Name: Network
Author: Robert Krus
Publisher: Lizard Sar
Number of Pages: 300
Price: 543.12

*/

Part II:


#include<iostream>
#include<fstream>

using namespace std;

class Book{
  
   private:
       string isbn;
       string name;
       string author;
       string publisher;
       double price;
       int pages;
      
   public:
       // constructor that takes isbn number
       Book(string isbn){
           this->isbn = isbn;
          
       }
      
       // constructor that takes all parameters
       Book(string isbn,string name, string author, string publisher,
               double price, int pages){
           this->isbn = isbn;
           this->name = name;
           this->author = author;
           this->publisher = publisher;
           this->price = price;
           this->pages = pages;
       }
      
       // getter setter for all members
       void setIsbn(string isbn){
           this->isbn = isbn;
       }
      
       void setName(string name){
           this->name = name;
       }
      
       void setAuthor(string author){
           this->author = author;
       }
      
       void setPrice(double price){
           this->price = price;
       }
      
       void setPages(int pages){
           this->pages= pages;
       }
      
       void setPublisher(string publisher){
           this->publisher = publisher;
       }
      
       string getIsbn(){
           return isbn;
       }
      
       string getName(){
           return name;
       }
      
       string getAuthor(){
           return author;
       }
      
       string getPublisher(){
           return publisher;
       }
      
       double getPrice(){
           return price;
       }
      
       int getPages(){
           return pages;
       }
      
       // display function
       void display(){
           cout<<"ISBN No.: "<<isbn<<endl;
           cout<<"Name: "<<name<<endl;
           cout<<"Author: "<<author<<endl;
           cout<<"Publisher: "<<publisher<<endl;
           cout<<"Number of Pages: "<<pages<<endl;
           cout<<"Price: "<<price<<endl<<endl;
       }
};


int main(){
  
   ofstream writeBook("Books.txt"); // opening a write stream
   char ch = 'y';
   string isbn;
   string name;
   string author;
   string publisher;
   double price;
   int pages;
  
   while(ch == 'y'){
      
       cout<<"Enter book ISBN No.: ";
       cin>>isbn;
       cin.ignore();
       cout<<"Enter name of book: ";
       getline(cin, name);
       cout<<"Enter author name: ";
       getline(cin, author);
       cout<<"Enter publisher name: ";
       getline(cin, publisher);
       cout<<"Enter number of pages: ";
       cin>>pages;
       cout<<"Enter price: ";
       cin>>price;
      
       // creating book object with all user data
       Book b(isbn, name,author,publisher,price,pages);
      
       // writing to file (each data members in separate line)
       writeBook<<b.getIsbn()<<endl;
       writeBook<<b.getName()<<endl;
       writeBook<<b.getAuthor()<<endl;
       writeBook<<b.getPublisher()<<endl;
       writeBook<<b.getPrice()<<endl;
       writeBook<<b.getPages()<<endl;
      
       cout<<"Do you want to store more book details .. press y, any other to stop..";
       cin>>ch;
   }
  
   return 0;
}

/*

Sample run:

Enter book ISBN No.: IND123
Enter name of book: Computer Network
Enter author name: Robert Krush
Enter publisher name: GMT HIll
Enter number of pages: 340
Enter price: 432
Do you want to store more book details .. press y, any other to stop..n

After this, Books.txt will be created in same folder where your programe is there.

Content of Books.txt for me:

IND123
Computer Network
Robert Krush
GMT HIll
432
340

*/

Part III:


#include<iostream>
#include<fstream>

using namespace std;

class Book{
  
   private:
       string isbn;
       string name;
       string author;
       string publisher;
       double price;
       int pages;
      
   public:
       // constructor that takes isbn number
       Book(string isbn){
           this->isbn = isbn;
          
       }
      
       // constructor that takes all parameters
       Book(string isbn,string name, string author, string publisher,
               double price, int pages){
           this->isbn = isbn;
           this->name = name;
           this->author = author;
           this->publisher = publisher;
           this->price = price;
           this->pages = pages;
       }
      
       // getter setter for all members
       void setIsbn(string isbn){
           this->isbn = isbn;
       }
      
       void setName(string name){
           this->name = name;
       }
      
       void setAuthor(string author){
           this->author = author;
       }
      
       void setPrice(double price){
           this->price = price;
       }
      
       void setPages(int pages){
           this->pages= pages;
       }
      
       void setPublisher(string publisher){
           this->publisher = publisher;
       }
      
       string getIsbn(){
           return isbn;
       }
      
       string getName(){
           return name;
       }
      
       string getAuthor(){
           return author;
       }
      
       string getPublisher(){
           return publisher;
       }
      
       double getPrice(){
           return price;
       }
      
       int getPages(){
           return pages;
       }
      
       // display function
       void display(){
           cout<<"ISBN No.: "<<isbn<<endl;
           cout<<"Name: "<<name<<endl;
           cout<<"Author: "<<author<<endl;
           cout<<"Publisher: "<<publisher<<endl;
           cout<<"Number of Pages: "<<pages<<endl;
           cout<<"Price: "<<price<<endl<<endl;
       }
};


int main(){
  
   ifstream readBook("Books.txt");
   string isbn;
   string name;
   string author;
   string publisher;
   double price;
   int pages;
  
   while(!readBook.eof()){
       getline(readBook, isbn);
       if(isbn.empty())
           break;
       getline(readBook,name );
       getline(readBook, author);
       getline(readBook, publisher);
       readBook>>pages;
       readBook>>price;
      
       // creating book object with all user data
       Book b(isbn, name,author,publisher,price,pages);
      
       //displaying
       b.display();
          
   }
  
   return 0;
}

/*

Sample run:

ISBN No.: IND123
Name: Computer Network
Author: Robert Krush
Publisher: GMT HIll
Number of Pages: 432
Price: 340

*/