Create a class BookData having following members:
private Book book[ ] – Array of type Book
private int index
public void setBook(int bookNo, String title, String publication, String author, float price, String type)
method should create an object at index position starting from 0. public void printBooks( ) – method to print all book details in an array
public String searchByNo ( int bookNo ) – method to return book details of
book whose book no passed as parameter
public String searchByAuthor ( String author ) – method to return all book
names of an author whose name passed as parameter
#include<iostream>
#include<string>
using namespace std;
class Book{
private:
int bookNo;
string title;
string publication;
string author;
float price;
string type;
public:
void setBookDetails(int n, string t, string pu, string a, float pr, string ty){
bookNo = n;
title = t;
publication = pu;
author = a;
price = pr;
type = ty;
}
void displayBook(){
cout<<"Book No: "<<bookNo<<"\nBook Title: "<<title<<"\nBook Publication: "<<publication<<endl;
cout<<"Book Author: "<<author<<"\nBook Price: "<<price<<"\nBook Type: "<<type<<endl;
}
string BookDetails(){
string finalBook1 = "Book No: "+to_string(bookNo)+"\nBook Title: "+title+"\nBook Publication: "+publication;
string s = "Book Author: "+author+"\nBook Price: "+to_string(price)+"\nBook Type: "+type;
return finalBook1 + s;
}
int getBookNo(){
return bookNo;
}
string getAuthor(){
return author;
}
};
class BookData{
private:
int x=2;
Book book[2]; //Kindly, put a valid size of the book array
int index;
public:
void setBook(int bookNo, string title, string publication, string author, float price, string type){
for(int i=0; i<x; i++){
cout<<"Enter details for book "<<(i+1)<<endl;
cout<<"Enter the Book No\n";
cin>>bookNo;
cout<<"Enter the Book Title\n";
cin>>title;
cout<<"Enter the Book Publication\n ";
cin>>publication;
cout<<"Enter the Book Author\n";
cin>>author;
cout<<"Enter the Book Price\n";
cin>>price;
cout<<"Enter the Book Type\n";
cin>>type;
book[i].setBookDetails(bookNo,title,publication,author,price, type);
}
}
void printBooks( ) {
for(int i=0; i<x; i++){
cout<<"Details for Book "<<(i+1)<<endl;
book[i].displayBook();
}
}
string searchByNo ( int bookNo ){
string finalBook ;
for(int i=0; i<x; i++){
if(book[i].getBookNo()==bookNo){
finalBook="Books details are\n"+book[i].BookDetails();
}
}
return finalBook ;
}
string searchByAuthor( string author ) {
string finalBook ;
for(int i=0; i<x; i++){
if(book[i].getAuthor()==author){
finalBook="Books details are\n"+book[i].BookDetails();
}
}
return finalBook ;
}
};
int main(){
int bookNo;
string title;
string publication;
string author; float price;
string type;
BookData data;
data.setBook(bookNo,title,publication,author,price, type);
//data.printBooks();
int number;
cout<<"Search the book by book number\n";
cout<<"Enter the book number\n";
cin>>number;
cout<<data.searchByNo(number);
cout<<"\n\n";
string a;
cout<<"Search the book by book author\n";
cout<<"Enter the book author\n";
cin>>a;
cout<<data.searchByAuthor(a);
}
Comments
Leave a comment