Question
//books.cpp #include books.hpp #include #include using std::string; using std::ostream; using std::istream; using std::cout; using std::cin; // Getters and setters for each of the private fields.
//books.cpp
#include "books.hpp"
#include
#include
using std::string;
using std::ostream;
using std::istream;
using std::cout;
using std::cin;
// Getters and setters for each of the private fields.
int Book::getEdition() const {
return edition;
}
/**
* @brief Set the Edition object
*
* @param edition is the edition number of the book
*/
void Book::setEdition( int edition ) {
this->edition = edition;
}
/**
* @brief Get the Author object
*
* @return string author name
*/
string Book::getAuthor() const {
return authorName;
}
/**
* @brief Set the authorName object
*
* @param aName is the author name
*/
void Book::setAuthor( string aName ) {
authorName = aName;
}
/**
* @brief Get the Title object
*
* @return string as the book title
*/
string Book::getTitle() const {
return title;
}
/**
* @brief Set the Title object
*
* @param tName is the title name
*/
void Book::setTitle( string tName ) {
title = tName;
}
/**
* @brief Get the ID object
*
* @return string
*/
string Book::getID() const {
return id;
}
/**
* @brief Set the ID object
*
* @param sID is the 6-character book ID
*/
void Book::setID( string sID ) {
if( id.length() == 6 ) id = sID;
}
/**
* @brief Get the ISBN
*
* @return string is the ISBN-10
*/
string Book::getISBN() const {
return isbn;
}
/**
* @brief Sets the ISBN
*
* @param sIsbn is an ISBN-10 value
*/
void Book::setISBN( string sIsbn ) {
isbn = sIsbn;
}
/**
* @brief Convenience operator
*
* @param os is the output stream.
* @param s is the Book object.
* @return ostream& is the output stream
*/
ostream& operator
os
return os;
}
istream& operator>>( istream &is, Book &s ) {
string str="";
getline( is, str );
if( is.fail() ) {
return is;
}
std::stringstream jStream( str );
string id="", author="", title="", isbn="";
string edition="", year="";
getline( jStream, id, ',' );
getline( jStream, author, ',' );
getline( jStream, title, ',' );
getline( jStream, edition, ',' );
getline( jStream, year, ',' );
getline( jStream, isbn );
Book newBook( id, author, title, stoi(edition), stoi(year), isbn );
s = newBook;
return is;
}
/**
* Default constructor.
*/
Book::Book() {
id=authorName=title=isbn="";
edition=1;
year=1970;
}
/**
* Constructor with initializers.
*/
Book::Book( string sid, string fName, string lName, int edition, int year, string isbn ) {
id=sid;
authorName=fName;
title=lName;
this->isbn=isbn;
this->edition=edition;
this->year = year;
}
//books.hpp
#ifndef __BOOKS_HPP_
#define __BOOKS_HPP_
#include
#include
using std::string;
using std::ostream;
using std::istream;
class Book {
string id; // a six-digit numeric ID. Can begin with 0, so we need a string to store.
string authorName; // Author name
string title; // book title
int edition; // edition
int year; // year published
string isbn; // ISBN 10-digit
public:
Book();
Book( string sid, string fName, string lName, int edition, int year, string isbn );
int getEdition() const;
void setEdition( int edition );
string getAuthor() const;
void setAuthor( string aName );
string getTitle() const;
void setTitle( string title );
string getID() const;
void setID( string sID );
int getYear() const;
void setYear( int year );
string getISBN() const;
void setISBN( string sIsbn );
/**
* @brief Convenience operator
*
* @param os is the output stream.
* @param s is the Book object.
* @return ostream& is the output stream
*/
friend ostream& operator
friend istream& operator>>( istream &is, Book &s );
/**
* @brief Sorts an array of books based on their title
*
* @param bookList Array of Book objects.
* @param size Number of Book items in the array.
*/
friend void Sort( Book bookList[], int size, short field ); // Method to sort books by title
};
#endif
//books_main.cpp
#include "books.hpp"
#include "books.cpp"
#include
using std::cerr;
using std::cout;
using std::cin;
using std::endl;
/**
* @brief Swaps two book objects.
*
* @param a Book one
* @param b Book two
*/
template
void Swap( T &a, T &b ) {
T temp =a;
a=b;
b=temp;
}
/**
* @brief Crude but correct method to sort an array of books having size items.
*
* @param bookList The array of books to sort.
* @param size Number of Books in the array.
*/
void Sort( Book bookList[], int size, short field ) {
for( int i = 0; i
for( int j = i+1; j
switch( field ) {
case 1:
if( bookList[i].id > bookList[j].id )
Swap( bookList[i], bookList[j] );
break;
case 2:
if( bookList[i].authorName > bookList[j].authorName )
Swap( bookList[i], bookList[j] );
break;
case 3:
if( bookList[i].title > bookList[j].title )
Swap( bookList[i], bookList[j] );
break;
case 4:
if( bookList[i].edition > bookList[j].edition )
Swap( bookList[i], bookList[j] );
break;
case 5:
if( bookList[i].year > bookList[j].year )
Swap( bookList[i], bookList[j] );
break;
case 6:
if( bookList[i].isbn > bookList[j].isbn )
Swap( bookList[i], bookList[j] );
break;
}
}
}
}
int LoadDatabase( Book *books) {
std::ifstream infile("bookinput.dat");
if( infile.fail() )
return -1;
string iString=""; // book descriptor string read from a database.
Book newBook;
int bookNum=0; /umber of books in the input file.
while( infile >> newBook ) {
//Book newBook = BookMaker( iString );
books[bookNum++] = newBook;
}
if (bookNum==0 ) { // Empty file
cout
return 0;
}
infile.close();
return bookNum;
}
bool FlushToDatabase( Book *books, int size ) {
std::ofstream outFile("bookinput.dat");
if( !outFile ) {
cout
return false;
}
for( int i=0; i
outFile
}
outFile.close();
return true;
}
int main() {
std::ofstream outfile("bookoutput.dat");
if( outfile.fail() ) {
cerr
return 1;
}
char choice = 'y';
Book library[200]; //library that can hold 200 books.
int bookNum = LoadDatabase( library );
while( toupper( choice ) != 'Q' ) {
cout
cin >> choice;
cin.ignore();
switch( toupper(choice) ) {
case 'A': /* Add record */
{ cout
Book newBook;
cin >> newBook;
library[bookNum++]=newBook;
cout
break;
}
case 'F': /* Find and print record */
{
cout
string lName;
cin >> lName;
int i=0;
bool found=false;
for( i=0; i if( library[i].getAuthor().find( lName ) != std::string::npos ) { cout found = true; } } if( !found ) cout break; } case 'P': /* Print all records */ { for( int i=0; i cout } break; } case 'S': /* Sort and print records, also send to output file */ { Book *sortedBooks = new Book[bookNum]; for( int i=0; i sortedBooks[i] = library[i]; char sortChoice =' '; cout cin >> sortChoice; cin.ignore(); switch( sortChoice ) { case '1': Sort( sortedBooks, bookNum, 1 ); break; case '2': Sort( sortedBooks, bookNum, 2 ); break; case '3': Sort( sortedBooks, bookNum, 3 ); break; case '4': Sort( sortedBooks, bookNum, 4 ); break; case '5': Sort( sortedBooks, bookNum, 5 ); break; case '6': Sort( sortedBooks, bookNum, 6 ); break; default: break; } for( int i=0; i outfile cout } delete [] sortedBooks; break; } default: break; } } FlushToDatabase(library, bookNum ); outfile.close(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started