Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have the following code which is currently in one single cpp file but needs to be broken out to have three separate files and

I have the following code which is currently in one single cpp file but needs to be broken out to have three separate files and not sure how I extract from this to create the three. One should be booksreceived.h (header file), booksreceived.cpp (class implementation file) and booksdriver.cpp (driver file). The checklist I need to meet is attached as well.
#include
#include
#include
#include
using namespace std;
class BooksReceived {
private:
string nameBookstore;
string dateShipped;
int numberHardbound;
int numberPaperback;
vector books;
public:
// Constructors
BooksReceived() : nameBookstore(""), dateShipped(""), numberHardbound(0), numberPaperback(0){}
BooksReceived(string name, string date, int hardbound, int paperback)
: nameBookstore(name), dateShipped(date), numberHardbound(hardbound), numberPaperback(paperback){}
// Setters
void setNameBookstore(string name){ nameBookstore = name; }
void setDateShipped(string date){ dateShipped = date; }
void setNumberHardbound(int hardbound){ numberHardbound = hardbound; }
void setNumberPaperback(int paperback){ numberPaperback = paperback; }
// Getters
string getNameBookstore() const { return nameBookstore; }
string getDateShipped() const { return dateShipped; }
int getNumberHardbound() const { return numberHardbound; }
int getNumberPaperback() const { return numberPaperback; }
// Functions
void addBook(string book){ books.push_back(book); }
void sortBookList(){ sort(books.begin(), books.end()); }
int countBooksReceived() const { return books.size(); }
double calcHardboundPercentage() const { return (numberHardbound *100.0)/(countBooksReceived()+0.0); }
double calcPaperbackPercentage() const { return (numberPaperback *100.0)/(countBooksReceived()+0.0); }
void displayBooksReceivedInfo() const {
cout "Bookstore: " nameBookstore endl;
cout "Date Shipped: " dateShipped endl;
cout "Number of Hardbound Books: " numberHardbound endl;
cout "Number of Paperback Books: " numberPaperback endl;
cout "
Book Titles:
";
for (const string& book : books){
cout "-" book endl;
}
cout fixed setprecision(1);
cout "Percentage of Hardbound Books: " calcHardboundPercentage()"%" endl;
cout "Percentage of Paperback Books: " calcPaperbackPercentage()"%" endl;
}
};
// Non-Class Functions
void readDataFromFile(BooksReceived& shipment, const string& fileName){
ifstream file(fileName);
if (!file.is_open()){
cerr "Error opening file: " fileName endl;
exit(EXIT_FAILURE);
}
string name;
string date;
int hardbound;
int paperback;
string bookTitle;
// Read data from the file
getline(file, name);
getline(file, date);
file >> hardbound >> paperback;
file.ignore(); // Consume the newline character
shipment.setNameBookstore(name);
shipment.setDateShipped(date);
shipment.setNumberHardbound(hardbound);
shipment.setNumberPaperback(paperback);
while (getline(file, bookTitle)){
shipment.addBook(bookTitle);
}
file.close();
}
int main(){
BooksReceived shipment;
// Prompt the user for the file name
string fileName;
cout "Enter the file name: ";
getline(cin, fileName);
// Read data from the file
readDataFromFile(shipment, fileName);
// Display the shipment information
shipment.displayBooksReceivedInfo();
return 0;
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sql++ For Sql Users A Tutorial

Authors: Don Chamberlin

1st Edition

0692184503, 978-0692184509

More Books

Students also viewed these Databases questions

Question

Recognize who fits under the diversity umbrella.

Answered: 1 week ago