Question
C++ Create a class ShoppingCart that will represent the users virtual shopping cart. The shopping cart will use the STL std::vector for storing items that
C++
Create a class ShoppingCart
that will represent the users virtual shopping cart. The shopping cart will use the STL std::vector
for storing items that are added to the cart, and the public interface will expose methods that allow specific kinds of operations to be performed on the virtual cart.
Create the files ShoppingCart.h
and ShoppingCart.cpp
to contain the class definition and method implementations for your ShoppingCart
class. Dont forget to add include guards in the header file! It might be a good idea to start a new project directory for this assignment, and copy the Item
library from your in-lab exercise into it.
Class description for ShoppingCart
:
The shopping cart only needs a single internal (private) attribute: a std::vector
of Item
objects.
The public interface of the cart must provide the following methods. You mustname the methods exactly as specified; assume that your code will be tested against a different main program during grading.
- Constructor
- The default constructor is all that is needed for the cart; the implicit default constructor is OK, so there is no need to define any constructor.
add()
- the
add()
method must take anItem
as a parameter and add it at the end of the cart if the item isnt already in the cart. If the cart already contains the item, increase the quantity of the existing item. Two items are considered the same if and only if both the name and price are the same. This method does not return any value. If the item to be added to the cart has a quantity less than 1, it must not actually be added to the cart. update_quantity()
- The
update_quantity()
method must take the name of an item (a string) and the new quantity (an integer) as parameters. If the cart contains the item, the quantity must be updated to the new value and the method must returntrue
. If no item in the cart matches the name provided, the method must returnfalse
. If the new quantity is zero (or any negative value), the item must be removed from the cart if it is found and the method must returntrue
. Note that it is possible that two items with the same name (but different prices) could exist in the cart due to the behavior of theadd()
method if this is the case, the first item in the cart (the one nearest the beginning) must be updated. remove()
- The
remove()
method must take the name of an item (a string) and remove the matching item from the cart if the cart contains an item with that name. If the cart did contain the item, the method must returntrue
; if the cart did not contain the item, the method must returnfalse
. Note that it is possible that two items with the same name (but different prices) could exist in the cart due to the behavior of theadd()
method if this is the case, only the first item in the cart (the one nearest the beginning) must be removed. total_value()
- The
total_value()
method takes no parameters and must return the total value (total price) of all items in the cart. Note that the total value must take into account the quantity of each item. If the cart is empty, the total value is zero. write()
- The
write()
method must write the name, price, and quantity of all items contained in the cart, as well as the total value (total price) of all items in the cart to the console in a clean, easily readable format. Note that the total value must take into account the quantity of each item.
Testing
Create a main program cart_test.cpp
to test your shopping cart library. There are no specific input/output or interface requirements for this testing code, but you should be sure that your tests exercise your library fully. Dont just test the obvious or common use-cases, be sure to look for edge cases as well things that are not expected to happen (or things that would be unlikely to happen), but that are not prohibited by the behavior guidelines specified above.
Hint: For some recommendations and hints about writing good testing code, see our Testing Recommendations page.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started