Assignment #4

COP3330 Object Oriented Programming in C++

Spring 2009

Due: Friday, March 6th, 2009

Objective

Upon completion of this program, you should be able to work with dynamic arrays of objects, as well as the use of objects from more than one class, using the composition ("has-a") relationship.  This program should be portable, so it will work with any C++ compiler.  Make sure that it works with the g++ compiler and with Visual C++ before you hand it in.

Task

You will be writing classes that simulate sales at a store that sells books, DVDs, and software, as well as the accounting of the day's sales on one cash register. The register will keep track of a list of the most recent sales. You will need to finish the writing of two classes:  Sale and Register.  The full header file for the Sale class has been provided in a file called sale.h.  The Register class is described below. You can get a copy of sale.h here


Program Details and Requirements:

Sale class

Using the Sale class declaration, write the sale.cpp file and define all of the member functions that are declared in the file sale.h.   (Do not change sale.h in any way.  Just take the existing class interface and fill in the function definitions).  Notice that there are only four kinds of items:  BOOK, DVD, SOFTWARE, and CREDIT.  The sale.h file contains the descriptions of how the functions should work and what the member data variables represent.  Make sure that the member functions only do their intended tasks (i.e. no extraneous inputs, outputs, or results that are not specified).
 


Register class

Write a class called Register (filenames are register.h and register.cpp).  A Register object should store at least the following information:  an identification number (an integer) for the cash register object, the amount of money in the cash register object, and a list of sales. All member data must be private.

There is no size limit to the sales list, so it should be implemented with a dynamically allocated array.  (This means you'll need an array of Sale objects). There should never be more than 5 unused slots in this array (i.e. the number of allocated spaces may be at most 5 larger than the number of slots that are actually filled with real data).  This means that you will need to ensure that the array allocation expands or shrinks at appropriate times.  Whenever the array is resized, print a message (for testing purposes) that states that the array is being resized, and what the new size is.  Example:  "** Array being resized to 10 allocated slots".  Correct memory management should be used, including the cleanup of memory (i.e. you must program the class so that it leaves no "memory leaks").  This means that you need to deallocate any dynamic space, when and where appropriate.

You can add any private data and functions into the Register class that you feel are useful, but your Register class must have at least the following functions (names and descriptions provided here) in its public interface. These functions should not do extraneous input, output, or other non-specified tasks (i.e. they do only what is specified).

(Hint: keep in mind that inside the register, you are keeping a dynamic array of Sale objects. This means that most of these functions will be using this array to do their work -- and they can also call upon Sale class member functions).

Constructor
The constructor should take in two parameters, which allow the ID number and starting amount in the register to be initialized when the Register object is created. To create a Register object, this data must be passed in.

the destructor
The destructor should do any appropriate clean-up tasks needed to manage the dynamic memory

- GetID
- GetAmount
These accessor functions should return the current ID number and current amount in the register, respectively, to the caller.

- RingUpSale
This function allows the item type and base price of a sale to be passed in as parameters. This function should store the sale in the sale list, and it should update the amount of money in the cash register appropriately. Items that are bought will add money to the register.  Remember that sales tax must be added to the base price of any item sold. If the sale type is CREDIT, then you should deduct the amount from the register.

- ShowLast       // display the last sale made
- ShowAll        // display entire sale list
These functions will display information about sales to the screen.  ShowLast should display only the information about the last sale that was made.  ShowAll should show all of the sales currently in the sale list, one per line.  This display should be in the order oldest to newest.  (i.e. the most recent sale is displayed last).  If there are no sales currently in the list, then output an appropriate message instead (like "No sales have been made").

- Cancel
This function should cancel the last sale in the list.  This means that the amount in the cash register, as well as the data in the sale list, should be adjusted accordingly (as if the sale had never happened).  This simulates the idea that a sale rung up incorrectly can be voided out by the cashier.  If the sale list is empty, print an appropriate error message and abort the cancel operation.

- SalesTax
This function should calculate and return the total amount of sales tax for the last n sales (i.e. the most recent ones), where n is an integer passed in as a parameter. If n is higher than the number of sales currently stored, just calculate the total tax for all sales currently in the sale list. If n is invalid (a negative number), print an appropriate error message and return 0.
 


Test Program

I've created a menu program that will help you interactively test the features of your Register class. You can get a copy of menu4.cpp here.

This menu program will start by prompting the user to input a cash register ID and the starting amount in the register. Then it will create a Register object and enter a menu loop -- the menu options will work with both lower and upper case inputs:

  S:   Show current amount in the cash register 
  R:   Ring up a sale 
  D:   Display the last sale 
  L:   Display the entire sale List 
  C:   Cancel the last stored sale 
  T:   Find the Total sales tax for recent sales 
  M:   Show this Menu 
  X:   eXit the program 

Some of the menu options will prompt for other inputs. Each will allow you to interactively test the behavior of your Register class public interface. The code in menu4.cpp also demonstrates appropriate calls to the Register class member functions.

 


Submit the following files (using the usual web submission procedure):

  sale.cpp 
  register.h 
  register.cpp