Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include #include Book.h using namespace std; namespace sdds { Book::Book() { m_author = ; m_title = ; m_country = ; m_year =

#include
#include
#include
#include
#include "Book.h"
using namespace std;
namespace sdds {
Book::Book() {
m_author = "";
m_title = "";
m_country = "";
m_year = 0;
m_price = 0;
m_description = "";
}
Book::Book(const string& strBook) {
string bookText = strBook;
//format: AUTHOR,TITLE,COUNTRY,PRICE,YEAR,DESCRIPTION
//author
m_author = bookText.substr(0, bookText.find(','));
trim(m_author);
//title
string title = bookText.erase(0, bookText.find(',') + 1);
m_title = title.substr(0, title.find(','));
trim(m_title);
//country
string country = title.erase(0, title.find(',') + 1);
m_country = country.substr(0, country.find(','));
trim(m_country);
//price
string price = country.erase(0, country.find(',') + 1);
m_price = stod(price.substr(0, price.find(',')));
//year
string year = price.erase(0, price.find(',') + 1);
m_year = stoi(year.substr(0, year.find(',')));
//description
string description = price.erase(0, price.find(',') + 1);
m_description = description.substr(0, description.find('.')+1);
trim(m_description);
}
void Book::trim(string& strBook) {
strBook.erase(0, strBook.find_first_not_of(" "));
strBook.erase(strBook.find_last_not_of(" ") + 1);
}
const string& Book::title() const {
return m_title;
}
const string& Book::country() const {
return m_country;
}
const size_t& Book::year() const {
return m_year;
}
double& Book::price() {
return m_price;
}
ostream& operator
os
return os;
}
}
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
I have to a main function to test code can u request that i add more info so i can post it
programjng language C++
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
At-Home The at-home part of this workshop upgrades your in-lab solution to include more modules: Movie Spellchecker Collection Spell Checker Module (functor) Add a Spellchecker module to your project. This module should maintain two arrays of strings, both of size 5 (statically allocated): m_badWords : an array with 5 misspelled words m_goodWords: an array with the correct spelling of those 5 words Public Members Spellchecker(const char* filename); : a constructor that receives as a parameter the name of the file that contains the misspelled words. If the file is missing, this constructor should generate an exception of type const char* , with the message Bad file name! this constructor should load the content of the file. Each line from the file is in the format BAD_WORD GOOD_WORD ; the two fields can be separated by any number of spaces. void operator() (std::string& text) const o this operator should search in text if any of the misspelled words appear and replace them with the correct version. When implementing the operator, consider the following functions: When implementing the operator, consider the following functions: std::string::find() std::string::replace() Book Module Add to the Book class a public template function: void fixSpelling (T spellchecker) : this function should call the overloaded operator() on instance spellchecker passing to it the book description. In this design, type T must have an overload of the operator() that accepts a string as a parameter Since this is a template function, it must be implemented in the header! The class is not a template. Movie Module Design and code a class named Movie that should be able to store the following information (for each attribute, you can chose any type you think it's appropriate--you must be able to justify the decisions you have made): . title the year of release the description Public Members a default constructor const std::string&title() const: a query that returns the title of the movie Movie(const std::string& strMovie) : A constructor that receives the movie as a string; this constructor is responsible to extract the information about the movie from the parameter and store it in the attributes of the instance. The parameter to extract the information about the movie from the parameter and store it in the attributes of the instance. The parameter will always have the following format: TITLE, YEAR, DESCRIPTION This constructor should remove all spaces from the beginning and end of any token in the string. When implementing the constructor, consider the following functions: std::string: substr() std::string::find) std::string::erasel) std::stoi void fixSpelling(T spellchecker): a template function. This function should call the overloaded operator() on instance spellchecker , passing to it the movie title and description. In this design, type T must have an overload of the operator() that accepts a string as a parameter Since this is a template function, it must be implemented in the header! The class is not a template. Add any other function that is required by your design! Friend Helpers overload the insertion operator to insert the content of a movie object into an ostream object, in the following format: TITLE | YEAR DESCRIPTION the title should be printed on a field of size 40; the year should be printed on a field of size 4; Collection Module Add a Collection module to your project. The purpose of this class is to manage a collection items of template type T. Since this is template class, it doesn't need a .cpp file. This module should manage a dynamically allocated array of objects of type T , resizing it when a new item is added. Using a callback function, this class will inform the client when a new item has been added to the collection The class collection will provide two overloads for operator] to access the stored item. Private Data the name of the collection; a dynamically allocated array of items T the size of the array . a pointer to a function that returns void and receives two parameters of type const Collection& and const T&. This is the observer function (it observes an event): when an item has been added to the collection, the class Collection will call this function informing the client about the adition Public Members Collection(std::string name) : sets the name of the collection to the parameter and all other attributes to their default value this class doesn't support copy operations; delete them. a destructor const std::string& name() const : a query that returns the name of the collection. size_t size) const : a query that returns how many items are in the collection. void setObserver(void *observer) (const Collection&, const T&)) : stores the parameter into an attribute, to be used when an item is added to the collection. The parameter is a pointer to a function that returns void and accepts two parameters: a collection and an item that has just been added to the collection. Collection& operator+(const & item) : adds a copy of item to the collection, only if the collection doesn't contain an item with the same title (type T has a member function called title() that returns the title of the item). If item is already in the collection, this function does nothing. If the item is not already in the collection, this function: o resize the array to accomodate the new item o if there is an observer registered, call the observer function passing *this and the new item as parameters. T& operator] (size_t idx) const : returns the item at index idx. o if the index is out of range, this function throws an exception of type std::out_of_range with the message Bad index (IDX). Collection has (SIZE) items. . Use operator + to concatenate strings. When implementing this operator, consider the following: std::to_string() o std::out_of_range T* operator] (std::string title) const returns the address of the item with title title (type T has a member function called title() that returns the title of the item). If no such item exists, this function returns nullptr. #include #include #include #include "Collection.h" #include "Collection.h" #include "Book.h" #include "Book.h" #include "Movie.h" #include "Movie.h" #include "Spellchecker.h" #include "Spellchecker.h" using namespace sdds; // The observer function for adding books to the collection: 11 should be called every time a new book is added to the collection void bookAddedObserver(const Collection& theCollection, const Book& theBook) std::cout & theCollection, const Movie& theMovie) std::cout library("Bestsellers"); // TODO: load the first 4 books from the file "argv[1]". - read one line at a time, and pass it to the Book constructor - store each book read into the collection "library" (use the + operator) // - lines that start with "#" are considered comments and should be ignored library.setObserver(bookAddedObserver); // TODO: add the rest of the books from the file. double usdToCadRate = 1.3; double gbpToCadRate - 1.5; // TODO: (from in-lab) create a lambda expression that fixes the price of a book accoding to the rules - the expression should receive a single parameter of type "Book&" - if the book was published in US, multiply the price with "usdToCadRate" and save the new price in the book object - if the book was published in UK between 1990 and 1999 (inclussive), multiply the price with "gbpToCadRate" and save the new price in the book object std::cout theCollection("Action Movies"); // Process the file Movie movies[5]; // TODO: load 5 movies from the file "argv[2]". - read one line at a time, and pass it to the Movie constructor - store each movie read into the array "movies" // - lines that start with "#" are considered comments and should be ignored std::cout where ERROR_MESSAGE is extracted from the exception object. for (auto i - Bu; i // where ERROR_MESSAGE is extracted from the exception object. Spellchecker sp(argv[i]); for (auto j = Ou; j & and const T&. This is the observer function (it observes an event): when an item has been added to the collection, the class Collection will call this function informing the client about the adition Public Members Collection(std::string name) : sets the name of the collection to the parameter and all other attributes to their default value this class doesn't support copy operations; delete them. a destructor const std::string& name() const : a query that returns the name of the collection. size_t size) const : a query that returns how many items are in the collection. void setObserver(void *observer) (const Collection&, const T&)) : stores the parameter into an attribute, to be used when an item is added to the collection. The parameter is a pointer to a function that returns void and accepts two parameters: a collection and an item that has just been added to the collection. Collection& operator+(const & item) : adds a copy of item to the collection, only if the collection doesn't contain an item with the same title (type T has a member function called title() that returns the title of the item). If item is already in the collection, this function does nothing. If the item is not already in the collection, this function: o resize the array to accomodate the new item o if there is an observer registered, call the observer function passing *this and the new item as parameters. T& operator] (size_t idx) const : returns the item at index idx. o if the index is out of range, this function throws an exception of type std::out_of_range with the message Bad index (IDX). Collection has (SIZE) items. . Use operator + to concatenate strings. When implementing this operator, consider the following: std::to_string() o std::out_of_range T* operator] (std::string title) const returns the address of the item with title title (type T has a member function called title() that returns the title of the item). If no such item exists, this function returns nullptr. #include #include #include #include "Collection.h" #include "Collection.h" #include "Book.h" #include "Book.h" #include "Movie.h" #include "Movie.h" #include "Spellchecker.h" #include "Spellchecker.h" using namespace sdds; // The observer function for adding books to the collection: 11 should be called every time a new book is added to the collection void bookAddedObserver(const Collection& theCollection, const Book& theBook) std::cout & theCollection, const Movie& theMovie) std::cout library("Bestsellers"); // TODO: load the first 4 books from the file "argv[1]". - read one line at a time, and pass it to the Book constructor - store each book read into the collection "library" (use the + operator) // - lines that start with "#" are considered comments and should be ignored library.setObserver(bookAddedObserver); // TODO: add the rest of the books from the file. double usdToCadRate = 1.3; double gbpToCadRate - 1.5; // TODO: (from in-lab) create a lambda expression that fixes the price of a book accoding to the rules - the expression should receive a single parameter of type "Book&" - if the book was published in US, multiply the price with "usdToCadRate" and save the new price in the book object - if the book was published in UK between 1990 and 1999 (inclussive), multiply the price with "gbpToCadRate" and save the new price in the book object std::cout theCollection("Action Movies"); // Process the file Movie movies[5]; // TODO: load 5 movies from the file "argv[2]". - read one line at a time, and pass it to the Movie constructor - store each movie read into the array "movies" // - lines that start with "#" are considered comments and should be ignored std::cout where ERROR_MESSAGE is extracted from the exception object. for (auto i - Bu; i // where ERROR_MESSAGE is extracted from the exception object. Spellchecker sp(argv[i]); for (auto j = Ou; j

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

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions