Question

C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header files given. Then make a main program using Book and Warehouse to read data from book.dat and have functions to list and find book by isbn

Objectives: Class Association and operator overloading This project is a continuation from Project 1. The program should acceBook ) Book (string title, string authors [], int authorCount, string publisher, short yearPublish, bool hardcover, float pri

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// File Book.h

#ifndef BOOK_H
#define BOOK_H

#include <iostream>
#include <string>
using namespace std;

// Class Book definition
class Book
{
/**
* @param is the input stream
* @param book is the book object to be filled
* @return the input stream
*/
friend istream& operator >> (istream & is, Book &book);

/**
* @param os the output stream
* @param book is the book object reference *
* @return the output stream
*/
friend ostream& operator << (ostream os, const Book &book);

public:
static const int MAX_AUTHORS = 35;

// Prototype of member functions
Book();
Book(string title, string authorsp[], int authorCount, string publisher, short yearPublish,
bool hardcover, float price, string isbn, long copies);

void setTitle(string);
void setNumberOfAuthor(int);
void setAuthor(string, int);
void setPublisher(string);
void setYear(int);
void setCover(bool);
void setPrice(double);
void setISBN(string);
void setCopies(long);

string getTitle();
int getNumberOfAuthor();
string getAuthors(int);
string getPublisher();
int getYear();
string getCover();
double getPrice();
string getISBN();
long getCopies();

private:
// Data members
string title;
int numberOfAuthor;
string author[Book::MAX_AUTHORS];
string publisher;
int year;
string cover;
double price;
string ISBN;
long copies;
};// End of class

#endif // BOOK_H

-------------------------------------------------------------------------------------------------------------------------------

// File Book.cpp
#include <cstring>
#include "Book.h"
using namespace std;

// Default constructor to assign default values to data member
Book::Book()
{
title = "";
numberOfAuthor = 0;
publisher = "";
year = 0;
cover = "";
price = 0.0;
ISBN = "";
copies = 0;
}// End of constructor

// Parameterized constructor to assign parameter data to data member
Book::Book(string title, string authorsp[], int authorCount, string publisher, short yearPublish,
bool hardcover, float price, string isbn, long copies)
{
char buffer[50];
this->title = title;
for(int x = 0; x < authorCount; x++)
author[x] = authorsp[x];
this->publisher = publisher;
this->year = yearPublish;
if(hardcover == true)
cover = "paperback";
else
cover = "clear";
this->price = price;
this->ISBN = isbn;
this->copies = copies;
}// End of constructor

// Function to set book title
void Book::setTitle(string ti)
{
title = ti;
}// End of function

// Function to set number of authors
void Book::setNumberOfAuthor(int no)
{
numberOfAuthor = no;
}// End of function

// Function to set author name
void Book::setAuthor(string au, int pos)
{
author[pos] = au;
}// End of function

// Function to set publisher name
void Book::setPublisher(string pu)
{
publisher = pu;
}// End of function

// Function to set year
void Book::setYear(int ye)
{
year = ye;
}// End of function

// Function to set cover information
void Book::setCover(bool hardcover)
{
// Checks if hardcover value is true then set cover as "paperback"
if(hardcover == true)
cover = "paperback";
// Otherwise hardcover value is false then set cover as "clear"
else
cover = "clear";
}// End of function

// Function to set price
void Book::setPrice(double pr)
{
price = pr;
}// End of function

// Function to set ISBN number
void Book::setISBN(string isbn)
{
ISBN = isbn;
}// End of function

// Function to set number of copies
void Book::setCopies(long co)
{
copies = co;
}// End of function

// Function to return book title
string Book::getTitle()
{
return title;
}// End of function

// Function to return number of authors
int Book::getNumberOfAuthor()
{
return numberOfAuthor;
}// End of function

// Function to return author name at pos index position
string Book::getAuthors(int pos)
{
return author[pos];
}// End of function

// Function to return publisher name
string Book::getPublisher()
{
return publisher;
}// End of function

// Function to return year
int Book::getYear()
{
return year;
}// End of function

// Function to return cover information
string Book::getCover()
{
return cover;
}// End of function

// Function to return price
double Book::getPrice()
{
return price;
}// End of function

// Function to return ISBN number
string Book::getISBN()
{
return ISBN;
}// End of function

// Function to return number of copies
long Book::getCopies()
{
return copies;
}// End of function

----------------------------------------------------------------------------------------------------------------------------------

// File: Warehouse.h

#ifndef WAREHOUSE_H
#define WAREHOUSE_H

#include <iostream>
#include <string>
#include "Book.h"
using namespace std;

// Defines class Warehouse
class Warehouse
{
/**
* @param is the input stream
* @param warehouse is the warehouse object reference
* @return the input stream
*/
friend istream& operator >> (istream & is, Warehouse &warehouse);

/**
* @param is the input stream
* @param warehouse is the warehouse object reference
* @return the input stream
*/
friend ostream& operator << (ostream & os, Warehouse &warehouse);
public:
static const int MAX_BOOKS = 35;
Warehouse();

/**
* @param isbn the ISBN number to search for
* if found
* @return true if found.
*/
bool find (string isbn);

/**
* Prints the inventory of the Warehouse (i.e., list all the books)
*/
void list(Book &book) const;
private:
void sort_();
private:
Book books[Warehouse::MAX_BOOKS];
int bookCount;
};// End of class

#endif // WAREHOUSE_H

----------------------------------------------------------------------------------------------------------------------------------

// File warehouse.cpp

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <string>

#include "Book.cpp"

#include "Warehouse.h"

using namespace std;

Warehouse::Warehouse()

{

bookCount = 0;

}// End of constructor

/**

* @param is the input stream

* @param warehouse is the warehouse object reference

* @return the input stream

*/

istream& operator >> (istream & is, Warehouse &warehouse)

{

// To store data read from file

int intData;

string strData;

double doubleData;

long longData;

bool boolData;

// ifstream object declared

ifstream readF;

// Opens the file for reading

readF.open("book.dat");

// Checks whether the file can be opened or not

if(readF.fail())

{

// Displays error message if unable to open

cout<<"\n The input file failed to open.";

exit(1);

}// End of if condition

// Loops till end of the file

while(!readF.eof())

{

// Reads title from file

getline(readF, strData);

// Calls the function to set book title

warehouse.books[warehouse.bookCount].setTitle(strData);

// Reads number of authors from file

readF>>intData;

// Calls the function to set number of authors

warehouse.books[warehouse.bookCount].setNumberOfAuthor(intData);

// Reads new line character from file

getline(readF, strData, '\n');

// Loops till number of authors

for(int x = 0; x < intData; x++)

{

// Reads author name from file

getline(readF, strData);

// Calls the function to set author name

warehouse.books[warehouse.bookCount].setAuthor(strData, x);

}// End of for loop

// Reads publisher name from file

getline(readF, strData);

// Calls the function to set publisher name

warehouse.books[warehouse.bookCount].setPublisher(strData);

// Reads year from file

readF>>intData;

// Calls the function to set year

warehouse.books[warehouse.bookCount].setYear(intData);

// Reads cover information from file

readF>>boolData;

// Calls the function to set cover information

warehouse.books[warehouse.bookCount].setCover(boolData);

// Reads price from file

readF>>doubleData;

// Calls the function to set price

warehouse.books[warehouse.bookCount].setPrice(doubleData);

// Reads ISBN number from file

readF>>strData;

// Calls the function to set ISBN number

warehouse.books[warehouse.bookCount].setISBN(strData);

// Reads number of copies from file

readF>>longData;

// Calls the function to set number of copies

warehouse.books[warehouse.bookCount].setCopies(longData);

// Reads new line character from file

getline(readF, strData, '\n');

// Increase the book count by one

warehouse.bookCount++;

}// End of while loop

// Returns the input stream

return is;

}// End of function

/**

* @param is the input stream

* @param warehouse is the warehouse object reference

* @return the input stream

*/

ostream& operator << (ostream &os, Warehouse &warehouse)

{

// Calls the function to sort

warehouse.sort_();

// Loops till number of books

for(int y = 0; y < warehouse.bookCount; y++)

{

// Calls the function to display book title

os<<"\n Title: "<<warehouse.books[y].getTitle();

// Calls the function to display number of authors

os<<"\n No. authors: "<<warehouse.books[y].getNumberOfAuthor();

// Loops till number of authors

for(int x = 0; x < warehouse.books[y].getNumberOfAuthor(); x++)

// Calls the function to display author name

os<<"\n Author: "<<warehouse.books[y].getAuthors(x);

// Calls the function to display publisher name

os<<"\n Publisher: "<<warehouse.books[y].getPublisher();

// Calls the function to display year

os<<"\n Year: "<<warehouse.books[y].getYear();

// Calls the function to display cover information

os<<"\n Cover: "<<warehouse.books[y].getCover();

// Calls the function to display price

os<<"\n Price: "<<warehouse.books[y].getPrice();

// Calls the function to display ISBN number

os<<"\n ISBN: "<<warehouse.books[y].getISBN();

// Calls the function to display number of copies

os<<"\n copies: "<<warehouse.books[y].getCopies();

os<<endl;

}// End of for loop

// Returns ostream object

return os;

}// End of function

/**

* @param isbn the ISBN number to search for

* @param book reference to the matched book object, if found

* @return true if found.

*/

bool Warehouse::find (string isbn)

{

// Loops till number of books

for(int y = 0; y < bookCount; y++)

{

// Checks if current book ISN is equals to parameter ISBN

if(books[y].getISBN().compare(isbn) == 0)

{

// Calls the function to display book information

list(books[y]);

// returns true for found

return true;

}// End of if condition

}// End of for loop

// Returns false for not found

return false;

}// End of function

/**

* Prints the inventory of the Warehouse (i.e., list all the books)

*/

void Warehouse::list(Book &book) const

{

// Calls the function to display book title

cout<<"\n Title: "<<book.getTitle()<<" ---- Found";

// Calls the function to display number of authors

cout<<"\n No. authors: "<<book.getNumberOfAuthor();

// Loops till number of authors

for(int x = 0; x < book.getNumberOfAuthor(); x++)

// Calls the function to display author name

cout<<"\n Author: "<<book.getAuthors(x);

// Calls the function to display publisher name

cout<<"\n Publisher: "<<book.getPublisher();

// Calls the function to display year

cout<<"\n Year: "<<book.getYear();

// Calls the function to display cover information

cout<<"\n Cover: "<<book.getCover();

// Calls the function to display price

cout<<"\n Price: "<<book.getPrice();

// Calls the function to display ISBN

cout<<"\n ISBN: "<<book.getISBN();

// Calls the function to display number of copies

cout<<"\n copies: "<<book.getCopies();

cout<<endl;

}// End of function

// Function to sort the books information based on book title descending order

void Warehouse::sort_()

{

// Book object for swapping

Book temp;

// Loops till number of books

for(int y = 0; y < bookCount; y++)

{

// Loops till number of books minus outer loop value minus one

// Outer loop value is subtracted because after each iteration one record sorted

// minus one because we need two record to compare

for(int x = 0; x < bookCount - y - 1; x++)

{

// Checks if current book title is greater then the next book title

if(books[x].getTitle().compare(books[x + 1].getTitle()) > 0)

{

// Swapping process

temp = books[y];

books[y] = books[y + 1];

books[y + 1] = temp;

}// End of if condition

}// End of inner for loop

}// End of outer for loop

}// End of function

----------------------------------------------------------------------------------------------------------------------------------

#include <iostream>
#include "Warehouse.cpp"
using namespace std;

// main function definition
int main()
{
// To store isbn number entered by the user to search
string isbn;
// Declares an object of the class Warehouse
Warehouse wa;

// Reads books information from file
cout<<"\n Reads books information from file.";
cin>>wa;

// Displays all the books information
cout<<"\n List of all books. \n Sorted on BOOK TITLE.";
cout<<wa;

// Accepts the isbn number to search
cout<<"\n Enter a ISBN number to search: ";
cin>>isbn;

// Calls the function to search if not found then display message
if(!wa.find(isbn))
cout<<isbn<<" --- Not found.";

return 0;
}// End of main function

Sample Output:

Reads books information from file.
List of all books.
Sorted on BOOK TITLE.
Title: Programming Ruby
No. authors: 2
Author: David Thomas
Author: Andrew Hunt
Publisher: Addison-wesley
Year: 2001
Cover: clear
Price: 42.95
ISBN: 0-201-71089-7
copies: 123

Title: Problem Solving with C++ - The Object of Programming
No. authors: 1
Author: Walter Savitch
Publisher: Addison Wesley Longman, Inc.
Year: 2001
Cover: clear
Price: 74
ISBN: 0-201-70390-4
copies: 325

Title: C++ Network Programming - Volume 1
No. authors: 2
Author: Douglas C. Schmidt
Author: Stephen D. Huston
Publisher: Addison-wesley
Year: 2002
Cover: clear
Price: 35.99
ISBN: 0-201-60464-7
copies: 236

Enter a ISBN number to search: 0-201-70390-4

Title: Problem Solving with C++ - The Object of Programming ---- Found
No. authors: 1
Author: Walter Savitch
Publisher: Addison Wesley Longman, Inc.
Year: 2001
Cover: clear
Price: 74
ISBN: 0-201-70390-4
copies: 325

book.dat file contents

C++ Network Programming - Volume 1
2
Douglas C. Schmidt
Stephen D. Huston
Addison-wesley
2002
0
35.99
0-201-60464-7
236
Programming Ruby
2
David Thomas
Andrew Hunt
Addison-wesley
2001
0
42.95
0-201-71089-7
123
Problem Solving with C++ - The Object of Programming
1
Walter Savitch
Addison Wesley Longman, Inc.
2001
0
74.00
0-201-70390-4
325

Add a comment
Know the answer?
Add Answer to:
C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objectives You will implement and test a class called MyString. Each MyString object keeps track ...

    Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...

  • BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to...

    BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to sort movies by their quality. In this assignment, you will collect a list of movies and then a list of reviews for each movie. You will then take a simple average (total of reviews divided by number of reviews) for each movie and output a sorted list of movies with their average review. Here you are provided the shell of a program and asked...

  • For this lab you will be creating a class representing the shape square. A Square is...

    For this lab you will be creating a class representing the shape square. A Square is a special case of a Rectangle where both sides have the same length. Rectangle has been provided for your use in this lab. A Rectangle has a height and a width as member variables, two constructors, two member functions, area() and perimeter(), and lastly, an input and output operator. Your Square class should publicly inherit from Rectangle. You will need to update the Constructors...

  • Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp,...

    Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp, and a test file to test the functionality of the class. Hint: read all the explanations in the header with attention. MyArray.h : #ifndef MYARRAY_H #define MYARRAY_H #include <iostream> using namespace std; class MyArray { friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array friend istream& operator>>( istream & input, MyArray & rhs); // to...

  • In this lab, you will need to implement the following functions in Text ADT with C++...

    In this lab, you will need to implement the following functions in Text ADT with C++ language(Not C#, Not Java please!): PS: The program I'm using is Visual Studio just to be aware of the format. And I have provided all informations already! Please finish step 1, 2, 3, 4. Code is the correct format of C++ code. a. Constructors and operator = b. Destructor c. Text operations (length, subscript, clear) 1. Implement the aforementioned operations in the Text ADT...

  • C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve...

    C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve very big integer calculations that are outside the limit of all available primitive data types. For example, factorial of 100 contains 158 digits in it so we can’t store it in any primitive data type available. We can store as large Integer as we want in it. Your goal is to overload the operators for a generic “BigInt” class. You will need to write...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • The task is to write Song.cpp to complete the implementation of the Song class, as defined...

    The task is to write Song.cpp to complete the implementation of the Song class, as defined in the provided header file, Song.h, and then to complete a program (called sales) that makes use of the class. As in Project 1, an input data file will be provided containing the song name and other information in the same format as in Program 1: Artist    Song_Title    Year    Sales    Medium As before, the program (sales) which are individual copies, will use this information to compute the gross...

  • I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string>...

    I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string> using namespace std; class Triple { private: int a, b, c; public: Triple(); // all elements have value 0 Triple(int k); // all elements have value k Triple(int x, int y, int z); // specifies all three elements Triple(string s); // string representation is "(a,b,c)" string toString(); // create a string representation of the vector void fromString(string s); // change the vector to equal...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT