Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include // TODO Follow the instructions in each comment, adding code below the comment int main() { std::cout item.cpp #include Item.h //Define Item methods here

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

#include // TODO Follow the instructions in each comment, adding code below the comment int main() { std::cout

item.cpp

#include "Item.h" //Define Item methods here

Receipt.cpp

#include "Receipt.h" #include /* * @brief Add ReceiptItems to a vector in Receipt to store and print * @remark Checks to see if ReceiptItem is already in the list. * If true, rather than adding ReceiptItem to vector, * we will add ReceiptItems together using ReceiptItem overloaded addition operator * @param ReceiptItem user chosen ReceiptItem to add to list */ void Receipt::AddReceiptItem(ReceiptItem receiptItem) { int position = Find(receiptItem); if(position != -1) receiptItems_.at(position) = receiptItems_.at(position) + receiptItem; else receiptItems_.push_back(receiptItem); } /* * @brief prints each ReceiptItem in the list including * Item name * Item Quantity * Item Price * Item Total * then prints a separating line and a final total */ void Receipt::PrintReceipt() const { double total = 0; // print labels std::cout

Recipt.h

#ifndef RECEIPT_H #define RECEIPT_H #include #include #include "ReceiptItem.h" class Receipt { private: std::vector receiptItems_; int Find(const ReceiptItem) const; public: void AddReceiptItem(ReceiptItem receiptItem); void PrintReceipt() const; //for testing not for use in your code int GetReceiptItemCount(); int GetItemCount(); double GetFinalPrice(); }; #endif //RECEIPT_H

ReceiptItem.h

#ifndef RECEIPT_ITEM_H #define RECEIPT_ITEM_H #include "Item.h" //Define the Item class here #endif //RECEIPT_ITEM_H

Receipt_test.cpp

