Question: # Workshop 5: Functions and Error Handling In this workshop, you code a function object and a lambda expression, and handle exceptions. You are to
# Workshop 5: Functions and Error Handling
In this workshop, you code a function object and a lambda expression, and handle exceptions.
You are to create a class template that manages a family of collection of objects of type `T`.
This template's client can register a callback function (an observer) that will be called every
time a new item is added successfully to a collection.
Your design works with collections of books and collections of movies, both loaded from files.
The file information contains mistakes:
- You are to create a lambda expression that fixes the price information about a book
- You are to create a function object (functor) that fixes some spelling mistakes in the descriptions and titles for books or movies.
In case of exceptional circumstances, your design generates exceptions and subsequently handles them
- the functor loads the misspelled words from a file, but if that file is missing, generates an exception.
- iterating over the collection using indices, your design generates an exception if an index is invalid.
## Part 1 (0%)
The first part of this workshop consists of two modules:
- `w5` (partially supplied)
- `Book`
Enclose all your source code within the `sdds` namespace and include the necessary guards in each header file.
### `Book` Module
This module holds information about a single book.
Design and code a class named `Book` that can store the following information (for each attribute, chose any type that you think is appropriate--you must be able to justify the decisions you have made):
- **author**
- **title**
- **the country of publication**
- **the year of publication**
- **the price of the book**
- **the description**: a short summary of the book
***Public Members***
- a default constructor
- `const std::string& title() const`: a query that returns the title of the book
- `const std::string& country() const`: a query that returns the publication country
- `const size_t& year() const`: a query that returns the publication year
- `double& price()`: a function that returns the price **by reference**, allowing the client code to update the price
- `Book(const std::string& strBook)`: a constructor that receives a reference to an unmodifiable string that contains information about the book; this constructor extracts the information about the book from the string by parsing it and stores the tokens in the object's attributes. The string always has the following format:
```
AUTHOR,TITLE,COUNTRY,PRICE,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()](https://en.cppreference.com/w/cpp/string/basic_string/substr)
- [std::string::find()](https://en.cppreference.com/w/cpp/string/basic_string/find)
- [std::string::erase()](https://en.cppreference.com/w/cpp/string/basic_string/erase)
- [std::stoi()](https://en.cppreference.com/w/cpp/string/basic_string/stol)
- [std::stod()](https://en.cppreference.com/w/cpp/string/basic_string/stof)
**Add any other private member that is required by your design!**
***Friend Helpers***
- overload the insertion operator to insert the content of a book object into an **ostream** object, in the following format:
```
AUTHOR | TITLE | COUNTRY | YEAR | PRICE | DESCRIPTION
```
- the **author** printed on a field of size 20;
- the **title** printed on a field of size 22;
- the **country** printed on a field of size 5;
- the **year** printed on a field of size 4;
- the **price** printed on a field of size 6, with 2 decimal digits;
- see alignment in the sample output.
### `w5` Module (partially supplied)
This module has some missing statements. The missing parts are marked with `TODO`, which describes the code that you should add and where to add it. **Do not modify the existing code, only add the code that is missing!**
### Sample Output
When the program is started with the command (the file `book.txt` is provided):
```bash
ws book.txt
```
the output should look like that is the `sample_output.txt` file.
SAMPLE_OUTPUT.TXT For PART1
Command Line: -------------------------- 1: ws 2: books.txt --------------------------
----------------------------------------- The library content ----------------------------------------- Miguel de Cervantes | Don Quixote | Spain | 1612 | 9.99 | The story follows a Hidalgo, Alonso Quixano, who proves that chivalry will in fact never die. Charles Dickens | A Tale of Two Cities | UK | 1859 | 12.32 | A historical novel, set in London and Paris at teh begiming and during the French Revolution. George R.R. Martin | A Song of Ice and Fire | US | 1997 | 99.90 | Nine noble famillies begim fighting for control over teh mythical lands of Westeros. J.R.R. Tolkien | The Lord of the Rings | UK | 1993 | 21.11 | Sauron has gathered to him all teh Rings of Power, and he intends to use them to rule Middle-earth. J.K. Rowling | Harry Potter | UK | 1997 | 45.99 | Harry realizes his life is far from ordinary. Dan Brown | The Da Vinci Code | US | 2003 | 7.88 | While in Paris, Harvard symbologist Robert Langdon is awakened by a phone call in teh night. J.D. Salinger | The Catcher in teh Rye | US | 1951 | 12.21 | The story of a teen named Holden Caulfield and his struggle to find his voice in an adult world. -----------------------------------------
----------------------------------------- The library content (updated prices) ----------------------------------------- Miguel de Cervantes | Don Quixote | Spain | 1612 | 9.99 | The story follows a Hidalgo, Alonso Quixano, who proves that chivalry will in fact never die. Charles Dickens | A Tale of Two Cities | UK | 1859 | 12.32 | A historical novel, set in London and Paris at teh begiming and during the French Revolution. George R.R. Martin | A Song of Ice and Fire | US | 1997 | 129.87 | Nine noble famillies begim fighting for control over teh mythical lands of Westeros. J.R.R. Tolkien | The Lord of the Rings | UK | 1993 | 31.66 | Sauron has gathered to him all teh Rings of Power, and he intends to use them to rule Middle-earth. J.K. Rowling | Harry Potter | UK | 1997 | 68.98 | Harry realizes his life is far from ordinary. Dan Brown | The Da Vinci Code | US | 2003 | 10.24 | While in Paris, Harvard symbologist Robert Langdon is awakened by a phone call in teh night. J.D. Salinger | The Catcher in teh Rye | US | 1951 | 15.87 | The story of a teen named Holden Caulfield and his struggle to find his voice in an adult world. -----------------------------------------
***This part represents a milestone in completing the workshop and is not marked!***
## Part 2 (100%)
The second part of this workshop upgrades your solution to incorporate three additional modules:
- `Movie`
- `SpellChecker`
- `Collection`
### `SpellChecker` Module (functor)
Add a `SpellChecker` module to your project. This module holds two parallel arrays of strings, both of size 6 (statically allocated):
- `m_badWords`: an array with 6 misspelled words
- `m_goodWords`: an array with the correct spelling of those 6 words
- any other member required by your design to accomplish the goals described below.
***Public Members***
- `SpellChecker(const char* filename)`: receives the address of a C-style null-terminated string that holds the name of the file that contains the misspelled words. If the file exists, this constructor loads its contents. If the file is missing, this constructor throws an exception of type `const char*`, with the message `Bad file name!`. Each line in the file has the format `BAD_WORD GOOD_WORD`; the two fields can be separated by any number of spaces.
- `void operator()(std::string& text)`: this operator searches `text` and replaces any misspelled word with the correct version. It should also count how many times **each** misspelled word has been replaced.
When implementing this operator, consider the following functions:
- [std::string::find()](https://en.cppreference.com/w/cpp/string/basic_string/find)
- [std::string::replace()](https://en.cppreference.com/w/cpp/string/basic_string/replace)
- `void showStatistics(std::ostream& out) const`: inserts into the parameter how many times each misspelled word has been replaced by the correct word using the current instance. The format of the output is:
```
BAD_WORD: CNT replacements
```
where `BAD_WORD` is to be printed on a field of size 15, aligned to the right.
**You will have to design a method to remember how many times each bad word has been replaced.**
### `Book` Module
Add a public templated function to your `Book` class:
- `void fixSpelling(T& spellChecker)`: this function calls the overloaded `operator()` on the instance `spellChecker`, passing to it the book description.
***ASSUMPTION***: In this design, type `T` must have an overload of the `operator()` that accepts a string as a parameter.
**Since this is a templated function, it must be implemented in the header file!** The class itself is not templated; only the function is templated.
### `Movie` Module
Design and code a class named `Movie` that stores the following information for a single movie (for each attribute, chose any type that you think is 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)`: receives the movie through a reference to a string. This constructor extracts the information about the movie from the string and stores the tokens in the attributes. The received string always has the following format:
```
TITLE,YEAR,DESCRIPTION
```
This constructor removes all spaces from the **beginning and end** of any token in the string.
When implementing this constructor, consider the following functions:
- [std::string::substr()](https://en.cppreference.com/w/cpp/string/basic_string/substr)
- [std::string::find()](https://en.cppreference.com/w/cpp/string/basic_string/find)
- [std::string::erase()](https://en.cppreference.com/w/cpp/string/basic_string/erase)
- [std::stoi()](https://en.cppreference.com/w/cpp/string/basic_string/stol)
- `void fixSpelling(T& spellChecker)`: a templated function. This function calls the overloaded `operator()` on instance `spellChecker`, passing to it the movie title and description.
***ASSUMPTION***: In this design, type `T` must have an overload of the `operator()` that accepts a string as a parameter.
**Since this is a templated function, it must be implemented in the header!** The class is not a templated class.
**Add any other private member 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** printed on a field of size 40;
- the **year** printed on a field of size 4;
### `Collection` Module
Add a `Collection` module to your project. The purpose of this module is to manage a collection items of template type `T`. Since this is templated class, it doesn't need a `.cpp` file.
The `Collection` class manages a **dynamically allocated** array of objects of type `T`, resizing it when a new item is added. When a new item is added to the collection, this class informs the client using a *callback function*.
This class provides two overloads of the subscripting operator (`operator[]`) to access a 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
This is the **observer** function (it *observes* an event): when an item has been added to the collection, the class `Collection
***Public Members***
- `Collection(const std::string& name)`: sets the name of the collection to the string referred to by the parameter and sets all other attributes to their default value
- this class doesn't support any copy operations; delete all of 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 the number of items in the collection.
- `void setObserver(void (*observer)(const Collection
that returns `void` and accepts two parameters: a collection and an item that has just been added to the collection. This function is called when an item is added to the collection.
- `Collection
- resizes the array of items to accommodate the new item
- if an observer has been registered, this operator calls the observer function passing the current object (`*this`) and the new item as arguments.
- **ASSUMPTION**: type `T` has a member function called `title()` that returns the title of the item (`std::string`).
- `T& operator[](size_t idx) const`: returns the item at index `idx`.
- if the index is out of range, this operator 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()](https://en.cppreference.com/w/cpp/string/basic_string/to_string)
- [std::out_of_range](https://en.cppreference.com/w/cpp/error/out_of_range)
- `T* operator[](const std::string& title) const`: returns the address of the item with the 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`.
***FREE Helpers***
- overload the insertion operator to insert the content of a `Collection` object into an **ostream** object. Iterate over all elements in the collection and insert each one into the `ostream` object (do not add newlines).
**:warning:Important: The class `Collection` should have no knowledge of any of the custom types you have defined (`Book`, `Movie`, `SpellChecker`).**
### Sample Output
When the program is started with the command (the files are provided):
```bash
w5.exe books.txt movies.txt missing_file.txt words.txt
```
the output should look like the one from the `sample_output.txt` file.
SAMPLE_OUTPUT.TXT For PART2
Command Line: -------------------------- 1: ws 2: books.txt 3: movies.txt 4: missing.txt 5: words.txt --------------------------
Book "Harry Potter" added! Book "The Da Vinci Code" added! Book "The Catcher in teh Rye" added! ----------------------------------------- The library content ----------------------------------------- Miguel de Cervantes | Don Quixote | Spain | 1612 | 9.99 | The story follows a Hidalgo, Alonso Quixano, who proves that chivalry will in fact never die. Charles Dickens | A Tale of Two Cities | UK | 1859 | 12.32 | A historical novel, set in London and Paris at teh begiming and during the French Revolution. George R.R. Martin | A Song of Ice and Fire | US | 1997 | 99.90 | Nine noble famillies begim fighting for control over teh mythical lands of Westeros. J.R.R. Tolkien | The Lord of the Rings | UK | 1993 | 21.11 | Sauron has gathered to him all teh Rings of Power, and he intends to use them to rule Middle-earth. J.K. Rowling | Harry Potter | UK | 1997 | 45.99 | Harry realizes his life is far from ordinary. Dan Brown | The Da Vinci Code | US | 2003 | 7.88 | While in Paris, Harvard symbologist Robert Langdon is awakened by a phone call in teh night. J.D. Salinger | The Catcher in teh Rye | US | 1951 | 12.21 | The story of a teen named Holden Caulfield and his struggle to find his voice in an adult world. -----------------------------------------
----------------------------------------- The library content (updated prices) ----------------------------------------- Miguel de Cervantes | Don Quixote | Spain | 1612 | 9.99 | The story follows a Hidalgo, Alonso Quixano, who proves that chivalry will in fact never die. Charles Dickens | A Tale of Two Cities | UK | 1859 | 12.32 | A historical novel, set in London and Paris at teh begiming and during the French Revolution. George R.R. Martin | A Song of Ice and Fire | US | 1997 | 129.87 | Nine noble famillies begim fighting for control over teh mythical lands of Westeros. J.R.R. Tolkien | The Lord of the Rings | UK | 1993 | 31.66 | Sauron has gathered to him all teh Rings of Power, and he intends to use them to rule Middle-earth. J.K. Rowling | Harry Potter | UK | 1997 | 68.98 | Harry realizes his life is far from ordinary. Dan Brown | The Da Vinci Code | US | 2003 | 10.24 | While in Paris, Harvard symbologist Robert Langdon is awakened by a phone call in teh night. J.D. Salinger | The Catcher in teh Rye | US | 1951 | 15.87 | The story of a teen named Holden Caulfield and his struggle to find his voice in an adult world. -----------------------------------------
----------------------------------------- Testing addition and callback function ----------------------------------------- Movie "Star Wars: The Rise of Skywalker" added to collection "Action Movies" (4 items). Movie "Terminator: Dork Fate" added to collection "Action Movies" (5 items). -----------------------------------------
----------------------------------------- Testing exceptions and operator[] ----------------------------------------- Game of Thrones | 2011 | Nine noble famillies begim fighting for control over teh mythical lands of Westeros. Avengers: Endgame | 2019 | With the help of remaining allies, teh Avengers assemble once more in order to reverse Thanos' actions. Dork Phoenix | 2019 | Jean Grey begims to develop incredible Dork powers that corrupt and turn her into a Dork Phoenix. Star Wars: The Rise of Skywalker | 2019 | The surviving Resistance faces the First Order once more in the final chapter of the Skywalker saga. Terminator: Dork Fate | 2019 | Sarah Connor and a hibrid cyborg human must protect a young girl from a newly modiffied liquid Terminator. ** EXCEPTION: Bad index [5]. Collection has [5] items. -----------------------------------------
----------------------------------------- Testing the functor ----------------------------------------- ** EXCEPTION: Bad file name! Spellchecker Statistics teh: 4 replacements modiffied: 0 replacements begim: 2 replacements famillies: 1 replacements Dork: 0 replacements hibrid: 0 replacements Spellchecker Statistics teh: 6 replacements modiffied: 1 replacements begim: 4 replacements famillies: 2 replacements Dork: 4 replacements hibrid: 1 replacements
========================================= Wrapping up this workshop --------------- Movies ------------------ Game of Thrones | 2011 | Nine noble families begin fighting for control over the mythical lands of Westeros. Avengers: Endgame | 2019 | With the help of remaining allies, the Avengers assemble once more in order to reverse Thanos' actions. Dark Phoenix | 2019 | Jean Grey begins to develop incredible Dark powers that corrupt and turn her into a Dark Phoenix. Star Wars: The Rise of Skywalker | 2019 | The surviving Resistance faces the First Order once more in the final chapter of the Skywalker saga. Terminator: Dark Fate | 2019 | Sarah Connor and a hybrid cyborg human must protect a young girl from a newly modified liquid Terminator. --------------- Books ------------------- Miguel de Cervantes | Don Quixote | Spain | 1612 | 9.99 | The story follows a Hidalgo, Alonso Quixano, who proves that chivalry will in fact never die. Charles Dickens | A Tale of Two Cities | UK | 1859 | 12.32 | A historical novel, set in London and Paris at the begining and during the French Revolution. George R.R. Martin | A Song of Ice and Fire | US | 1997 | 129.87 | Nine noble families begin fighting for control over the mythical lands of Westeros. J.R.R. Tolkien | The Lord of the Rings | UK | 1993 | 31.66 | Sauron has gathered to him all the Rings of Power, and he intends to use them to rule Middle-earth. J.K. Rowling | Harry Potter | UK | 1997 | 68.98 | Harry realizes his life is far from ordinary. Dan Brown | The Da Vinci Code | US | 2003 | 10.24 | While in Paris, Harvard symbologist Robert Langdon is awakened by a phone call in the night. J.D. Salinger | The Catcher in teh Rye | US | 1951 | 15.87 | The story of a teen named Holden Caulfield and his struggle to find his voice in an adult world. ----------------------------------------- Testing operator[] (the other overload) ----------------------------------------- ** Movie Terminator 2 not in collection. In this collection: Dark Phoenix | 2019 | Jean Grey begins to develop incredible Dark powers that corrupt and turn her into a Dark Phoenix. -----------------------------------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
