Using the classes designed in Programming Exercises 10 and 11, write a program t
ID: 3857370 • Letter: U
Question
Using the classes designed in Programming Exercises 10 and 11, write a program to simulate a bookstore. The bookstore has two types of customers: Those who are members of the bookstore and those who buy books from the bookstore only occasionally. Each member has to pay a $10 yearly membership fee and receives a 5% discount on each book purchased.
For each member, the bookstore keeps track of the number of books purchased and the total amount spent. For every eleventh book that a member buy, the bookstore takes the average of the total amount of the last 10 books purchased, applies this amount as a discount, and then resets the total amount spent to 0.
Write a program that can process up to 1000 book titles and 500 members. Your program should contain a menu that gives the user different choices to effectively run the program; in other words, your program should be user dri
The output should look similar to the screen shot below
The book data is provided in a file called BookData.txt and is ordered as follows. Each book element is stored as one line in the file Ch10_Ex12: Submit only: Example Data Data Element The number of books in the file. (up to 2 Header files (bookType.h, memberType.h) Implementation files (bookTypeImp.cpp, memberTypeImp.cpp) Your main program, and your documentation files. 1000 1 book) Title (1st book) ISBN 1% book) Publisher Name (1s: book) Published Year (1st book) Cost of Book (1 book) Copies on Hand (1* book) Count of Authors (1 book) Authors [up to 4 The Reason For Air 5-17-525281-3 BJ Smith & Co. 2016 45.00 15 The bookType header file must contain the following functional prototypes: void setBookInfolstring title, string ISBN, string Publisher, int PublishYear string auth, double cost, int copies, void setBokTitlefstring s); void setBookISBN(string s) void setBookPrice(double cost); void setcopiesInStock(int noOfCopies); (2nd book) Title (2nd book) ISBN 2nd book) Publisher Nane (2nd book) Published Year 2nd book) Cost of Book 2nd book) Copies on Hand (2nd book) Count of Authors (2nd book) Authors (4 Max) Fester Bestertester Joe Blowinski WTF Programmin 1-23-456789-3 Fly By Night Publishi 2014 25.87 10 void printinfo) onst bool isISEN(string s) const: bool isTitle(string s) const bool isAuthor(string s) snst void getBokTitle(string& s) sonsti void getBookISBN(string& s) const; double getBookPrice) const: Fester Bestertester loe Blowinski John Q. Public Sarah Smith E bool isIntk soast; void makeSale) void printBookPrice) const; void printbookTitle) const: void printbaskTitlAndISENO sonsti void updateduantity int addBooks); bookType);Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
ofstream fileWrite;
#include "Globel.h"
int main() {
BooksList* total = new BooksList();
char again='n';
do {
system("cls");
char ch;
cout<<" Welcome To MY-BOOKSTORE ";
cout<<"Select Option. ";
cout<<" 1. Add New Category ";
cout<<" 2. Add New Book ";
cout<<" 3. Delete a Category ";
cout<<" 4. Delete a Book ";
cout<<" 5. Search a Category ";
cout<<" 6. Search a Book ";
cout<<" 7. Print All ";
cout<<"Input : ";
cin>>ch;
switch(ch) {
case '1':
total->InsertNewCategory();
break;
case '2':
total->InsertNewBook();
break;
case '3':
total->DeleteCategory();
break;
case '4':
total->DeleteBook();
break;
case '5':
total->SearchCategory();
break;
case '6':
total->SearchBook();
break;
case '7':
total->PrintAll();
break;
default:
cout<<"Invalid Option. ";
break;
}
cout<<" Do you want to run again(y/n) : "; cin>>again;
}while(again!='n');
system("pause");
}
class BooksList {
CategoryNode *head;
public:
BooksList() {
head = NULL;
}
bool isEmpty() {
return (head==NULL);
}
bool Search(string query, int val, string save) { // search a book / category..
bool retFound = false;
if(save == "yes")
fileWrite.open("Books_Info.txt",ios::app);
if(!isEmpty()){
CategoryNode *curr=head,*ret=NULL;
bool found=false;
while(curr!=NULL && !found){
if(curr->category == query && val==2) { // search in category
if(save == "yes")
fileWrite<<" Category : "<<curr->category;
cout<<" Category : "<<curr->category;
found=true;
BookNode* sBooks = curr->books;
while(sBooks != NULL && !found) {
if(save == "yes")
fileWrite<<" Book Title : "<<sBooks->title;
cout<<" Book Title : "<<sBooks->title;
sBooks = sBooks->next;
}
}
else if(val==1) { // search in books..
BookNode* sBooks = curr->books;
bool bookF = false;
while(sBooks != NULL && !bookF) {
if(sBooks->title == query) {
bookF = true;
if(save == "yes")
fileWrite<<" Book Title : "<<sBooks->title;
cout<<" Book Title : "<<sBooks->title;
}
else {
sBooks = sBooks->next;
}
}
if(bookF) {
if(save == "yes")
fileWrite<<" Category : "<<curr->category;
cout<<" Category : "<<curr->category;
found = retFound = true;
}
}
curr=curr->next;
}
retFound = found;
}
if(save == "yes") fileWrite.close();
return retFound;
}
bool DeleteBookByTitle(string bookName) {
bool retResult = false;
if(!isEmpty()){
CategoryNode *curr=head,*ret=NULL;
bool deleted=false;
while(curr!=NULL && !deleted) {
bool found = false;
BookNode* currBook = curr->books;
BookNode* prevBook = NULL;
while(currBook != NULL && !found) {
if(currBook->title == bookName) {
found = true;
}
else {
prevBook = currBook;
currBook = currBook->next;
}
}
if(found) {
BookNode* node = currBook;
if(prevBook == NULL) {
curr->books = currBook->next;
}
else {
prevBook->next = currBook->next;
}
delete (node);
deleted = true;
}
else // move to next category
curr=curr->next;
}
retResult = deleted;
}
return retResult;
}
bool InsertCategory(string cat) {
bool retInserted = false;
CategoryNode *newnode = new CategoryNode(cat);
if(isEmpty()) {
head = newnode;
retInserted = true;
}
else {
newnode->next = head;
head = newnode;
retInserted = true;
}
return retInserted;
}
bool InsertBook(string bok, int catID) {
bool retInserted = false;
BookNode *newBook = new BookNode(bok);
if(!isEmpty()) { // categories available
int i=1;
CategoryNode *curr = head,*prev = NULL;
bool CanInsert=false;
i = 1;
while(i<= catID && curr!=NULL && !CanInsert) {
if(i == catID) {
if(curr->books == NULL) {
CanInsert = true;
}
else{
BookNode *sBooks = curr->books;
bool foundBook = false;
while(sBooks!=NULL && !foundBook) {
if(sBooks->title == bok) { // book found
foundBook = true;
}
else {
sBooks = sBooks->next;
}
}
if(!foundBook) {
CanInsert = true;
}
}
}
else{
curr = curr->next;
i++;
}
}
if( CanInsert && curr != NULL ) {
if(curr->books == NULL){
curr->books = newBook;
retInserted = true;
}
else {
newBook->next = curr->books;
curr->books = newBook;
retInserted = true;
}
}
}
return retInserted;
}
bool DeleteCategoryByID(int catID) {
bool retDeleted = false;
if(!isEmpty()){
CategoryNode* curr = head;
CategoryNode* prev = NULL;
int i=1;
bool canDelete = false;
while(curr!=NULL && i<=catID && !canDelete) {
if(i == catID){
canDelete = true;
}
else{
prev = curr;
curr = curr->next;
i++;
}
}
if(canDelete) {
if(prev == NULL) { // delete First
CategoryNode *node = curr;
head = curr->next;
delete (node);
retDeleted = true;
}
else {
CategoryNode *node = curr;
prev->next = curr->next;
delete (node);
retDeleted = true;
}
}
}
return retDeleted;
}
void InsertNewCategory() {
string categ;
cout<<"Category Name : ";
fflush(stdin);
getline(cin,categ);
convertToLower(categ);
if(InsertCategory(categ)) {
cout<<"Inserted.. ";
}
else {
cout<<"Already Exist.! ";
}
}
void InsertNewBook() {
if(!isEmpty()){
string bookTitle = "";
int categ = -1, i = 1;
CategoryNode *ptr = head;
cout<<"Book Title : ";
fflush(stdin);
getline(cin,bookTitle);
convertToLower(bookTitle);
cout<<" Available Books Categories ";
while(ptr!=NULL) {
cout<<i<<". "<<ptr->category<<endl;
ptr = ptr->next;
i++;
}
cout<<"Choose Category :"; cin>>categ;
if( InsertBook(bookTitle,categ) ) {
cout<<"Inserted.. ";
}
else {
cout<<"Book Already Exist.! ";
}
}
else {
cout<<"No Categories Available! ";
}
}
void DeleteCategory() {
if(!isEmpty()) {
int categ = -1, i = 1;
CategoryNode *ptr = head;
cout<<" Available Books Categories ";
while(ptr!=NULL) {
cout<<i<<". "<<ptr->category<<endl;
ptr = ptr->next;
i++;
}
cout<<"Choose Category to delete :"; cin>>categ;
if( DeleteCategoryByID(categ) ) {
cout<<"Deleted.. ";
}
else {
cout<<"Category not exist.! ";
}
}
else {
cout<<"No Categories Available! ";
}
}
void DeleteBook() {
if(!isEmpty()) {
string bookTitle = "";
cout<<"BookTitle to delete :";
fflush(stdin);
getline(cin,bookTitle);
convertToLower(bookTitle);
if( DeleteBookByTitle(bookTitle) ) {
cout<<"Deleted.. ";
}
else {
cout<<"No Category exist.! ";
}
}
else {
cout<<"No Categories Available! ";
}
}
void SearchBook() {
if(!isEmpty()) {
string bookTitle = "", saveResult="no";
cout<<"BookTitle to search :";
fflush(stdin);
getline(cin,bookTitle);
cout<<"Do you want to save results too(yes/no): ";
cin>>saveResult;
convertToLower(bookTitle);
convertToLower(saveResult);
if(!Search(bookTitle,1,saveResult))
cout<<"Book Not Found! ";
}
else {
cout<<"No Categories Available! ";
}
}
void SearchCategory() {
if(!isEmpty()) {
string Categ = "", saveResult="no";
cout<<"Category to search :";
fflush(stdin);
getline(cin,Categ);
cout<<"Do you want to save results too(yes/no): ";
cin>>saveResult;
convertToLower(saveResult);
convertToLower(Categ);
if(!Search(Categ,2,saveResult))
cout<<"Category Not Found! ";
}
else {
cout<<"No Categories Available! ";
}
}
void PrintAll() {
if(!isEmpty()) {
CategoryNode* currCateg = head;
int i=1,j=1;
while(currCateg!=NULL) {
cout<<" Category "<<i<<": "<<currCateg->category<<" ";
BookNode *currBook = currCateg->books;
j=1;
while(currBook!=NULL) {
cout<<" Book Title"<<j<<" : "<<currBook->title<<" ";
currBook = currBook->next;
j++;
}
currCateg = currCateg->next;
i++;
}
}
else {
cout<<"No Categories Available! ";
}
}
};
class BookNode {
public:
string title;
BookNode *next;
BookNode(string cat) {
title = cat;
next = NULL;
}
void setNext(BookNode *nn) {
next = nn;
}
BookNode* getNext() {
return next;
}
string getTitle() {
return title;
}
};
class CategoryNode {
public:
string category;
CategoryNode *next;
BookNode *books; // all books of that particular category
CategoryNode(string cat) {
category = cat;
next = NULL;
books = NULL;
}
void setNext(CategoryNode *nn) {
next = nn;
}
void setBooks(BookNode *bb) {
books = bb;
}
CategoryNode* getNext() {
return next;
}
BookNode* getBooks() {
return books;
}
string getCategory() {
return category;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.