#include "Item.h" #include "ReceiptItem.h" //#include "Receipt.h" #include #include // this tells catch to provide a main() // only do this in one cpp file #define CATCH_CONFIG_MAIN #include "catch.hpp" //for floating point numbers using namespace Catch::Matchers::Floating; TEST_CASE("Item Class Constructors & Getters") { INFO("Checks that default constructor creates objects with correct default values") Item item1; CHECK(item1.GetId() == 0); CHECK(item1.GetName() == "no name"); CHECK(item1.GetDescription() == ""); CHECK(item1.GetPrice() == 0); INFO("Checks that general constructor creates objects with correct values") Item item2(5, "test product", "test description that is longer than the name of the product", 15.99); CHECK(item2.GetId() == 5); CHECK(item2.GetName() == "test product"); CHECK(item2.GetDescription() == "test description that is longer than the name of the product"); CHECK(item2.GetPrice() == 15.99); } TEST_CASE("Item Class Setters & Getters") { // Checks that the setters are setting correct values Item testItem; // Checking id_ getter & setter testItem.SetId(1); CHECK(testItem.GetId() == 1); testItem.SetId(-1); CHECK(testItem.GetId() == 0); // Checking name_ getter & setter CHECK(testItem.GetName() == "no name"); testItem.SetName("New Name"); CHECK(testItem.GetName() == "New Name"); // Checking description_ getter & setter CHECK(testItem.GetDescription() == ""); testItem.SetDescription("New description"); CHECK(testItem.GetDescription() == "New description"); // Checking price_ getter & setter CHECK(testItem.GetPrice() == 0); testItem.SetPrice(5.50); CHECK(testItem.GetPrice() == 5.50); testItem.SetPrice(-5.50); CHECK(testItem.GetPrice() == 0); } TEST_CASE("Item's Overloaded Operators") { // Checks to make sure the overloaded operators are working correctly Item item1(5, "test product", "test description that is longer than the name of the product", 15.99); Item item2(5, "test product", "test description that is longer than the name of the product", 15.99); CHECK(item1 == item2); // Checks how the name_ parameter impacts the overloaded equality operator item1.SetName("Test Product"); CHECK_FALSE(item1 == item2); item1.SetName("test product"); CHECK(item1 == item2); // Checks how the id_ parameter impacts the overloaded equality operator item1.SetId(3); CHECK_FALSE(item1 == item2); item1.SetId(5); CHECK(item1 == item2); // Checks how the description_ parameter impacts the overloaded equality operator item1.SetDescription("testing a different description"); CHECK_FALSE(item1 == item2); item1.SetDescription("test description that is longer than the name of the product"); CHECK(item1 == item2); // Checks how the price_ parameter impacts the overloaded equality operator item1.SetPrice(16.00); CHECK_FALSE(item1 == item2); item1.SetPrice(15.99); CHECK(item1 == item2); } TEST_CASE("ReceiptItem Class Constructors") { // Checks that default constructor creates objects with correct default values ReceiptItem receiptItem1; Item item1; CHECK(receiptItem1.GetItem() == item1); CHECK(receiptItem1.GetQuantity() == 0); CHECK(receiptItem1.GetTotal() == 0); // Checks that general constructor creates objects with correct given values Item item2(5, "test product", "test description that is longer than the name of the product", 15.99); ReceiptItem receiptItem2(item2, 4); CHECK(receiptItem2.GetItem() == item2); CHECK(receiptItem2.GetQuantity() == 4); CHECK(receiptItem2.GetTotal() == 4 * 15.99); } TEST_CASE("ReceiptItem Class Setters & Getters") { ReceiptItem receiptItem1; Item item1; Item item2(5, "test product", "test description that is longer than the name of the product", 15.99); // Uses ReceiptItem getters to check that the ReceiptItem object has default values CHECK(receiptItem1.GetItem() == item1); CHECK(receiptItem1.GetQuantity() == 0); CHECK(receiptItem1.GetTotal() == 0); // Uses ReceiptItem getters to check that the setters successfully set new values receiptItem1.SetItem(item2); CHECK(receiptItem1.GetItem() == item2); receiptItem1.SetQuantity(15); CHECK(receiptItem1.GetQuantity() == 15); CHECK(receiptItem1.GetTotal() == 15 * 15.99); } TEST_CASE("ReceiptItem operator+ that adds two ReceiptItems together") { // Creating several Item and ReceiptItem objects Item item1(5, "test product", "test description that is longer than the name of the product", 15.99); Item item2(5, "test product", "test description that is longer than the name of the product", 15.99); Item item3(6, "test product 2", "test description that is longer than the name of the product", 10.12); ReceiptItem receiptItem1(item1, 1); ReceiptItem receiptItem2(item2, 3); ReceiptItem receiptItem3(item3, 2); // Checks that operator+ correctly adds together two ReceiptItems w/ the same Item. // Should not care what quantity is when adding two LogItems together. // Same quantities ReceiptItem receiptItemResult = receiptItem1 + receiptItem1; CHECK(receiptItemResult.GetItem() == item1); CHECK(receiptItemResult.GetQuantity() == 2); // Different quantities receiptItemResult = receiptItem1 + receiptItem2; CHECK(receiptItemResult.GetItem() == item1); CHECK(receiptItemResult.GetQuantity() == 4); // Checks that operator+ returns correct results when the item in the ReceiptItem is different CHECK_THROWS(receiptItem1 + receiptItem3); } TEST_CASE("ReceiptItem operator+ that adds an integer") { // Creating Item and ReceiptItem objects Item item1(5, "test product", "test description that is longer than the name of the product", 15.99); ReceiptItem receiptItem1(item1, 1); // Checks that operator+ returns correct results when adding an integer value to the right-hand side of an ReceiptItem object ReceiptItem receiptItemResult = receiptItem1 + 5; CHECK(receiptItemResult.GetItem() == item1); CHECK(receiptItemResult.GetQuantity() == 6); // Checks that the friend operator+ returns correct results when adding an integer value to the left-hand side of an ReceiptItem object receiptItemResult = 5 + receiptItem1; CHECK(receiptItemResult.GetItem() == item1); CHECK(receiptItemResult.GetQuantity() == 6); } //TEST_CASE("Receipt Class Tests") //{ // // Create Item objects // Item item1(5, "test product", "test description that is longer than the name of the product", 15.99); // Item item2(5, "test product", "test description that is longer than the name of the product", 15.99); // Item item3(6, "test product 2", "test description that is longer than the name of the product", 10.12); // // // Create ReceiptItem objects using above Item objects // ReceiptItem ReceiptItem1(item1, 1); // ReceiptItem ReceiptItem2(item2, 3); // ReceiptItem ReceiptItem3(item3, 2); // // // Creat Receipt object // Receipt testReceipt; // CHECK(testReceipt.GetFinalPrice() == 0); // // testReceipt.AddReceiptItem(ReceiptItem1); // CHECK(testReceipt.GetFinalPrice() == 15.99); // CHECK(testReceipt.GetReceiptItemCount() == 1); // CHECK(testReceipt.GetItemCount() == 1); // // testReceipt.AddReceiptItem(ReceiptItem2); // CHECK(testReceipt.GetFinalPrice() == 15.99 * 4); // CHECK(testReceipt.GetReceiptItemCount() == 1); // CHECK(testReceipt.GetItemCount() == 4); // // testReceipt.AddReceiptItem(ReceiptItem3); // CHECK(testReceipt.GetFinalPrice() == 15.99 * 4 + 10.12 * 2); // CHECK(testReceipt.GetReceiptItemCount() == 2); // CHECK(testReceipt.GetItemCount() == 6); //} M README.md C++main.cpp c++Receipt.cpp [H Receipt.h H] Receiptltem.h C++Item.cpp H Item.h Part I - Writing Classes The Item Class Files for modification - Item,h - Item. cpp Member Variables The Item class will have four private member variables: - int id_ - an integer value that uniquely identifies the object - string name_ - the name of the item - string description_ - a brief description of the item - double price_ - the price of the item Member Functions The Item class will need several functions, include constructors, getters, setters, and some overloaded operators. Constructors This class will need two constructors. - Default - Written inline using an initializer list - No parameters - sets id_to 0 - sets price_value to zero - sets name_ value to "no name" - sets description_ value to empty string - General - Parameters should include (in this order): int id, string name, string description, double price Getters Create getter functions that return the member variable value when called. These can be defined inline or in the source file (Item.cpp). /ersion control MREADME.mdC++main.cpp C++Receipt.cpp Receipt.h Debug main ir D = c+0 Part III - Generating a Receipt In this section we will use our newly defined classes to output our Receiptltems in a formatted receipt with a total. Receipt Class you will use the Receipt class. It has been written by someone else to work with your completed ltem and Receiptltem classes. storing as Receiptltems into a vector. You will then be able to print the list of Receiptltems along with the final How to use the Receipt Class 1. \#include the Receipt class file header in main 2. Create a Receipt object. You will only need one instance. 4. Use the AddReceiptltem() method several times, adding many of your Receiptltems to the vector to print. 5. After this is finished, call the PrintReceipt() method. Your output should look similar to the following example: Submission Run the unit tests to make sure they are all passing. Take a screenshot of the passing tests. Verify that main has the correct output. Take a screenshot of the PrintReceipt() output. Zip up the folder called src. Should include the following files: Item.cpp, Item.h, main.cpp, Receipt.h, Receipt.cpp, Receiptltem.cpp, and Receiptltem.h Submit your work in the Challenge Activity assignment page for Module 3. M README.md C++main.cpp C ++Receipt.cpp Receipt.h Item.h Ihese can be detined inline or in the source tile (Item.cpp). - Names - Should use the word 'Get' then the name of the attribute. For example, for id_we should see GetId . Make sure to use UpperCammelCase. - Parameters - No parameters - Read-Only - Make these functions const since we will not be modifying the member variables within any of the function definitions. - Definition - These should simply return the member variable values. Setters Create setters that allow the class to control what the user can set the variables to. These must be defined in the source file (Item.cpp) - Return Types: These should not return any values so make them have a return type of void - Names - Should use the word 'Set' then the name of the attribute. For example, for id_we should see SetId . Make sure to use UpperCammelCase. Overloaded Operators The Item class will have 2 overloaded operators - Overloaded equality operator: == - Member function - This function should compare the member variables for two Item objects. Returns true if they are all equal and false if any are not. - Overloaded inserter operator: - Friend function - Prints the member variables of an Item object. Formatting is not important. The Receiptltem Class This class will manage the quantity of each item Files for modification - Receiptltem.h - Receiptltem.cpp The Receiptltem class will have 2 private member variables: - item_- Instance of the Item class - quantity_ type integer, keeps track the item count on our receipt m README.md CC + main.cpp C ++Receipt.cpp Receipt.h Receiptltem.h Item.h Adds together two Receiptltems. member variable, use a try/catch block. If they are not the same then throw an error. Returns an updated Receiptltem object - Overloaded addition operator for integers: + - Allows the user to add an integer to a Receiptltem object as the right-hand operand. For example: myReceiptItem +5 Returns an updated Receiptltem object - Overloaded subtraction operator for integers: - Allows the user to subtract an integer from a Receiptltem object as the right-hand operand. For example: myReceiptItem - 5 - Returns an updated Receiptltem object - Overloaded addition operator for integers: + Allows the user to add an integer to an Receiptltem object as the left-hand operand. For example: 5+ myReceiptItem should return an updated Receiptltem object - Overloaded inserter operator: - Prints specific member variables of the Receiptltem object and stored Item object. Item name_ - Left justified with a width of 50 chars Receiptltem quantity_ - Left justified with a width of 9 chars - Should look like this: (2) - Hint: Make the (quantity_) into a string using the std::to_string() function before trying to format with iomanip to make it work with () Print $ - Item price_ - Right justified with a width of 10 Returns an ostream object Hint: Remember to \#include the library, especially if you are on a Mac. Macs don't typically show a compiler error when it is missing, but Windows do. In the first section of main.cpp there are several TODO comments for creating and using Receiptltem objects. Follow these directions to use and test your Receiptltem class. Part II - Trying everything in main.cpp Now that each of your classes have been created. If you haven't yet already, switch over to main and follow the directions listed throughout the page in /ITODO comments and print statements up until Part 3 . Part III - Generating a Receipt In this section we will use our newly defined classes to output our Receiptltems in a formatted receipt with a total. M README.md main.cpp Receipt.cpp Receipt.h Receiptltem.h Item.cpp Item.h Constructors This class will need two constructors. - Default - Written inline using an initializer list - No parameters - calls Item default constructor - sets numerical values to zero - General - Parameters should include (in this order): Item food, int quantity - Sets the member variables to user chosen values. Should not allow user to set member variable values to a negative number. If the user tries this, give the variable a default value of 0 . Getters Create getter functions that return the member variable value when called. These should follow the same guidelines as the Item getters. These can be defined inline ( ReceiptItem. h ) or in the source file (ReceiptItem. cpp ). - Return Types - These should each be set to have the return type that is the same as the attribute being returned. - Names - Should use the word 'Get' then the name of the attribute. Make sure to use UpperCamelCase. - Parameters - No parameters - Read-Only - Make these functions are const since we will not be modifying the member variables within any of the function definitions. - Definition - These should simply return the member variable values. Getter for a Calculated Value - GetTotal() - Gets the total price for the quantity of item's - No parameters Setters These must be defined in the source file (Receiptltem.cpp). These should follow the same guidelines as the ltem setters. - Return Types: These should not return any values so make them type void - Names - Should use the word 'Set' then the name of the attribute. Make sure to use UpperCamelCase. - Parameters - Each setter should accept one parameter. It should be the same type as the related attribute. Overloaded Operators The Receiptltem class will have 6 overloaded operators - Overloaded addition operator: + - Adds toaether two Receintltems. are complete you will use them in main to create objects for a business of your choice. Objectives Demonstrate knowledge about the following topics: - Writing classes - Using objects - Passing \& Returning objects in functions - Overloading operators for user defined data types - Simple try/catch blocks Files to work on The following files are included in the src folder for you to either work on or use. - src/Item.h - src/ Item.cpp - src/Receiptitem.h - src/ReceiptItem.cpp - src/Receipt.h do not modify - src/Receipt.cpp do not modify - Your driver file will be src/main.cpp Unit Tests Do NOT modify the unit tests! Your code will be tested against a separate copy than what you have. - tests/receipt_test.cpp The Problem to Solve You and a team of software engineers have been tasked to create a receipt system. You have been assigned by your team to create two out of three classes that will be used: Item and Receiptitem The Item class will store information about any product that the user is purchasing. The purpose of the Receiptltem class will be to manage the quantity of each Item. README.md 1:1 CRLF UTF-8 2 spaces* M README.md C++main.cpp c++Receipt.cpp [H Receipt.h H] Receiptltem.h C++Item.cpp H Item.h Part I - Writing Classes The Item Class Files for modification - Item,h - Item. cpp Member Variables The Item class will have four private member variables: - int id_ - an integer value that uniquely identifies the object - string name_ - the name of the item - string description_ - a brief description of the item - double price_ - the price of the item Member Functions The Item class will need several functions, include constructors, getters, setters, and some overloaded operators. Constructors This class will need two constructors. - Default - Written inline using an initializer list - No parameters - sets id_to 0 - sets price_value to zero - sets name_ value to "no name" - sets description_ value to empty string - General - Parameters should include (in this order): int id, string name, string description, double price Getters Create getter functions that return the member variable value when called. These can be defined inline or in the source file (Item.cpp). /ersion control MREADME.mdC++main.cpp C++Receipt.cpp Receipt.h Debug main ir D = c+0 Part III - Generating a Receipt In this section we will use our newly defined classes to output our Receiptltems in a formatted receipt with a total. Receipt Class you will use the Receipt class. It has been written by someone else to work with your completed ltem and Receiptltem classes. storing as Receiptltems into a vector. You will then be able to print the list of Receiptltems along with the final How to use the Receipt Class 1. \#include the Receipt class file header in main 2. Create a Receipt object. You will only need one instance. 4. Use the AddReceiptltem() method several times, adding many of your Receiptltems to the vector to print. 5. After this is finished, call the PrintReceipt() method. Your output should look similar to the following example: Submission Run the unit tests to make sure they are all passing. Take a screenshot of the passing tests. Verify that main has the correct output. Take a screenshot of the PrintReceipt() output. Zip up the folder called src. Should include the following files: Item.cpp, Item.h, main.cpp, Receipt.h, Receipt.cpp, Receiptltem.cpp, and Receiptltem.h Submit your work in the Challenge Activity assignment page for Module 3. M README.md C++main.cpp C ++Receipt.cpp Receipt.h Item.h Ihese can be detined inline or in the source tile (Item.cpp). - Names - Should use the word 'Get' then the name of the attribute. For example, for id_we should see GetId . Make sure to use UpperCammelCase. - Parameters - No parameters - Read-Only - Make these functions const since we will not be modifying the member variables within any of the function definitions. - Definition - These should simply return the member variable values. Setters Create setters that allow the class to control what the user can set the variables to. These must be defined in the source file (Item.cpp) - Return Types: These should not return any values so make them have a return type of void - Names - Should use the word 'Set' then the name of the attribute. For example, for id_we should see SetId . Make sure to use UpperCammelCase. Overloaded Operators The Item class will have 2 overloaded operators - Overloaded equality operator: == - Member function - This function should compare the member variables for two Item objects. Returns true if they are all equal and false if any are not. - Overloaded inserter operator: - Friend function - Prints the member variables of an Item object. Formatting is not important. The Receiptltem Class This class will manage the quantity of each item Files for modification - Receiptltem.h - Receiptltem.cpp The Receiptltem class will have 2 private member variables: - item_- Instance of the Item class - quantity_ type integer, keeps track the item count on our receipt m README.md CC + main.cpp C ++Receipt.cpp Receipt.h Receiptltem.h Item.h Adds together two Receiptltems. member variable, use a try/catch block. If they are not the same then throw an error. Returns an updated Receiptltem object - Overloaded addition operator for integers: + - Allows the user to add an integer to a Receiptltem object as the right-hand operand. For example: myReceiptItem +5 Returns an updated Receiptltem object - Overloaded subtraction operator for integers: - Allows the user to subtract an integer from a Receiptltem object as the right-hand operand. For example: myReceiptItem - 5 - Returns an updated Receiptltem object - Overloaded addition operator for integers: + Allows the user to add an integer to an Receiptltem object as the left-hand operand. For example: 5+ myReceiptItem should return an updated Receiptltem object - Overloaded inserter operator: - Prints specific member variables of the Receiptltem object and stored Item object. Item name_ - Left justified with a width of 50 chars Receiptltem quantity_ - Left justified with a width of 9 chars - Should look like this: (2) - Hint: Make the (quantity_) into a string using the std::to_string() function before trying to format with iomanip to make it work with () Print $ - Item price_ - Right justified with a width of 10 Returns an ostream object Hint: Remember to \#include the library, especially if you are on a Mac. Macs don't typically show a compiler error when it is missing, but Windows do. In the first section of main.cpp there are several TODO comments for creating and using Receiptltem objects. Follow these directions to use and test your Receiptltem class. Part II - Trying everything in main.cpp Now that each of your classes have been created. If you haven't yet already, switch over to main and follow the directions listed throughout the page in /ITODO comments and print statements up until Part 3 . Part III - Generating a Receipt In this section we will use our newly defined classes to output our Receiptltems in a formatted receipt with a total. M README.md main.cpp Receipt.cpp Receipt.h Receiptltem.h Item.cpp Item.h Constructors This class will need two constructors. - Default - Written inline using an initializer list - No parameters - calls Item default constructor - sets numerical values to zero - General - Parameters should include (in this order): Item food, int quantity - Sets the member variables to user chosen values. Should not allow user to set member variable values to a negative number. If the user tries this, give the variable a default value of 0 . Getters Create getter functions that return the member variable value when called. These should follow the same guidelines as the Item getters. These can be defined inline ( ReceiptItem. h ) or in the source file (ReceiptItem. cpp ). - Return Types - These should each be set to have the return type that is the same as the attribute being returned. - Names - Should use the word 'Get' then the name of the attribute. Make sure to use UpperCamelCase. - Parameters - No parameters - Read-Only - Make these functions are const since we will not be modifying the member variables within any of the function definitions. - Definition - These should simply return the member variable values. Getter for a Calculated Value - GetTotal() - Gets the total price for the quantity of item's - No parameters Setters These must be defined in the source file (Receiptltem.cpp). These should follow the same guidelines as the ltem setters. - Return Types: These should not return any values so make them type void - Names - Should use the word 'Set' then the name of the attribute. Make sure to use UpperCamelCase. - Parameters - Each setter should accept one parameter. It should be the same type as the related attribute. Overloaded Operators The Receiptltem class will have 6 overloaded operators - Overloaded addition operator: + - Adds toaether two Receintltems. are complete you will use them in main to create objects for a business of your choice. Objectives Demonstrate knowledge about the following topics: - Writing classes - Using objects - Passing \& Returning objects in functions - Overloading operators for user defined data types - Simple try/catch blocks Files to work on The following files are included in the src folder for you to either work on or use. - src/Item.h - src/ Item.cpp - src/Receiptitem.h - src/ReceiptItem.cpp - src/Receipt.h do not modify - src/Receipt.cpp do not modify - Your driver file will be src/main.cpp Unit Tests Do NOT modify the unit tests! Your code will be tested against a separate copy than what you have. - tests/receipt_test.cpp The Problem to Solve You and a team of software engineers have been tasked to create a receipt system. You have been assigned by your team to create two out of three classes that will be used: Item and Receiptitem The Item class will store information about any product that the user is purchasing. The purpose of the Receiptltem class will be to manage the quantity of each Item. README.md 1:1 CRLF UTF-8 2 spaces*

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

Management

Authors: Robert Kreitner, Charlene Cassidy

12th edition

1111221367, 978-1285225289, 1285225287, 978-1111221362

More Books

Students also viewed these General Management questions

Question

Expand the quotients by partial fractions. [+2 z(z 1)

Answered: 1 week ago