using the source code at the bottom of the page, use the following instructions
ID: 3907430 • Letter: U
Question
using the source code at the bottom of the page, use the following instructions to make the required adjustments.
Serendipity Booksellers Software Development
Project— Part 8: A Problem-Solving Exercise
For this chapter’s assignment you will add searching capabilities to the addBook, lookUpBook, editBook, and deleteBook functions.
1.Modify the addBook function
When a new book is added to the inventory, the program will search the bookTitle array for an element that is set to an empty string or the null terminator. If the element contains an empty string or a null terminator, it means there isn’t a string stored there. (Because the arrays are global, all their elements will be automatically initialized to zero.)
Once an empty element is found in the bookTitle array, the book’s data may be stored in each of the other arrays by using the same subscript. For instance, if the title of the book is stored in bookTitle[7], its ISBN number will be stored in isbn[7], its author’s name will be stored in author[7], its publisher’s name will be stored in publisher[7], the date the book was added to the inventory will be stored in dateAdded[7], the quantity of books on hand will be stored in qtyOnHand[7], the book’s wholesale price will be stored in wholesale[7], and the book’s retail price will be stored in retail[7].
The addBook function is currently a stub function. Modify it so it performs the following steps:
First the function should search the bookTitle array for an empty element. As described above, the function should look for an element containing an empty string or the null terminator. If no empty element is found, it means the array is full. In that case, the function should display a message indicating that no more books may be added to the inventory, and then terminate.
Once an empty element is found in bookTitle, the function should ask the user to enter the following items:
Book title
ISBN number
Author’s name
Publisher’s name
The date the book was added to the inventory
The quantity of the book being added
The wholesale cost of the book
The retail price of the book
Each item above should be added to the appropriate array. Note that the wholesale cost and retail price is for a single copy of the book. Also, remember that the date should be entered in the form MM-DD-YYYY, where MM is the month, DD is the day, and YYYY is the year.
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 8 Searching and Sorting Arrays
2.Modify the lookUpBook Function
The lookUpBook function is currently a stub function. Modify it so it performs the following tasks:
It should ask the user for the title of a book. This is the book that is to be looked up in the inventory database.
The function should search the bookTitle array for a title that matches the one entered by the user. If no match is found, the function should display a message indicating that the book is not in inventory, and terminate. If the book is found, the function should call bookInfo, passing the correct data as arguments.
3.Modify the editBook Function
The editBook function is currently a stub function. Modify it so it performs the following tasks:
It should ask the user for the title of a book. This is the book whose data are to be edited.
The function should search the bookTitle array for a title that matches the one entered by the user. If no match is found, the function should display a message indicating that the book is not in inventory, and terminate. If the book is found, the function should call bookInfo, passing the correct data as arguments.
The function should ask the user which of the fields he or she wished to change. The user is then allowed to enter new values for the selected field. These new values are saved in the arrays, replacing the old values.
4.Modify the deleteBook Function
The deleteBook function is currently a stub function. Modify it so it performs the following tasks:
It should ask the user for the title of a book. This is the book that is to be deleted from the inventory database.
The function should search the bookTitle array for a title that matches the one entered by the user. If no match is found, the function should display a message indicating that the book is not in inventory, and terminate. If the book is found, the function should call bookInfo, passing the correct data as arguments.
The function should ask the user if they are sure the book’s data are to be deleted from the inventory. If so, the book’s title (stored in the bookTitle array) is to be set to an empty string or the null terminator. The ISBN number (stored in the isbn array) should be set to an empty string or the null terminator as well.
mainmenu.h
//functions used in the main function required for each menu option
int reports(); //to report
int booksInfo(); //gives books info
int cashier(); //to calculate the price
int invMenu(); //to perform inventory options
//stub functions for inventory
void lookUpBook(); //to lookUp book
void addBook(); //to add book
void editBook(); //to edit book
void deleteBook(); //to delete book
//Stub functions for reporting
void repListing(); //to list books in inventory
void repWholesale(); //to report wholesale value
void repRetail(); //to report retail value
void repQty(); //to report quantity
void repCost(); //to report cost
void repAge(); //to report age
mainmenu.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "mainmenu.h"
using namespace std;
int main()
{
int choice = 0, ret;
while(choice!=4)
{
cout<<" Serendipity Booksellers" <<endl;
cout<<" Main Menu"<<endl <<endl;
cout<<" 1. Cashier Module" <<endl;
cout<<" 2. Inventory Database Module" <<endl;
cout<<" 3. Report Module" <<endl;
cout<<" 4. Exit" << endl;
cout<<" Enter Your Choice: ";
while(1)
{
cin>>choice;
if(choice<1 || choice>4)
{
cout<<" Please enter a number in the range 1 - 4."<<endl;
cout<<" Enter Your Choice: ";
}
else
break;
}
switch(choice)
{
case 1:
ret=cashier(); //cashier function is called
break;
case 2:
ret=invMenu(); //invMenu function is called
break;
case 3:
ret=reports(); //reports function is called
break;
case 4:
cout<<" You selected item 4";
break;
}
}
return 0;
}
//function bookInfo displays the information of book
int bookInfo(string isbn, string title, string author, string publisher, string date,
string qty, double wholesale, double retail)
{
cout << " Serendipity Booksellers" << endl;
cout << " Book Information" << endl << endl;
cout << "ISBN:" << isbn << endl;
cout << "Author:" << author << endl;
cout << "Publisher:" << publisher << endl;
cout << "Date Added:" << date << endl;
cout << "Quantity-On-Hand:" << qty << endl;
cout << "Wholesale Cost:" << wholesale << endl;
cout << "Retail Price:" << retail << endl;
return 0;
}
//function inventory menu to display the inventory menu options
int invMenu()
{
int choice = 0;
while(choice!=5)
{
cout<<" Serendipity Booksellers" <<endl;
cout<<" Inventory Database"<<endl <<endl;
cout<<" 1. Look Up a Book" <<endl;
cout<<" 2. Add a Book" <<endl;
cout<<" 3. Edit a Book's Record" <<endl;
cout<<" 4. Delete a Book" << endl;
cout<<" 5. Return to the Main Menu"<<endl <<endl;
cout<<" Enter Your Choice: ";
while(1)
{
cin>>choice;
if(choice<1 || choice>5)
{
cout<<" Please enter a number in the range 1 - 5."<<endl;
cout<<" Enter Your Choice: ";
}
else
break;
}
switch(choice)
{
case 1:
lookUpBook(); //call the lookupBook function
break;
case 2:
addBook(); //call the addBook function
break;
case 3:
editBook(); //call the editBook function
break;
case 4:
deleteBook(); //call the deleteBook function
break;
case 5:
cout<<" You selected item 5";
break;
}
}
return 0;
}
int reports()
{
int choice = 0;
while(choice!=7)
{
cout<<" Serendipity Booksellers" <<endl;
cout<<" Reports"<<endl <<endl;
cout<<" 1. Inventory Listing" <<endl;
cout<<" 2. Inventory Wholesale Value" <<endl;
cout<<" 3. Inventory Retail Value" <<endl;
cout<<" 4. Listing by Quantity " << endl;
cout<<" 5. Listing by Cost " << endl;
cout<<" 6. Listing by Age " <<endl;
cout<<" 7. Return To Main Menu:"<<endl <<endl;
cout<<" Enter Your Choice: ";
while(1)
{
cin>>choice;
if(choice<1 || choice>7)
{
cout<<" Please enter a number in the range 1 - 7."<<endl;
cout<<" Enter Your Choice: ";
}
else
break;
}
switch(choice)
{
case 1:
repListing(); //call the repListing function
break;
case 2:
repWholesale(); //call the repWholesale function
break;
case 3:
repRetail(); //call the repRetail function
break;
case 4:
repQty(); //call the repQty function
break;
case 5:
repCost(); //call the repCost() function
break;
case 6:
repAge(); //call the repAge() function
break;
case 7:
cout<<" You selected item 7";
break;
}
}
return 0;
}
//function cashier
int cashier()
{
string date, ISBN, Title;
int numOfBook;
double price, subtotal, tax, total;
int flag=1;
while(flag==1)
{
cout<<" Serendipity Booksellers"<<endl;
cout<<"Cashier Module"<<endl <<endl;
cout<< "Date (MM/DD/YY): ";
getline (cin >> ws, date);
cout<<"Quantity of Book: ";
cin >> numOfBook;
cout<< "ISBN: ";
getline (cin >> ws, ISBN);
cout<< "Title: ";
getline (cin >> ws, Title);
cout<< "Price: ";
cin >> price;
subtotal = numOfBook * price;
tax = 6 * subtotal / 100;
total = subtotal + tax;
// setting to 2 decimal places in fixed point notation
cout << fixed << setprecision(2);
cout <<endl << "Serendipity Book Sellers" <<endl;
cout<< "Date: " << date << endl << endl;
// printing the nice header
cout<< "Qty" <<setw(10) << "ISBN" << setw(17) << "Title" << setw(24)
<< "Price" << setw(11)<< "Total" << endl;
cout<< "--------------------------------------------------------------------" <<endl;
// printing the book data
cout << numOfBook << setw(21) << ISBN << setw(22) << Title << setw(18) << setfill(' ')
<< setw(6)<< "$" << price << setfill(' ') << setw(6) << "$"<< subtotal<<endl;
cout<<" Subtotal" <<setfill(' ') << setw(6) << "$"<< subtotal << endl;
cout<<" Tax" << setfill(' ') << setw(6) << "$" << tax <<endl;
cout<<"Total" << " $" << total <<endl << endl;
cout<<" ------------------------------------------";
cout<<" Do you want to continue..??(1 for Yes):";
cin>>flag;
cout<<" ------------------------------------------";
}
cout<<"Thank you for shopping at Serendipity!" <<endl;
}
//Stub functions for inventory menu options
//function to lookup book
void lookUpBook()
{
cout<<"You selected Look Up Book.";
}
//function to add book
void addBook()
{
cout<<"You selected Add Book.";
}
//function to edit book
void editBook()
{
cout<<"You selected Edit Book.";
}
//function to delete book
void deleteBook()
{
cout<<"You selected Delete Book.";
}
//Stub functions for report options
//function to list books
void repListing()
{
cout<<"You selected Inventory Listing.";
}
//function to report wholesale value
void repWholesale()
{
cout<<"You selected Inventory Wholesale Value.";
}
//function to report retail value
void repRetail()
{
cout<<"You selected Inventory Retail Value.";
}
//function to report Quantity
void repQty()
{
cout<<"You selected Listing By Quantity.";
}
//function to report cost
void repCost()
{
cout<<"You selected Listing By Cost.";
}
//function to report age
void repAge()
{
cout<<"You selected Listing By Age.";
}
Output:
Serendipity Booksellers
Main Menu
1. Cashier Module
2. Inventory Database Module
3. Report Module
4. Exit
Enter Your Choice: 2
Serendipity Booksellers
Inventory Database
1. Look Up a Book
2. Add a Book
3. Edit a Book's Record
4. Delete a Book
5. Return to the Main Menu
Enter Your Choice: 1
You selected Look Up Book.
Serendipity Booksellers
Inventory Database
1. Look Up a Book
2. Add a Book
3. Edit a Book's Record
4. Delete a Book
5. Return to the Main Menu
Enter Your Choice: 5
You selected item 5
Serendipity Booksellers
Main Menu
1. Cashier Module
2. Inventory Database Module
3. Report Module
4. Exit
Enter Your Choice: 4
You selected item 4
Explanation / Answer
Implemented Modified Source Code:-
-------------------------------
Main.cpp:-
=========
#include <iostream>
#include <string>
#include <fstream>
#include "bookTypeImp.cpp"
using namespace std;
void getBookData(bookType books[], int& noOfBooks);
void printBookData(bookType books[], int noOfBooks);
void searchBookData(bookType books[], int bookCount);
void searchBookDataByISBN(bookType books[], int bookCount, string ISBN,int& loc);
void searchBookDataByTitle(bookType books[], int bookCount, string title,int& loc);
void updateCopiesInStock(bookType books[], int bookCount);
void showMenu();
void subMenu();
int main()
{
bookType books[100];
int numberOfBooks = 0;
int choice;
int i;
getBookData(books, numberOfBooks);
showMenu();
cin>>choice;
while(choice != 9)
{
switch (choice)
{
case 1:
for(i=0;i<numberOfBooks;i++)
books[i].printbookTitle();
cout<<endl;
break;
case 2:
for(i=0;i<numberOfBooks;i++)
books[i].printbookTitleAndISBN();
cout << endl;
break;
case 3:
searchBookData(books, numberOfBooks);
break;
case 4:
updateCopiesInStock(books, numberOfBooks);
break;
case 5:
printBookData(books, numberOfBooks);
break;
default:
cout << "Invalid selection." << endl;
}
showMenu();
cin >> choice;
}
return 0;
}
void getBookData(bookType books[], int& noOfBooks)
{
string title;
string ISBN;
string Publisher;
int PublishYear;
string auth[4];
double cost;
int copies;
int authorCount;
int i, j;
ifstream infile;
char ch;
infile.open("bookData.txt");
if (!infile)
{
cout<<"Cannot open Input file"<<endl;
cout<<"Exit the program"<<endl;
return;
}
infile >> noOfBooks;
infile.get(ch);
for(i =0;i< noOfBooks;i++)
{
getline(infile, title);
getline(infile,ISBN);
getline(infile,Publisher);
infile >> PublishYear >> cost >> copies >> authorCount;
infile.get(ch);
for(j=0;j<authorCount;j++)
getline(infile, auth[j]);
books[i].setBookInfo(title, ISBN, Publisher,PublishYear, auth, cost, copies,authorCount);
}
}
void printBookData(bookType books[], int noOfBooks)
{
int i;
for (i = 0; i < noOfBooks; i++)
{
books[i].printInfo();
cout << endl << "---------------------------------" << endl;
}
}
void searchBookDataByISBN(bookType books[], int bookCount, string ISBN,int& loc)
{
int i;
loc = -1;
for (i = 0; i < bookCount; i++)
if (books[i].isISBN(ISBN))
{
loc = i;
break;
}
}
void searchBookDataByTitle(bookType books[], int bookCount, string title,int& loc)
{
int i;
loc = -1;
for (i = 0; i < bookCount; i++)
if (books[i].isTitle(title))
{
loc = i;
break;
}
}
void searchBookData(bookType books[], int bookCount)
{
int choice;
char ch;
int loc;
string str;
subMenu();
cin >> choice;
cin.get(ch);
switch(choice)
{
case 1:
cout << "Enter the ISBN of the book." << endl;
getline(cin, str);
searchBookDataByISBN(books, bookCount, str, loc);
if (loc != -1)
cout << "The store sells this book." << endl;
else
cout << "The store does not sell this book" << endl;
break;
case 2:
cout << "Enter the title of the book." << endl;
getline(cin, str);
searchBookDataByTitle(books, bookCount, str, loc);
if (loc != -1)
cout << "The store sells this book." << endl;
else
cout << "The store does not sell this book" << endl;
break;
default:
cout << "Invalid choice" << endl;
}
}
void updateCopiesInStock(bookType books[], int bookCount)
{
int choice;
int loc;
int count;
char ch;
string str;
subMenu();
cin >> choice;
cin.get(ch);
switch (choice)
{
case 1:
cout << "Enter the ISBN of the book." << endl;
getline(cin, str);
searchBookDataByISBN(books, bookCount, str, loc);
if (loc != -1)
{
cout << "Enter the number of books: " ;
cin >> count;
cout << endl;
books[loc].updateQuantity(count);
}
else
cout << "The store does not sell this book: " << endl;
break;
case 2:
cout << "Enter the title of the book." << endl;
getline(cin, str);
searchBookDataByTitle(books, bookCount, str, loc);
if (loc != -1)
{
cout << "Enter the number of books" ;
cin >> count;
cout << endl;
books[loc].updateQuantity(count);
}
else
cout << "The store does not sell this book" << endl;
break;
default:
cout << "Invalid choice" << endl;
}
}
void showMenu()
{
cout << "Welcome to Rock's Book Store" << endl;
cout << "To make a selection enter the number and press enter"<< endl;
cout << "1: Print a list of books" << endl;
cout << "2: Print a list of books and ISBN numbers" << endl;
cout << "3: To see if a book in store" << endl;
cout << "4: To update the number of copies of a book" << endl;
cout << "5: To print books data" << endl;
cout << "9: Exit the program." << endl;
}
void subMenu()
{
cout << "Enter" << endl;
cout << "1: To search the book by ISBN" << endl;
cout << "2: To search the book by title" << endl;
}
bookType.h:-
=============
#include <string>
using namespace std;
class bookType
{
public:
void setBookInfo(string title, string ISBN,string Publisher, int PublishYear,string auth[], double cost, int copies,int noAuthors);
void setBookTitle(string s);
void setBookISBN(string s);
void setBookPrice(double cost);
void setCopiesInStock(int noOfCopies);
void printInfo() const;
bool isISBN(string s) const;
bool isTitle(string s) const;
bool isAuthor(string s) const;
void getBookTitle(string& s) const;
void getBookISBN(string& s) const;
double getBookPrice() const;
bool isInStock() const;
void makeSale();
void printBookPrice() const;
void printbookTitle() const;
void printbookTitleAndISBN() const;
void showQuantityInStock() const;
void updateQuantity(int addBooks);
bookType();
private:
string bookTitle;
string bookISBN;
string bookPublisher;
int bookPublishYear;
string authors[4];
double price;
int quantity;
int noOfAuthors;
};
bookTypeImp.cpp:-
================
#include <iostream>
#include <string>
#include "bookType.h"
using namespace std;
void bookType::setBookInfo(string title, string ISBN,string Publisher, int PublishYear,string auth[], double cost, int copies,int authorCount)
{
int i;
bookTitle = title;
bookISBN = ISBN;
bookPublisher = Publisher;
bookPublishYear = PublishYear;
noOfAuthors = authorCount;
for (i = 0; i < noOfAuthors; i++)
authors[i] = auth[i];
price = cost;
quantity = copies;
}
void bookType::setBookTitle(string s)
{
bookTitle = s;
}
void bookType::setBookISBN(string s)
{
bookISBN = s;
}
void bookType::setBookPrice(double cost)
{
price = cost;
}
void bookType::setCopiesInStock(int noOfCopies)
{
quantity = noOfCopies;
}
void bookType::printInfo() const
{
int i;
cout << "Title: " << bookTitle << endl;
cout << "ISBN: " << bookISBN << endl;
cout << "Publisher: " << bookPublisher << endl;
cout << "Year of Publication: " << bookPublishYear << endl;
cout << "Number of Authors: " << noOfAuthors << endl;
cout << "Authors: ";
for (i = 0; i < noOfAuthors; i++)
cout << authors[i] << "; ";
cout << endl;
cout << "Price: " << price << endl;
cout << "Quantities in stock: " << quantity << endl;;
}
bool bookType::isISBN(string s) const
{
return (bookISBN == s);
}
bool bookType::isTitle(string s) const
{
return (bookTitle == s);
}
bool bookType::isAuthor(string s) const
{
bool found = false;
int i;
for (i = 0; i < noOfAuthors; i++)
if (authors[i] == s)
{
found = true;
break;
}
return found;
}
void bookType::getBookTitle(string& s) const
{
s = bookTitle;
}
void bookType::getBookISBN(string& s) const
{
s = bookISBN;
}
double bookType::getBookPrice() const
{
return price;
}
bool bookType::isInStock() const
{
return (quantity > 0);
}
void bookType::makeSale()
{
quantity--;
}
void bookType::printBookPrice() const
{
cout << "Price = " << price << endl;
}
void bookType::printbookTitle() const
{
cout << "Title: " << bookTitle << endl;
}
void bookType::printbookTitleAndISBN() const
{
cout << "Title: " << bookTitle << "; ISBN: " << bookISBN << endl;
}
void bookType::showQuantityInStock() const
{
cout << "Quantity: " << quantity << endl;
}
void bookType::updateQuantity(int addBooks)
{
quantity = quantity + addBooks;
}
bookType::bookType()
{
int i;
bookTitle = "";
bookISBN = "";
bookPublisher = "";
bookPublishYear = 1900;
noOfAuthors = 0;
for(i=0;i<4;i++)
authors[i] = "";
price = 0;
quantity = 0;
}
bookData.txt:-
=================
5
C++Programing: From Problem Analysis to Program Design
5-17-525281-3
ABC
2000
52.50
20
1
Malik, D.S.
Fuzzy Discrete Structures
3-7908-1335-4
Physica-Verlag
2000
89.00
10
2
Malik, Davender
Mordeson, John
Fuzzy Mathematic in Medicine
3-7908-1325-7
Physica-Verlag
2000
89.00
10
3
Mordeson, John
Malik, Davender
Cheng, Shih-Chung
Harry John and The Magician
0-239-23635-0
McArthur A. Devine Books
1999
19.95
10
3
Goof, Goofy
Pluto, Peter
Head, Mark
Dynamic InterWeb Programming
22-99521-453-1
GNet
1998
39.99
25
1
Python venky 05/08/2018 2019
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.