Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ program Write a C++ program You have to implement a searchable textbook data

ID: 3719443 • Letter: C

Question

C++ program

Write a C++ program You have to implement a searchable textbook database. This will allow the user to search for the author (or ISBN or publisher, etc.) for a book with a particular title, as well as finding all books by a particular author, among other things. You will implement 2 classes: Book and BookDatabase The Book class must contain: A string for the title and publisher, an integer for the year of publication, and a vector (or list, or set) of strings for the author. (You can choose whichever one you want (You may write as many private functions as you would like to help your implementation.) Book(const string&title;, const string&publisher;, int yearOfPublication, string" authors, int numAuthors); This is the book constructor t l initialize all the member variables and al so store all of the authors names in the container that will hold these names bool operator (Book&bl;, Book& b2); This will return true iff the two Books have the same title. (You can assume that the database will not contain 2 books with exactly the same title bool operator(Book&bl;, Book& b2); WThis will return true iff the title of bookl is lexicographically before the title of book2. This will print a representation for a Book ostream& operator

Explanation / Answer

//Books.h

#pragma once

#ifndef BOOKS_H

#define BOOKS_H

#include<iostream>

#include<string>

#include<vector>

using namespace std;

class Book

{

public:

Book(const string &title, const string &publisher, int yearOfPublication, string *authors, int numOfAuthors);

~Book();

bool operator==(Book &b);

bool operator <(Book &b);

ostream& operator <<(ostream &os);

vector<string> getAuthors();

int getYear();

string getTitle();

string getPublisher();

private:

string title;

string publisher;

int year;

vector<string> authors;

};

#endif // !BOOKS_H

//Books.cpp

#include"Books.h"

using namespace std;

Book::Book(const string &title, const string &publisher, int yearOfPublication, string *authors, int numOfAuthors)

{

this->title = title;

this->publisher = publisher;

this->year = yearOfPublication;

for (int i = 0; i < numOfAuthors; i++)

{

this->authors.push_back(authors[i]);

}

}

Book::~Book()

{

}

vector<string> Book::getAuthors()

{

return this->authors;

}

int Book::getYear()

{

return year;

}

string Book::getTitle()

{

return title;

}

string Book::getPublisher()

{

return publisher;

}

bool Book::operator==(Book &b)

{

return this->title == b.title;

}

bool Book::operator<(Book & b)

{

return this->title < b.title;

}

ostream & Book::operator<<(ostream & os)

{

os << "Title: " << this->title << endl;

for (int i = 0; i < this->authors.size(); i++)

os << this->authors[i] << " ";

os << endl;

os << "publisher: " << this->publisher << endl;

os << "Year: " << this->year << endl;

return os;

}

//BookDatabase.h

#ifndef BOOKDATABASE_H

#define BOOKDATABASE_H

#include<iostream>

#include<string>

#include<vector>

#include<set>

#include<map>

#include"Books.h"

using namespace std;

class BookDatabase

{

public:

Book & searchBtTitle(const string& title);

set<Book> searchByAuthor(const string& author);

set<Book> publishedBefore(int year);

set<Book> searchByPublisher(const string& author);

void addBook(Book &book);

void removeBook(Book &book);

ostream& operator <<(ostream &os);

BookDatabase();

~BookDatabase();

private:

map <string,Book> bookData;

};

BookDatabase::BookDatabase()

{

}

BookDatabase::~BookDatabase()

{

}

#endif // !BOOKDATABASE_H

//BookDatabase.cpp

#include "BookDatabase.h"

Book & BookDatabase::searchBtTitle(const string & title)

{

// TODO: insert return statement here

for (auto it = bookData.begin(); it != bookData.end(); it++)

{

if (it->first == title)

return it->second;

}

}

set<Book> BookDatabase::searchByAuthor(const string & author)

{

set<Book> s;

for (auto it = bookData.begin(); it != bookData.end(); it++)

{

Book b = it->second;

vector<string> authors = b.getAuthors();

for (auto itr = authors.begin(); itr != authors.end(); itr++)

{

if (*itr == author)

{

s.insert(b);

break;

}

}

}

return s;

}

set<Book> BookDatabase::publishedBefore(int year)

{

set<Book> s;

for (auto it = bookData.begin(); it != bookData.end(); it++)

{

Book b = it->second;

if (b.getYear() == year)

s.insert(b);

}

return s;

}

set<Book> BookDatabase::searchByPublisher(const string & publisher)

{

set<Book> s;

for (auto it = bookData.begin(); it != bookData.end(); it++)

{

Book b = it->second;

if (b.getPublisher() == publisher)

s.insert(b);

}

return s;

}

void BookDatabase::addBook(Book & book)

{

bookData.insert(std::pair<string,Book>(book.getTitle(), book));

}

void BookDatabase::removeBook(Book & book)

{

bookData.erase(book.getTitle());

}

ostream & BookDatabase::operator<<(ostream & os)

{

// TODO: insert return statement here

for (auto it = bookData.begin(); it != bookData.end(); it++)

{

Book b = it->second;

os << "Title: " << b.getTitle() << endl;

vector<string> authors = b.getAuthors();

for (int i = 0; i < authors.size(); i++)

os << authors[i] << " ";

os << endl;

os << "publisher: " << b.getPublisher() << endl;

os << "Year: " << b.getYear() << endl;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote