This C++ program consists of writing classes that implement an inventory list si
ID: 3852308 • Letter: T
Question
This C++ 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:
Thanks a lot for your assistance!
// 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
//book.h
#ifndef _BOOK_H
#define _BOOK_H
#include <iostream>
using namespace std;
enum Genre {FICTION, MYSTERY, SCIFI, COMPUTER};
class Book
{
public:
Book(); // default constructor, sets up blank book object
void Set(char* t, char* a, Genre g, double p);
const char* GetTitle(); // returns the title stored in the object
const char* GetAuthor(); // returns the author
double GetPrice(); // returns the price
Genre GetGenre(); // returns the genre
void Display(); // 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;
};
#endif
//store.h
#ifndef _STORE_H
#define _STORE_H
#include "book.h"
#include <iostream>
using namespace std;
class Store
{
public:
Store();
~Store();
void Add();
void Find();
void Sell();
void Display();
void Genre();
void ShowMenu();
void Exit();
private:
int maxSize,
currentSize;
void Grow();
int FindBook(char* aBook);
};
#endif
//store.cpp
#include <iostream>
#include <cstring>
#include "store.h"
Store::Store()
{
maxSize = 20;
currentSize = 0;
entryList = new char[maxSize];
}
Store::~Store()
// This destructor function for class Store
// deallocates the menu's list of Entry objects.
{
delete [] entryList;
}
void Store::Add()
// Insert a new entry into the store.
{
if (currentSize == maxSize) // If the directory is full, grow it.
Grow();
entryList[currentSize++]; // read new entry.
}
void Store::Find()
{
char aBook[20];
cout << " Type the title of the book to be looked up, followed by <Enter>: ";
cin.getline(aBook, 31);
int thisEntry = FindBook(aBook);
if (thisEntry == -1)
cout << aBook << " not found in current menu ";
else
{
cout << " Entry found: ";
entryList[thisEntry]; // display entry.
}
}
void Store::Sell()
{
char aBook[31];
cout << " Type title to be removed, followed by <Enter>: ";
cin.getline(aBook, 31);
int thisEntry = FindBook(aBook);
if (thisEntry == -1)
cout << aBook << " not found in directory";
else
{
// Shift each succeding element "down" one position in the
// Entry array, thereby deleting the desired entry.
for (int j = thisEntry + 1; j < currentSize; j++)
entryList[j - 1] = entryList[j];
currentSize--; // Decrement the current number of entries.
cout << "Entry removed. ";
}
}
void Store::Display()
{
if (currentSize == 0)
{
cout << " Store is currently empty. ";
return;
}
// Display a header.
cout << " ***Title*** ***Author*** ***Genre*** ***Price*** ";
for (int i = 0; i < currentSize; i++) // For each entry,
entryList[i]; // send it to output
}
void Store::Grow()
// Double the size of the store's entry list
// by creating a new, larger array of entries
// and changing the store's pointer to refer to
// this new array.
{
maxSize = currentSize + 5; // Determine a new size.
char* newList = new char[maxSize]; // Allocate a new array.
for (int i = 0; i < currentSize; i++) // Copy each entry into
newList[i] = entryList[i]; // the new array.
delete [] entryList; // Remove the old array
entryList = newList; // Point old name to new array.
}
int Store::FindBook(char* aBook)
// Locate a book in the store. Returns the
// position of the entry list as an integer if found.
// and returns -1 if the entry is not found in the directory.
{
//for (int i = 0; i < currentSize; i++) // Look at each entry.
// if (strcmp(entryList[i].GetTitle(), aBook) == 0)
// return i; // If found, return position and exit.
//return -1; // Return -1 if never found.
}
//menu.cpp
#include <cctype> // for toupper
#include <iostream> // for cin, cout
#include "store.h"
#include "store.cpp" // for class Directory
using namespace std;
void DisplayMenu()
// Display the main program menu.
{
cout << " *** Bookstore Menu ***";
cout << " A Add book to inventory";
cout << " F Find a book form inventory";
cout << " S Sell a book";
cout << " D Display the inventory list";
cout << " G Genre summary";
cout << " M Show this Menu";
cout << " X Exit the Program";
}
char GetAChar(char* promptString)
// Prompt the user and get a single character,
// discarding the Return character.
// Used in GetCommand.
{
char response; // the char to be returned
cout << promptString; // Prompt the user
cin >> response; // Get a char,
response = toupper(response); // and convert it to uppercase
cin.get(); // Discard newline char from input.
return response;
}
char Legal(char c)
// Determine if a particular character, c, corresponds
// to a legal menu command. Returns 1 if legal, 0 if not.
// Used in GetCommand.
{
return ((c == 'A') || (c == 'F') || (c == 'S') || (c == 'D') ||
(c == 'G') || (c == 'M') || (c == 'X'));
}
char GetCommand()
// Prompts the user for a menu command until a legal
// command character is entered. Return the command character.
// Calls GetAChar, Legal, ShowMenu.
{
char cmd = GetAChar(" >"); // Get a command character.
while (!Legal(cmd)) // As long as it's not a legal command,
{ // display menu and try again.
cout << " Illegal command, please try again . . .";
DisplayMenu();
cmd = GetAChar(" >");
}
return cmd;
}
int main()
{
Store m; // Create and initialize a new directory.
DisplayMenu(); // Display the menu.
char command; // menu command entered by user
do
{
command = GetCommand(); // Retrieve a command.
switch (command)
{
case 'A': m.Add();
break;
case 'F': m.Find();
break;
case 'S': m.Sell();
break;
case 'D': m.Display();
break;
//case 'G': m.genre();
//break;
case 'M': DisplayMenu();
break;
case 'X':
break;
}
} while (command != 'X');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.