h3main.c
, h3invent.h
, h3invent.c
,
and h3.scr
.
You are to write a program that maintains inventory for a store. The program will essentially use an array to hold these items.
The store has many different products. For each product, you will keep track of its:
Make sure you document this header and wrap it (see
.c
file if they will help you implement
those 6. Any extra helper functions should be prototyped in
this .c
file (i.e., not in the .h
file).
Also, these helper functions should not be called directly by people
using the module, i.e., in the main program.
Below is how each inventory function should operate and how you might call each in the main program (the list of inventory is always passed by reference so that it can be changed by functions that must do so).
InventoryInitialize()
: Should set up an inventory variable to
represent an empty inventory. Use it something like:
inventoryT list; ... InventoryInitialize(&list);
InventoryAddItems
: Should add a certain number of units of
the specified item to inventory. The item to be added is passed in the
'item' (structure) parameter, whose 'count' field should represent how
many units to add. Use this function something like:
itemT item; /* Set the fields of item to the proper name, count and price. */ strcpy(item.name, name); item.count = count; item.price = price; count = InventoryAddItems(&list, item); if (count == INVENT_PRICE_MISMATCH) { /* New price not same as stored price! */ }
This function will first need to see if the item is already in inventory.
When an item ISN'T already in inventory, you just need to add it (if there is room) and return the new number of units of the item. If there is no room left (the array that stores it has a limited size), this function should return the constant INVENT_NO_ROOM.
When an item IS already in inventory, you need to make sure that the specified price for the item and its old price (the price already stored with it in inventory) match, else this function should return the constant INVENT_PRICE_MISMATCH and should not change the inventory. If the prices match, you just need to increase the item's count and return the new number of units of the item.
InventoryRemoveItems
: Should remove a certain number of
units of an item from inventory. The item to be removed is passed in
the 'item' (structure) parameter, whose 'count' field should represent
how many units to remove. Used something like:
itemT item; /* Set up the item's name and count; price is ignored. */ strcpy(item.name, name); item.count = count; count = InventoryRemoveItems(&list, item);
If the item's count reaches zero, you should also remove it completely from inventory.
The function should return the new number of units of the item after the decrease. If there are not enough units of the item to remove, it should leave inventory alone and return the constant INVENT_NOT_ENOUGH. If the item does not exist, it should return INVENT_NO_SUCH_ITEM and also leave inventory alone.
InventoryGetPrice
: Should return the price for a given item.
You'll use it something like:
itemT item; /* Set up the item's name; price and count are ignored. */ ... price = InventoryGetPrice(&list, item);
The function should return the price or INVENT_NO_SUCH_ITEM if the item does not exist.
InventorySetPrice
: Should change the price of an item.
You'll use it something like:
itemT item; /* Set up the item's name and new price; count is ignored. */ ... price = InventorySetPrice(&list, item);
The function should return the old price or INVENT_NO_SUCH_ITEM if the item does not exist.
InventoryReadFromFile
: Should read in a set of inventory
items from a file, whose name is specified as a parameter. You'll use
it something like:
count = InventoryReadFromFile(&list, "the-file-name");
The function should return the number of inventory items read in or INVENT_FILE_ERROR if it encounters a problem reading items from the file.
Note that such a file will contain one set of items per line, listed as name, count, and price (in cents), as in:
Pinesol 7 199
Product names will never have spaces in them, and thus, can be read in
with the %s
format.
Thus, it is the main program's responsibility (described below) to examine these return values and print out proper error messages.
/cs/course/cs113/current/hw3/inventory.dat
% h3inventory Initial inventory read from:/cs/course/cs113/current/hw3/inventory.dat
Please choose one of the following operations by typing its letter and then pressing <Return>: a) Add some items. r) Remove (sell) some items. p) Show the price for an item. c) Change the price of an item. q) Quit inventory. Choice> a Item name: Pinesol Add quantity: 5 Unit price (in cents): 199 There are now 12 units of "Pinesol". Choice> r Item name: Pinesol Sell quantity: 2 There are now 10 units of "Pinesol". Choice> p Item name: Pinesol A unit of "Pinesol" goes for $1.99. Choice> c Item name: Pinesol New unit price (in cents): 299 Price of "Pinesol" changed by $1.00. Choice> p Item name: Turtlewax No such item "Turtlewax" in inventory! Choice> a Item name: Pinesol Add quantity: 10 Unit price (in cents): 299 There are now 20 units of "Pinesol". Choice> q Bye! Thanks for using inventory.
We have written a make file for this program that will generate the executable ``h3inventory'' for the inventory program above.
This make file is named h3makefile. Since the utility make normally expects the makefile to be named ``Makefile'' or ``makefile'', run the make utility with:
make -f h3makefile
h3.scr
) must contain: each source code
file (use "cat" to display h3main.c
, then
h3invent.h
, then h3invent.c
), compilation
(using our make file), and test runs (in that order). You may
construct your own test runs, but they must demonstrate each of the
operations, plus all the error handling. Your tests will have to be
more elaborate than the example run above, since ours does not test all
the error conditions; however, don't make your test runs longer than is
necessary to demonstrate all the features.