首页 > > 详细

C/C++设计辅导留学生、辅导留学生 Shopping Cart App with Dynamic Memory 、C/C++程序调试

CS 162 ­ Spr 2018
 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 re­do 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 re­ask 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, re­ask 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 C­strings for the title, author and publisher.  
All C­strings 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 C­string (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
C­strings 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 C­strings, 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)
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
******** Overstock Books Wholesaler's shopping app *******
********MAIN MENU**********
1 ­ Print Inventory
2 ­ Print Shopping Cart
3 ­ Add to Shopping Cart
4 ­ Change Quantity in Cart
5 ­ Checkout Cart
6 ­ quit without checking out
Enter choice: 
1
%%%%%%%%%% Book Inventory %%%%%%%%%%%
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: 1984
Author: George Orwell
Publisher: HarperCollins
In Stock: 202
Unit Price: 20.99
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: Oliver Twist
Author: Charles Dickins
Publisher: Random House
In Stock: 152
Unit Price: 14.99
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: White Fang
Author: Jack London
Publisher: Random House
In Stock: 114
Unit Price: 11.00
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: Dracula
Author: Bram Stoker
Publisher: Penguin Books
In Stock: 234
Unit Price: 15.99
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: Persuasion
Author: Jane Austin
Publisher: Random House
In Stock: 323
Unit Price: 12.99
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: Hard times
Author: Charles Dickins
Publisher: Penguin Books
In Stock: 317
Unit Price: 11.99
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: Flatland
Author: Edwin Abbott
Publisher: Random House
In Stock: 793
Unit Price: 23.33
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: Alice in Wonderland
Author: Lewis Carroll
Publisher: HarperCollins
In Stock: 124
Unit Price: 14.99
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
********MAIN MENU**********
1 ­ Print Inventory
2 ­ Print Shopping Cart
3 ­ Add to Shopping Cart
4 ­ Change Quantity in Cart
5 ­ Checkout Cart
6 ­ quit without checking out
Enter choice: 
3
Title to add?
1984
Quantity to put in cart? 
10
=== CART SUCCESSFULLY UPDATED ===
********MAIN MENU**********
1 ­ Print Inventory
2 ­ Print Shopping Cart
3 ­ Add to Shopping Cart
4 ­ Change Quantity in Cart
5 ­ Checkout Cart
6 ­ quit without checking out
Enter choice: 
2
========= CART ===============
Title: 1984
Unit Price: 20.99
Quantity: 10
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
 Cart Total: 209.90
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
********MAIN MENU**********
1 ­ Print Inventory
2 ­ Print Shopping Cart
3 ­ Add to Shopping Cart
4 ­ Change Quantity in Cart
5 ­ Checkout Cart
6 ­ quit without checking out
Enter choice: 
6
===== Thank you for using our app! =====
OUTPUT 2 (All user input is underlined and in gray)
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
******** Overstock Books Wholesaler's shopping app *******
********MAIN MENU**********
1 ­ Print Inventory
2 ­ Print Shopping Cart
3 ­ Add to Shopping Cart
4 ­ Change Quantity in Cart
5 ­ Checkout Cart
6 ­ quit without checking out
Enter choice: 
1
%%%%%%%%%% Book Inventory %%%%%%%%%%%
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: 1984
Author: George Orwell
Publisher: HarperCollins
In Stock: 212
Unit Price: 20.99
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: Oliver Twist
Author: Charles Dickins
Publisher: Random House
In Stock: 152
Unit Price: 14.99
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: White Fang
Author: Jack London
Publisher: Random House
In Stock: 119
Unit Price: 11.00
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: Dracula
Author: Bram Stoker
Publisher: Penguin Books
In Stock: 234
Unit Price: 15.99
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: Persuasion
Author: Jane Austin
Publisher: Random House
In Stock: 323
Unit Price: 12.99
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: Hard times
Author: Charles Dickins
Publisher: Penguin Books
In Stock: 317
Unit Price: 11.99
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: Flatland
Author: Edwin Abbott
Publisher: Random House
In Stock: 793
Unit Price: 23.33
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Title: Alice in Wonderland
Author: Lewis Carroll
Publisher: HarperCollins
In Stock: 124
Unit Price: 14.99
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
********MAIN MENU**********
1 ­ Print Inventory
2 ­ Print Shopping Cart
3 ­ Add to Shopping Cart
4 ­ Change Quantity in Cart
5 ­ Checkout Cart
6 ­ quit without checking out
Enter choice: 
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!