Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//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.

image text in transcribed

image text in transcribed

//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;

}

Problem: An even "Beefier" Book Database This homework will carry on our work on the book database. A solution file for homework 9 is going to be provided, in case you need it. However, you are not required to use this, as long as your own solution is complete and 100% correct as per the requirements of homework 9. 1. Program Tasks ("What do I have to do exactly?"): In this homework, you will extend your book database from homework 9 in the following way: Add a feature to delete a record from the book database. The book to be deleted can be searched for by any attribute of the book. This means, the user may want to search for the book to be deleted by ID, title, author name, edition, year or ISBN number. Only one attribute will be used to search for the book. 1. 2. The search to find the book to be deleted will be conducted by the binary search algorithm (as opposed to linear search which you already have done in homework 9) which we have discussed in class. 3. These two features will be added to the program that you completed in homework 9. As mentioned, a complete solution is already given, so you just have to add these two features. 2. Input/output specifications: The input file specification is the same as the one from homework 9. You can use your old bookinput.dat files 3. Program Operation: It will be identical to that of homework 9. In addition, an extra menu option will be added for deleting records 1 https://xlinux.nist.gov/dads/HTML/binarySearch.html Problem: An even "Beefier" Book Database This homework will carry on our work on the book database. A solution file for homework 9 is going to be provided, in case you need it. However, you are not required to use this, as long as your own solution is complete and 100% correct as per the requirements of homework 9. 1. Program Tasks ("What do I have to do exactly?"): In this homework, you will extend your book database from homework 9 in the following way: Add a feature to delete a record from the book database. The book to be deleted can be searched for by any attribute of the book. This means, the user may want to search for the book to be deleted by ID, title, author name, edition, year or ISBN number. Only one attribute will be used to search for the book. 1. 2. The search to find the book to be deleted will be conducted by the binary search algorithm (as opposed to linear search which you already have done in homework 9) which we have discussed in class. 3. These two features will be added to the program that you completed in homework 9. As mentioned, a complete solution is already given, so you just have to add these two features. 2. Input/output specifications: The input file specification is the same as the one from homework 9. You can use your old bookinput.dat files 3. Program Operation: It will be identical to that of homework 9. In addition, an extra menu option will be added for deleting records 1 https://xlinux.nist.gov/dads/HTML/binarySearch.html

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

Step: 3

blur-text-image

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

Database Management With Website Development Applications

Authors: Greg Riccardi

1st Edition

0201743876, 978-0201743876

More Books

Students also viewed these Databases questions

Question

=+1. Do you have insurance?

Answered: 1 week ago

Question

2. Identify four types of migrant groups.

Answered: 1 week ago

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago