Objectives: class associations, arrays, operator overloading, sort, and multiple
ID: 3607196 • Letter: O
Question
Objectives: class associations, arrays, operator overloading, sort, and multiple file l/Os This is the first iteration on the development of a textbook warehouse inventory management system. This iteration requires a minimum of two user-defined classes. A Book class, with the required interfaces shown below, is used to represent each book in the warehouse. Each is required to have the following attributes: title, one or more authors (no more than 20), publisher, year of publication, cover type (hard/paperback), price, ISBN, and number of available copies. The Book class uses its overloaded stream extraction operator >>) to retrieve its attributes from the provided database. The Book class also uses its overloaded stream insertion operator () to retrieve all the books from the provided database and it uses its overloaded stream insertion operator (Explanation / Answer
// C++ Solution :- Most functions of class book and class Warehouse has been implemented. Currently I have hardcoded the file name that contains book records . If you give information about usage of search list, I can take the data from that file. In the main function , you just need to create object of Warehouse , use cin>>objectName . By doing this , warehouse object will fetch all the records from the file using friend operator << (istream& , WareHouse& ) and sort it in ascending order . Please go through the code and let me know if you have any doubts.
#include<bits/stdc++.h>
using namespace std;
// book.cpp
class Book
{
string title;
string author;
string publisher;
bool hardCover;
float price;
string isbn;
long copies;
public:
Book(){
};
~Book(){
};
friend istream& operator >> (istream& , Book&);
friend ostream& operator << (ostream& ,const Book&);
string getISBN () const;
bool operator == (const Book&) const;
friend bool operator > (const Book& ,const Book&) const;
Book operator = (const Book&);
};
bool operator > (const Book& book1, const Book& book2) const{
{
if((book1.isbn).compare((book2.isbn)) > 0)
return true;
else
return false;
}
bool Book :: operator = (const Book& book)
{
title = book.title;
author = book.author;
publisher = book.publisher;
hardCover = book.hardCover;
price = book.price;
isbn = book.isbn;
copies = book.copies;
}
istream& operator >> (istream &input , Book& book)
{
input >> book.title
>> book.author
>> book.publisher
>> book.hardCover
>> book.price
>> book.isbn
>> book.copies;
return input;
}
ostream& operator << (ostream &out , Book& book)
{
out<< book.title<<" "
<< book.author<<" "
<< book.publisher<<" "
<< (book.hardCover==true)?"hard":"PaperBack"<<" "
<< book.price<<" "
<< book.isbn<<" "
<< book.copies<<" ";
return out;
}
// Warehouse.cpp
bool compare(const Book& book1 , const Book& book2)
{
return book1 > book2 ? 0 : 1;
}
class WareHouse{
Book arr[512];
int size;
void sort_(){
sort(arr, arr+size; compare);
}
public:
WareHouse(){
size = 0;
}
~WareHouse(){
}
friend istream& operator >> (istream& , WareHouse& );
friend ostream& operator << (istream& , WareHouse& );
bool find(string isbn , Book& book) const;
bool find(string year , Book& book) const;
};
friend istream& operator >> (istream& input , WareHouse& wH)
{
ifstream ifile;
ifile.open("data.txt",ios :: in);
if(!ifile.is_open())
{
cerr << "There was an error opening the input file! ";
exit(0);
}
Book book;
int i=0;
while(ifile >> book)
{
wH.arr[i++] = book;
}
size = i;
sort_();
return input;
}
friend ostream& operator << (istream& out , WareHouse& wH)
{
for(int i=0;i<size;i++)
cout<<wh.arr[i]<<" ";
}
bool WareHouse :: find(string isbn, Book& book) const {
for(int i=0;i<size;i++)
{
if((arr[i].isbn).compare(isbn) == 0)
{
book = arr[i];
return true;
}
}
return false;
}
int WareHouse :: find(string year, Book& book[])
{
int j=0;
for(int i=0;i<size;i++)
{
if((arr[i].year).compare(year)==0)
{
book[j++] = arr[i];
}
}
return j;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.