Project 4: Shopping Cart App with Dynamic Memory
DUE: Wednesday, May 23 at 11:59PM
The purpose of this project is to work with pointers, dynamic memory and operator
overloading. The project will be an app for customers to buy books from the book
wholesaler's website. The customer can then print out the inventory, add books, delete books
or change the amount of books in a 'shopping cart'. At the end of the program, the shopping
cart is printed and the 'inventory.txt' file is updated to reflect the new in stock totals for the
books bought.
File IO Changes
To make creating the array at run time more efficient, the 'inventory.txt' file will have an
added line to the top of the file. This will be a count of how many books in the file. So the
new format will be:
> make sure you read this in first!
So now you can read in the count first (before the for or while loop) and then make the array
to the right size before reading in the books. Also, you can use a for loop because you know
how many are in the file.
Remember when writing out the file to write out the count of books first before writing any
book fields.
What To Code
The main reason for doing this project is to be able to make everything in your program as
dynamic as possible. So you can borrow the Book and Inventory classes from project 3.
However, you will need to make the inventory array dynamic so it only holds the number of
actual books in the file.
You will need to create a list of items for the cart. You can do this in the Inventory class
where you have access to the inventory list or you can make a separate class. It should be a
separate list with it's own set of books so that you don't inadvertantly change anything from
the inventory list until the user has checked out. Use a struct to represent a cart item which
holds a Book object and an int to keep track of quantity. Then make a list of structs for the
cart.
Since this program is for customers who should NOT be able to add or modify inventory
information, these tasks should be removed from the Inventory class. Just keep the functions
that are needed such as printing all books, reading in and writing out the inventory.
Also, the Book class should have the following operators overloaded: ostream '<<' and
assignment '=' . Other operators to overload are up to the student. Remember to do a 'deep
copy' for the '=' operator (in other words, make sure the dynamic strings are copied to newly
allocated space and not just pointing to the same space). Use '=' when transfering the a book
in the inventory over to a book in the shopping cart.
Tasks
A user can do the following:
print all books in the inventory this is similar to project 3.
print current contents of shopping cart – Remember to redo and print out the total price.
add book to cart First, ask the customer for the book's title. If the title is not in the list,
print an error message and go back to the main menu. If the title is already in the cart, then
optionally you can ask the user if they want to change the quantity or you can print an error
message and go back to the main menu. Then ask to how many copies they want to buy.
Please check to make sure there is enough in stock for the amount of copies they want; if
not, you can reask this amount (or ask if they want all of what's in stock) or you can print an
error message and go back to the main menu. Also, if they enter zero or less, reask for an
amount.
change quantity a book in the cart – First, ask the customer for a book's title. If the title is
not in the cart, then print an error message and go back to the main menu. Else, ask them for
new quantity. Do the same checks for quantity that you do when adding to the cart.
DESIGN HINT: Since both add to cart and change quantity do the same thing, it would be
good to make the getting and checking for quantity in a separate function.
checkout – print out the total price. Then update in stock totals, write out the inventory to
the file and exit the program.
Requirements
The inventory array should be dynamically created to the size of the books in the
inventory. You need to read in the size of the list from the file and then create a list of book
objects of the actual number of books (as in Lab 5).
The shopping cart array of cart items can have a fixed size since we don't know how many
books a customer will buy. Just use 100 as a maximum size and make the user doesn't add
more items than this to the array. Use a default constructor to initialize dynamic strings to
nullptr and numbers to 0.
The Book class should have the following operators overloaded: ostream '<<' and
assignment '=' . Make sure you do a 'deep copy' of the Book class: all dynamicall created C
strings should occupy separate memory spaces.
You need to allocate all space including the Cstrings for the title, author and publisher.
All Cstrings in the book class should be the actual size of the title, author, etc. (plus one for
the null character). However, you will need to read in the title, author, etc. to a large enough
temp Cstring (about 250 is okay for this project). Then you need find the size of the string
using strlen, allocate memory for the member variable and then copy from the temp string to
the member variable.
Create a destructor for all classes that use dynamic memory. Remember to delete both the
Cstrings in the book class and the array in the inventory class. If the items in the cart were
also created dynamically (using the new keyword), then these need to also be deleted.
You must create at least two classes (Inventory and Book) but the cart or items in it can also
be a class. All member variables in each class must be private. The member variables
will be changed or read via a set of public member functions.
You will need more than one file for this project. Each class needs two files: a
implementation (.cpp) file and a header (.h) file. There will still be one main.cpp file. So you
need to turn in 5 or more files for this project. Please see d2l for module 4 "Using Multiple
Files" for how to structure your code.
You need to use Cstrings, no 'string' type in your entire code.
All functions, including main, must be less than 30 lines long (this count does not include
comments, blank lines and { } on separate lines).
No global variables are allowed in this program. Create all variables in the main function
and pass it into functions in main.cpp if this is needed.
The program should not crash if the file 'inventory.txt' is not present. In this case, print an
error message and exit the program.
Project Submission
Same as project 3's submission except I would like you to do a recorded script. of running
your program through gdb debugger. Set a breakpoint, run the program to the breakpoint and
then continue running it to completion. This script. can be a separate file from the compiling
and running script. or it can done at the same time.
OUTPUT 1 (All user input is underlined and in gray)