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

the user will provide a keyword and your program will report all books that have

ID: 3622050 • Letter: T

Question


the user will provide a keyword and your program will report all
books that have the keyword as part of their title. Start by using the UML digram below to make a Book class.

Book

- title: string
- price: double



Book
- price: double
+ Book():
+ setTitle(s: string): void
+ setPrice(p: double): void
+ getTitle(): string
+ getPrice(): double
+ hasKeyword(key: string): bool



The constructor should call the two mutator functions passing in “None” for
the title and 0 for the price. The setTitle member function should assign its parameter value to title. The setPrice member function should assign its parameter value to price if it is not negative. If it is negative, it should assign zero
to price. The getTitle member function should return the title and the
getPrice member function should return the price.

The hasKeyword member function should return true if the parameter is
part of the title. If it is not, it should return false. Use the find member function
of the string object to determine if the keyword (the parameter to the
hasKeyword member function) is part of the title (see page 597). Note that the find member function returns string::npos if it is not able to find the word in the string. To make your program logic simple, you can assume that all book titles will be originally entered with just the first letter of each word capitalized. Furthemore, you can assume that keyword(s) entered will also follow
this rule.




In the main function, ask the user how many books they would like the
program to process. Use this value to dynamically create in the main function, an
array of Book objects. Call a stand-alone function with the following prototype:

void Initalize(Book * ptr, int size);

This function will allow the user to input titles and prices for the books. Use
the mutator functions to set the attributes with the values supplied by the user.

In main, allow the user to enter the keyword(s). Using the hasKeyword member function, determine which book objects have titles with the user supplied keyword. Print out the title and price of the book(s). If no book matches
the keyword, print out a message letting the user know this information.

Explanation / Answer

Dear, Here is the code // book.cpp : Defines the entry point for the console application. // #include "stdafx.h" // bookType.h #include #include using namespace std; class Book { public : Book(); void setTitle( string ); void setPrice( double ); string getTitle(); double getPrice(); bool hasKeyword(string key); private: string title; double price; }; // end class definition of bookType Book::Book() { title = ""; price = 0; } void Book::setTitle( string myTitle ) { title = myTitle; } // end function setTitle string Book::getTitle() { return title; } // end function getTitle void Book::setPrice( double myPrice ) { price = myPrice; } // end function setPrice double Book::getPrice() { return price; } // end function getPrice bool Book::hasKeyword(string key) { int x=0; int pos=title.find(key, x); if(pos>=0) return true; else return false; } void Initialize(Book *ptr,int size); void main() { int noBooks; string keyword; // instantiate 10 objects of type bookType Book *myBooks; coutnoBooks; myBooks=new Book[noBooks]; Initialize(myBooks,noBooks); coutkeyword; for(int i=0;i