Homework 2 - Basic Classes
Due: Tuesday January 25, 2011 at 5:00pm
Coding Conventions
Use the same coding conventions as described in
Homework 1. Additionally, make sure to indent each
section of the class in the class definition.
Assignment
For this assignment, you will be modifying the structure from
Homework 1 into a class. Your ProductInfo
class will contain the following member variables in the private section:
- A C-style string for the product name
- A C-style string for the product description
- A 13 character C-style string for the UPC (Universal Product Code)
- An integer for how many of that product are in stock
Your class will now have the following member functions in the public section:
- Default constructor - Sets name, description and UPC to the empty
string. Sets the number of products in stock to 0.
- Constructor that takes four parameters (three strings and one integer) -
Sets the name, description, UPC and number in stock to the corresponding
parameter
- Accessor functions for each member variable:
- getName - returns a string containing the name
- getDescription - returns a string containing the description
- getUPC - returns a string containing the UPC
- getStock - returns an integer for the number of products in stock
- Mutator functions for each member variable:
- setName(char *) - Copies the given string into the name. Make sure
to limit the number of characters copied to the maximum number of
characters your name string supports.
- setDescription(char *) - Copies the given string into the
description. Again, limit the number of characters copied to the
number of characters in your description string.
- setUPC(char *) - Copies the given string into the UPC. Only copy
13 characters over to the UPC.
- setStock(int) - Sets the number of products in stock to the given
integer.
- inputProduct - Prompts the user to set each one of the product fields for
the current object (similar to addProduct for Homework 1).
- printProduct - Prints all the product fields to the screen (similar to
showProduct for Homework 1).
Additionally, your member functions must enforce the following constraint:
- The number of items in stock must be greater than or equal to 0.
Main Function
Your main() function will create an array of products, similar to Homework 1.
However, this time you will use the following menu to drive your main()
function. You can do all of the menu operations within main() or you can add
functions to support the menu operations. See
menu_example.cpp for an example of a
menu-driven program.
Welcome to the CS222 Homework 2 Menu
===================================================
1. Input all product information from the keyboard
2. Print all product information to the screen
3. Edit the stock level for a single product
0. Exit
===================================================
Option 0 will deallocate the array if it is allocated and exit the menu loop.
Option 1 will work similarly to Homework 1. It should prompt the user for how
many products there are, verify that the number of products is greater than 0,
dynamically allocate an array of products and then call inputProduct for each
item in the array.
If the user selects Option 1 a second time, the old array MUST be deallocated
before following the above steps.
Option 2 will loop through all products in the array and call printProduct. If
the user has NOT selected Option 1 yet (there is no array allocated), Option 2
should print an error message reminding the user to select Option 1 first.
Option 3 will print all the products and ask the user which product they wish
to update. It will then ask the user what the new stock level is for that
product. It will then update that product to the new stock level and reprint
just the selected product to show the user that the new stocking level has
been set.
As with Option 2, if the user selects Option 3 before selecting Option 1, you
should print an error message reminding the user to select Option 1 first.
Important Items
You will lose points if you do not implement these important features in your
menu program:
- If Option 1 is selected more than once, the old array MUST be deallocated
BEFORE new is called again.
- Option 0 MUST only deallocate the array IF it has been allocated (i.e. if
Option 1 has been selected).
- If Options 2 or 3 are selected before Option 1, your program MUST print
an error message to the screen and return to the menu. You will lose points
if your program segfaults under these conditions.
Test that you have properly implemented these items by running your menu
with out-of-order input. For example, run it once and immediately select 0 to
exit without selecting any other option and make sure it gracefully exits.
Then run it again and select 3 and make sure it prints an error message and
returns to the menu. Do not just test your code by selecting 1, then 2, then 3.
Email the completed source code to me.