This program consists of writing classes that implement an inventory list simula
ID: 3852123 • Letter: T
Question
This program consists of writing classes that implement an inventory list simulation for a bookstore. The list will be a dynamic array of Book objects, each of which stores several pieces of information about a book. You will need to finish the writing of two classes: Book and Store. Here is the full header file for the Book class:
// An object of type Book will store information about a single // book. The variable "type" stores the category of the book // (one of the four items in the enumerated type Genre). #1fndef BOOKH #define BOOKH - - - - enum Genre tFICTION, MYSTERY, SCIFI, COMPUTER class Book public: Book); // default constructor, sets up blank book object void Set(const char* t, const char* a, Genre g, double p); // the Set function should allow incoming data to be received through // parameters and loaded into the member data of the object. (i.e // this function "sets" the state of the object to the data passed in) // The parameters t, a, g, and p represent the title, author, genre, // and price of the book, respectively. // returns the title stored in the object const char* GetTitle() const const char* GetAuthor) const; /7 returns the author double GetPrice () consti Genre GetGenre) const; // returns the price // returns the genre void Display) const; // described below private: char title[31]; // may assume title is 30 characters or less char author[21] may assume author name is 20 characters or less Genre type; double price; /* Display() function The member function Display() should print out a Book object on one line as follows, in an organized manner. (Monetary output should be in dollars and cents notation, to two decimal places): Title Author Genre Price Examples: Programming for Dummies Mutant Space Weasels Marvin Dipwart Bob Myers Computer 24.95 Sci-Fi $5.95 #endifExplanation / Answer
Below is the code for the question. Please take the book.h from your question. Rest of the files are below
In case of any issues, please post a comment and I shall respond. Please don't forget to rate the answer if it helped. Thank you very much.
book.cpp
#include "book.h"
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
Book::Book()
{
//default values
title[0] = '';//empty string
author[0] = ''; //empty string
type = FICTION;
price = 0;
}
void Book::Set(const char*t, const char *a, Genre g, double p)
{
strcpy(title, t);
strcpy(author, a);
type = g;
price = p;
}
const char* Book::GetTitle() const
{
return title;
}
const char* Book::GetAuthor() const
{
return author;
}
double Book::GetPrice() const
{
return price;
}
Genre Book::GetGenre() const
{
return type;
}
void Book::Display() const
{
char genreNames[4][12]={"Fiction", "Mystery", "Sci-Fi", "Computer"};
cout << left << setw(40) << title
<< setw(30) << author
<< setw(15) << genreNames[type]
<< "$" << right << setw(7) << fixed << setprecision(2) << price << endl;
}
store.h
#ifndef store_h
#define store_h
#include "book.h"
class Store
{
public:
Store(double cash = 0.0); //default constructor
void AddBook(char *title, char *author, Genre type, double price); //add a book to inventory
void FindBook(char *search) const;//search for books with matching title or author
bool SellBook(char *title); //remove a book from list and add price to cash register
void DisplayInventory() const; // display all the books
void DisplayInventory(Genre type) const; //display summary of books of specific type
void SortByAuthor();
void SortByTitle();
double GetCashBalance() const;
~Store(); //destructor
private:
Book** books; //dynamic array of books, each element is a pointer to dynamically created book
int bookCount; //number elements in the book array
int capacity; //maximum capacity of the array
double cashRegister; //store's cash register
};
#endif /* store_h */
store.cpp
#include <iostream>
#include <cstring>
#include "store.h"
using namespace std;
Store::Store(double cash) //default constructor
{
books = new Book*[5]; //create space for 5 books initially
bookCount = 0;
capacity = 5;
cashRegister = cash;
}
void Store::AddBook(char *title, char *author, Genre type, double price) //add a book to inventory
{
if(bookCount == capacity) //array already full
{
//reallocate
capacity += 5; //increase capacity by 5 more slots
cout << "** Array being resized to " << capacity << " allocated slots " <<endl;
Book **temp = new Book*[capacity];
for(int i = 0; i < bookCount; i++)
{
temp[i] = books[i];
}
delete []books; //deallocate old array;
books = temp; //point to newly reallocated array
}
Book *b = new Book();
b->Set(title, author, type, price);
books[bookCount++] = b;
cout << "No. of books = " << bookCount << " Cash register : " << cashRegister << endl;
}
void Store::FindBook(char *search) const//search for books with matching title or author
{
for(int i = 0; i < bookCount; i++)
{
//if search word matches the title
if(strcmp(books[i]->GetTitle(), search) == 0)
{
//cout << "Found book with matching title " << search << endl;
books[i]->Display();
return; //since titles are unique, there will be only 1book to be displayed.
}
else if (strcmp(books[i]->GetAuthor(), search) == 0) //matching author
{
//cout << "Found book with matching author " << search << endl;
books[i]->Display();
}
}
}
bool Store::SellBook(char *title) //remove a book from list and add price to cash register
{
bool found = false;
int i;
for(i = 0; i < bookCount; i++)
{
//does teh title match?
if(strcmp(books[i]->GetTitle(), title) == 0)
{
found = true;
break;
}
}
if(found)
{
cashRegister += books[i] -> GetPrice();
delete books[i]; //delete the book at the found location
//move all books below the deleted book 1 location up
for(; i<bookCount - 1; i++)
books[i] = books[i+1];
bookCount --;
cout << "No. of books = " << bookCount << " Cash register : " << cashRegister << endl;
if(capacity - bookCount >= 5 ) //should we shrink the array
{
//create a smaller sized array with juts 5 empty slots, and copy all contents
//then deallocate old array and update the books pointer to newly allocated array
capacity = bookCount + 5;
Book** temp = new Book*[capacity];
for(int i = 0; i < bookCount; i++)
temp[i] = books[i];
delete []books;
books = temp;
cout << "** Array being resized to " << capacity << " allocated slots " <<endl;
}
}
return found;
}
void Store::DisplayInventory() const // display all the books
{
for(int i = 0; i < bookCount; i++)
{
books[i]->Display();
}
}
void Store::DisplayInventory(Genre type) const //display summary of books of specific type
{
for(int i = 0; i < bookCount; i++)
{
if(books[i]->GetGenre() == type)
books[i]->Display();
}
}
void Store::SortByTitle()
{
int minIdx;
for(int i = 0; i < bookCount; i++)
{
minIdx = i;
for(int j = i+1; j < bookCount; j++)
{
if(strcmp(books[j]->GetTitle() , books[minIdx]->GetTitle()) < 0)
minIdx = j;
}
if(minIdx != i)
{
//swap
Book *b = books[i];
books[i] = books[minIdx];
books[minIdx] = b;
}
}
}
void Store::SortByAuthor()
{
int minIdx;
for(int i = 0; i < bookCount; i++)
{
minIdx = i;
for(int j = i+1; j < bookCount; j++)
{
if(strcmp(books[j]->GetAuthor(), books[minIdx]->GetAuthor()) < 0)
minIdx = j;
}
if(minIdx != i)
{
//swap
Book *b = books[i];
books[i] = books[minIdx];
books[minIdx] = b;
}
}
}
double Store::GetCashBalance() const
{
return cashRegister;
}
Store::~Store() //destructor
{
//first deallocate all books, then delete the array
for(int i = 0; i < bookCount; i++)
{
delete books[i];
}
delete []books;
}
menu.cpp
#include <iostream>
#include "store.h"
using namespace std;
void MainMenu();
void AddBookMenu(Store &store);
void FindBookMenu(const Store &store);
void SellBookMenu(Store &store);
void GenreSummaryMenu(const Store &store);
void DisplayInventoryMenu(const Store &store);
void SortMenu(Store &store);
int main()
{
char choice = ' ' ;
double cashBalance ;
cout << "Enter initial cash balance: ";
cin >> cashBalance;
Store store(cashBalance);
MainMenu();
while(choice != 'X')
{
cout << " Enter menu choice: ";
cin >> choice;
choice = toupper(choice);
switch(choice)
{
case 'A':
AddBookMenu(store);
break;
case 'F':
FindBookMenu(store);
break;
case 'S':
SellBookMenu(store);
break;
case 'D':
DisplayInventoryMenu(store);
break;
case 'G':
GenreSummaryMenu(store);
break;
case 'M':
MainMenu();
break;
case 'X':
break;
case 'O':
SortMenu(store);
break;
default:
cout << "Not a valid option, choose again." << endl;
break;
}
}
cout << " Store Cash balance : " << store.GetCashBalance() << endl;
return 0;
}
void MainMenu()
{
cout << endl;
cout << "A: Add a book to inventory" << endl;
cout << "F: Find a book from inventory" << endl;
cout << "S: Sell a book" << endl;
cout << "D: Display the inventory list" << endl;
cout << "G: Genre summary" << endl;
cout << "O: Sort " << endl;
cout << "M: Show this menu" << endl;
cout << "X: Exit the program" << endl;
}
void AddBookMenu(Store &store)
{
char title[31], author[21];
char genreCh;
Genre genre;
double price;
cout << " Adding a book ..." << endl;
cout << "Enter title: ";
getchar();
cin.getline(title, 30, ' ');
cout << "Enter author: ";
cin.getline(author, 20, ' ');
while(true)
{
cout << "Enter genre(F,M,S,C) : ";
cin >> genreCh;
genreCh = toupper(genreCh);
if(genreCh == 'F')
genre = FICTION;
else if(genreCh == 'M')
genre = MYSTERY;
else if(genreCh == 'S')
genre = SCIFI;
else if(genreCh == 'C')
genre = COMPUTER;
else
{
cout << "Invalid genre entry. Please re-enter" << endl;
continue;
}
break;
}
while(true)
{
cout << "Enter price: ";
cin >> price;
if(price <= 0)
{
cout << "Must enter a positive price. Please re-enter" << endl;
}
else
break;
}
store.AddBook(title, author, genre, price);
cout << "Book added successfully!" << endl;
}
void FindBookMenu(const Store &store)
{
char search[31];
cout << " Enter search line: ";
getchar();
cin.getline(search, 30, ' ');
cout << "Searching the store ..." << endl;
store.FindBook(search);
}
void SellBookMenu(Store &store)
{
char title[31];
cout << " Enter title of book to sell: ";
getchar();//newline
cin.getline(title, 30, ' ');
if(store.SellBook(title))
cout << "Book sold successfully. " << endl;
else
cout << "No such book." << endl;
}
void GenreSummaryMenu(const Store &store)
{
char genreCh;
Genre genre;
while(true)
{
cout << "Enter genre(F,M,S,C) : ";
cin >> genreCh;
genreCh = toupper(genreCh);
if(genreCh == 'F')
genre = FICTION;
else if(genreCh == 'M')
genre = MYSTERY;
else if(genreCh == 'S')
genre = SCIFI;
else if(genreCh == 'C')
genre = COMPUTER;
else
{
cout << "Invalid genre entry. Please re-enter" << endl;
continue;
}
break;
}
cout << "Displaying list of matching genre..." << endl;
store.DisplayInventory(genre);
}
void DisplayInventoryMenu(const Store &store)
{
cout << "Displaying list of all books ..." << endl;
store.DisplayInventory();
}
void SortMenu(Store &store)
{
char ch;
while(true)
{
cout << "Sort by Author or Title (A/T) : ";
cin >> ch;
ch = toupper(ch);
if(ch != 'A' && ch != 'T')
cout << "Invalid sort option. Please re-enter" << endl;
else
break;
}
if(ch == 'A')
store.SortByAuthor();
else
store.SortByTitle();
}
compile
g++ book.cpp store.cpp menu.cpp
output
A: Add a book to inventory
F: Find a book from inventory
S: Sell a book
D: Display the inventory list
G: Genre summary
O: Sort
M: Show this menu
X: Exit the program
Enter menu choice: a
Adding a book ...
Enter title: Programming for Dummies
Enter author: Marvin Dipwart
Enter genre(F,M,S,C) : p
Invalid genre entry. Please re-enter
Enter genre(F,M,S,C) : C
Enter price: 24.95
Book added successfully!
Enter menu choice: u
Not a valid option, choose again.
Enter menu choice: a
Adding a book ...
Enter title: Mutant Space Weasels
Enter author: Bob Myers
Enter genre(F,M,S,C) : S
Enter price: 5.95
Book added successfully!
Enter menu choice: d
Displaying list of all books ...
Programming for Dummies Marvin Dipwart Computer $ 24.95
Mutant Space Weasels Bob Myers Sci-Fi $ 5.95
Enter menu choice: a
Adding a book ...
Enter title: C Programming
Enter author: Yeshwanth
Enter genre(F,M,S,C) : C
Enter price: 19.99
Book added successfully!
Enter menu choice: d
Displaying list of all books ...
Programming for Dummies Marvin Dipwart Computer $ 24.95
Mutant Space Weasels Bob Myers Sci-Fi $ 5.95
C Programming Yeshwanth Computer $ 19.99
Enter menu choice: o
Sort by Author or Title (A/T) : a
Enter menu choice: o
Sort by Author or Title (A/T) : p
Invalid sort option. Please re-enter
Sort by Author or Title (A/T) : t
Enter menu choice: d
Displaying list of all books ...
C Programming Yeshwanth Computer $ 19.99
Mutant Space Weasels Bob Myers Sci-Fi $ 5.95
Programming for Dummies Marvin Dipwart Computer $ 24.95
Enter menu choice: o
Sort by Author or Title (A/T) : a
Enter menu choice: d
Displaying list of all books ...
Mutant Space Weasels Bob Myers Sci-Fi $ 5.95
Programming for Dummies Marvin Dipwart Computer $ 24.95
C Programming Yeshwanth Computer $ 19.99
Enter menu choice: g
Enter genre(F,M,S,C) : c
Displaying list of matching genre...
Programming for Dummies Marvin Dipwart Computer $ 24.95
C Programming Yeshwanth Computer $ 19.99
Enter menu choice: s
Enter title of book to sell: C Programming
Book sold successfully.
Enter menu choice: d
Displaying list of all books ...
Mutant Space Weasels Bob Myers Sci-Fi $ 5.95
Programming for Dummies Marvin Dipwart Computer $ 24.95
Enter menu choice: m
A: Add a book to inventory
F: Find a book from inventory
S: Sell a book
D: Display the inventory list
G: Genre summary
O: Sort
M: Show this menu
X: Exit the program
Enter menu choice: x
Store Cash balance : 19.99
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.