a8 serendipity c++ Chapter 8 Searchine and Sorting Arrays Serendipity Bookseller
ID: 3748699 • Letter: A
Question
a8 serendipity
c++
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
hope it helps.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.