Change into your cps6 directory and create a directory called "assign6". Change into the "assign6" directory. In order to do this assignment, you need to copy files using the following "cp" command (don't forget the trailing period, or dot). Type:
cp ~dr/cps6/assign6/* .
If you type "ls" you should see "Makefile", "fish1.inv", "fish1.sale", "fish2.inv", "fish2.sale", "gwindow.h", "gwindow.cc", "inventory.h", "inventory.cc", and "fishinv.cc".
For the programming problem that follows, you should use the style rules discussed in class, which includes meaningful variable names, indentation, and comments at the top of the file and for each function.
In this assignment, you'll use the Inventory class to manage the inventory of the store, a Vector to store the information (name, quantity, etc.) for each item in the store, and the Window class to draw a histogram picture of the inventory.
NOTE: This doesn't show anything until you create an Inventory variable. Declare a variable of type Inventory in the main function in fishinv.cc, then try this out.
Let's compile the program and see how to run it. Compile the program fishinv.cc by typing: make fishinv (Note that you may get a fair number of WARNINGS when you type "make" troughout this assignment. Usually these warnings can be ignored.)
We'll be using the Samba animator to display the picture. The animator will only work under X windows; any of the Sparc 5's should work fine.
To use the animator, you should have first compiled fishinv.cc above. The Inventory and Window class output animator commands that are understood by the Samba animator. To run your program, type:
fishinv | samba
Go ahead and run the program now. An Samba window should appear. You might need to move the Samba window so you can see it and the other windows at the same time. To start the animation, click on the button start . To quit at any time, click on the quit button. Not much happens at this point, you should see a line drawn.
The animator is set to run on high speed. The Polka window has a speed control bar which is currently set on the highest speed. You might want to slow the animation down until your program is correct, so you can see how the picture is drawn. Move the rectangle in this column to midway to slow the animation down before clicking the start button.
IMPORTANT NOTE: All output is being sent to the animator. If you want to print anything to the screen (for debugging purposes or otherwise) you'll need to print the word comment at the beginning of each output line and put an endl at the end of each line output. That is, each cout statement in your program should start and end with:
cout << "comment " << .... << endl;
The Inventory class will store lots of information about each item that can be bought at the fish store. The private section of inventory.h includes the following declarations:
Window myPicture; // graphic window variable Vector <FishInfo> myList; // inventory of items in store int myNumItems; // number of items in myList int mySize; // max capacity of myList
The variable myPicture is a graphic window that will be used to display the histogram. The variable myList is a vector of type FishInfo. FishInfo is a struct with several parts.
struct FishInfo
{
string name; // name of item
int nameident; // tag for name displayed
int quantity; // quantity of items; if negative, items on order
int ypos; // y position to display this item and its points
Vector<int> tag; // array of tags for marks
};
For each item in the store, we'll keep track of its name and current quantity. The name will be displayed in the picture using the Text function from the Window class. The Text function returns a unique number, which will be stored in the variable nameident. Each item will be drawn in the picture at a different y-position. The y-positions for each item have already been calculated for you and stored in the variable ypos.
If an item has quantity X, then X marks will be drawn in the picture. Later, when transactions are processed, the quantity may increase or decrease. If it decreases, then you'll have to remove some of the marks. Marks are drawn as points using the Window class function Point. Remember that each object drawn in the Window class has a unique number (or tag) associated with it. This tag can be used to change it's color, hide it, etc. For every mark created in the picture, you will have to save the tag associated with it in case later you have to remove the mark. Thus, each item will have a vector called tag that can hold the unique tags for up to 100 marks, which will be the largest quantity possible.
Thus myList is a vector where each item in the vector contains several parts, one such part is another vector, tag.
In this part, you will create an Inventory and read in the data. The data for this program comes in two files. One file has the original inventory (filename ends in .inv). Each line contains the name of an item (one word with no blanks) and a nonnegative integer containing the quantity. A small sample inventory file is shown below. The lines show that there are two inventory items, discus and catfish. There are currently 6 discus fish and 0 catfish.
discus 6 catfish 0
Two sample inventory files have been provided for you, fish1.inv (smaller) and fish2.inv. You should make sure the program works for the smaller file before testing the larger file.
A second file provides transaction information about the inventory and may contain new inventory items (filename ends in .sale). Each line contains three items: the name of the item (one word with no blanks), the quantity (nonnegative) involved in the transaction, and the type of the transaction (bought or sold). Items sold decrease the current inventory (or make it negative if items are on backorder), and items bought increase the inventory. In the sample transaction file shown below, 3 discus were sold, 4 catfish were bought, and 2 angelfish (a new item in the inventory) were bought.
discus 3 sold catfish 4 bought angelfish 2 bought
Two sample transaction files have been provided for you, fish1.sale (smaller) and fish2.sale.
Make the following changes in the file fishinv.cc.
Compile your program and run it to make sure that it is correct. Run it with fish1.inv and fish1.sale as input. The only thing added to the picture at this time is the title. Currently, almost all output is being sent to the xterm.
Make the following changes to the program.
For example, if the item name is leaffish and the quantity is 5, then the word leaffish would be displayed in the picture, and five marks would be displayed to the right of it.
You can call the function CreateNameIdent to display the name, and you can call the function CreateTags to display the marks. These functions are private member functions that have been provided for you. See their pre and post conditions to see how to use these functions.
Compile your program by typing make fishinv and run it by typing fishinv | samba
If you use the datafiles fish1.inv and fish1.sale, then click here to see what the picture should look like. The name of your fish store may be different. If you use the datafiles fish2.inv and fish2.sale, then click here to see what the picture should look like.
Make the following changes to your program.
Test this function before moving to the next change to make sure it works correctly. In fishinv.cc, after reading in the inventory, add a few calls to the function LookupItem for items that should be in the array and items that should not be in the array. Once it works correctly, remove these test calls.
For example, if there were currently 5 leaffish, and UpdateItem was called for the item leaffish, with quantity 8 and transaction "sold", then the current five black marks would be erased, and three red marks would be written to indicate the quantity is now -3, meaning 3 leaffish are on backorder.
There will be many cases to consider here as to whether you start with red or black marks and end up with red or black marks. Here is another case. If there are currently -3 leaffish (3 red marks indicating 3 leaffish are on backorder) and 4 more leaffish are sold, then 4 more red marks should be added, displaying 7 total.
Consider using the function DisplayPoint, which has already been written for you. If you call DisplayPoint on a point that is already drawn, it will hide the point. If you call it on a point that is hidden, then it will redisplay the point. If you call it on a point that doesn't exist (a mark has never been drawn in this position) then it will display a point in black. If the point needs to be red, you can then call the Color function in the Window class to color it red. Note the name of the Window variable is myPicture.
For example if a point is not displayed in a particular position myList[x].tag[y] then call
DisplayPoint(x,y); // display a previous point or create a
// new black point
myPicture.Color(myList[x].tag[y], "red"); // color the point red
If the transaction item does not appear in myList, then add it. Careful, You can call LoadNewItem if the quantity is positive. But if the quantity is negative, the marks will have to be colored red.
UpdateItem should use LookupItem to determine if the item is already in the list.
Compile your program by typing make fishinv and run it by typing fishinv | samba
If you use the datafiles fish1.inv and fish1.sale, then click here to see what the final picture should look like. If you use the datafiles fish2.inv and fish2.sale, then click here to see what the final picture should look like.
NOTE: Make sure your histogram is dynamically updated. That is, the picture should be updated after each transaction. Waiting until all transactions have been processed and then displaying the final picture will not receive full credit.
If you do any extra credit, make sure you put a note in your README file mentioning that you did the extra credit.
Modify your program in the following manner.
In fishinv.cc, call this new member function once, after all the transactions have been processed.
Call this new member function once in fishinv.cc, after all transactions have been processed (and after RemoveZeros if you did that part).
When all your programs compile and produce the correct output, create a "README" file (please use all capital letters). Include your name, section number, the date, and an estimate of how long you worked on the assignment in the "README" file. If you did the extra credit, mention this. You must also include a list of names of all those people with whom you consulted or received help from on the assignment. See the CPS 6 syllabus for the rules on consulting.
To submit your programs electronically type:
submit6 assign6 README fishinv.cc inventory.cc inventory.h
You should receive a message telling you that the programs were submitted correctly.