Create a class Book <BOOK_ID,ISBN,TITLE,PRICE> as member variables. Create functions to read and write Book details.Overload a function SEARCH() to search a book based on ISBN and TITLE.Write a menu driven main(), opting user to key-in the search option as 1.Search by ISBN 2.Search by TITLE.
#include <iostream>
#include <string>
using namespace std;
class Book{
private:
int BOOK_ID;
int ISBN;
string TITLE;
float PRICE;
public:
void readData(){
cout<<"Enter book id: ";
cin>>BOOK_ID;
cout<<"Enter book ISBN: ";
cin>>ISBN;
cin.ignore();
cout<<"Enter book title: ";
getline(cin,TITLE);
cout<<"Enter book price: ";
cin>>PRICE;
}
void display(){
cout<<"The book id: "<<BOOK_ID<<"\n";
cout<<"The book ISBN: "<<ISBN<<"\n";
cout<<"The book title: "<<TITLE<<"\n";
cout<<"The book price: "<<PRICE<<"\n";
}
bool SEARCH(int ISBNTarget){
if(ISBN==ISBNTarget){
display();
return true;
}
return false;
}
bool SEARCH(string TITLETarget){
if(TITLE==TITLETarget){
display();
return true;
}
return false;
}
};
int main() {
int ch=1;
Book books[5];
for(int i=0;i<5;i++){
books[i].readData();
}
while(ch!=3){
cout<<"1. Search by ISBN\n";
cout<<"2. Search by TITLE\n";
cout<<"3. Exit\n";
cin>>ch;
if(ch==1){
int ISBN;
cout<<"Enter book ISBN to search: ";
cin>>ISBN;
for(int i=0;i<5;i++){
if(books[i].SEARCH(ISBN)){
break;
}
}
}else if(ch==2){
string title;
cout<<"Enter book title to search: ";
getline(cin,title);
for(int i=0;i<5;i++){
if(books[i].SEARCH(title)){
break;
}
}
}else{
}
}
system("pause");
return 0;
}
Comments
Leave a comment