Array of Structures, Character String Input and Output, Searching and Sorting

This programming assignment uses many of the ideas presented in sections 3 through 11 of the course notes, so you are advised to read those notes carefully as well as the following program specification.

The Program Specification: Simple Inventory

The Mall-Wart Mercantile Co needs to track its current inventory. The program will read an input file which will specify the initial inventory (see inventory file description below). The initial inventory will consist of a number of different items; for each item, a product number, a text description, the initial number of units in stock, the cost of one unit and the price of one unit will be specified. You should typedef an appropriate structure variable to store all the information about a particular item. This Structure should also include an area for message (you will see later how to use this area). The default message should be "Not Used" - in case no transactions is performed using the item.

The program will first read and store the initial inventory data and then proceed the inventory transactions. There are two types of transaction with which the inventory program must deal: sell and buy.

A sell transaction occurs when a customer wishes to purchase a certain number of units of a particular item from the store (e.g., 7 units of item number 1731). The program must look up item 1731 in the inventory list and determine if there are enough units of 1731 in stock to make the sale. If there are, then the number of units in stock should be adjusted to reflect the sale. Also, the message area in the structure should have "Trans. Completed".
If there are not, then the sell transaction should sell whatever there  is in stock, and the number of units in stock goes to zero. (Partial sales are made:  if the customer's full request cannot be met then she/he receives whatever there is in stock. The message area  in the structure should have "Stock Insufficient".)

A buy transaction occurs when the Mall-Wart Mercantile management wishes to purchase a certain number of units of a particular item to increase the store's supply (e.g., 8 units of item number 1996). The program must look up item 1996 in the inventory list and update the number of units in stock to reflect the purchase. (Buy transactions always succeed.). The area message in the structure should have "Trans. Completed".

There are some other calculations that must be performed; those are described in the calculations section.

When all the transactions have been processed, the program must print the final inventory list, as described in the output section below.

Input file description and sample:

This program requires the use of one input file with two sections. The first section contains the initial inventory list and the second section contains the transactions. Your program must read from a file named inventory.data — use of another input file name will generally result in a score of zero.

A newline character will terminate each input line. You may assume that all of the input values will be logically correct (no negative or missing values, for instance), that they will be in the specified ranges, and that they will be given in the specified order.

Initial Inventory Section

The first line of the input file specifies the number of inventory items, a positive integer no greater than 50.

Each remaining line of this section of the input file will contain the following data:

Note that the alignment of the unit numbers in the inventory list is not perfect, because the combination of tabs and spaces may not align the numbers correctly. This is a good example of why we suggest that you use spaces (not tabs) to align your output. Here, we use the tab character because it makes it somewhat easier for you to read the item descriptions.

Use an array of structures to store this table.

The initial inventory section of the input file is followed immediately by a list of transactions, as described below.

Transactions Section

Each line of the transactions section will contain a transaction entry, consisting of three fields:

There is no guarantee that the character string will be valid. Legal values are "sell" and "buy" but there may also be lines with other, illegal, values; those lines should be read but otherwise ignored. The character string for the transaction is guaranteed to be no more than 8 characters long.

There is no guarantee that the item number will correspond to any item in the inventory list. In that case, the transaction should be ignored.

There is no guarantee, for a sell transaction, that the specified number of units is available in inventory.

There is no guaranteed limit on the number of transactions.

An example input file appears on the next page:

What to Calculate:

Your program must output a table showing the final inventory, after all the transactions have been read and processed, if possible. The table format is shown in the output section below. In particular, for each inventory item, you must calculate:

In addition, you must sort the inventory list before printing it, so that the items are listed in alphabetical order of their descriptions. Modify the selection sort code given in chapter 11 of the course notes to do this, using the strcmp() function to compare the character strings.

# items:   19
 3809 Cayley               4      18.42      14.77
 4071 Widget Snaffler, Green            12      18.38      14.23
 4107 #8-32 Knurled Nut                  8       7.29       2.48
 4297 11th Century Anasazi Pot          17      14.05      10.78
 4377 Small Bevel Square                20      20.01      19.92
 1613 Microsoft Office 4.0               9      10.89       7.39
 1691 WDC31600 Hard Disk                18       9.16       5.48
 1730 Scroll Saw Blade 10-Pack          18      20.48      16.52
 1996 2 lb Mallet                        1       6.52       1.59
 2028 5 lb 3.5" AOL Diskettes           13      16.72      15.97
 2029 Rubber Mallet                      3       3.50       0.77
 2258 80386 CPU, 20MHz                  21      13.41      11.28
 2471 16MB 80ns SIMM Module              3       6.28       5.79
 2720 Project Adhesive, tube            18       7.21       5.41
 3045 Stainless Steel Foo Bar            1       3.71       2.10
 3055 Humphrey (Beanie), mint           12      19.73      17.65
 3212 VAX VMS User Manual set            1       8.40       3.44
 3381 Skydiving for Dummies             16      13.96       9.09
 3600 Myst                              21      21.16      18.18
sell        2258    17
sell        1996     1
sell        2471     3
sell        4107     2
buy         3600    18
sell        1731     7
sell        4377    12
sell        1613     3
sell        1730    42
sell        4071     6
sell        2720     2
sell        4071     2
buy         4107     3
buy         2720    16
sell        3809     2
sell        1730    14
sell        2720    30
buy         3600    19
sell        1613     3
sell        3055    26
buy         3600    33
sell        1691    16
sell        4297     7
sell        1730     1
sell        4107     3
sell        3381    10
sell        3381     4
sell        3055     7
buy         4297     5
sell        1996     6
burp        4107     2
buy         1996     8
sell        2258    10
sell        3809    26

Output description and sample:

Your program must write its output data to a file named inventory.out — use of any other output file name will result in a score of zero. Here is the output file corresponding to the given sample input:

Programmer:  Joe Doe
Project UFDA! - Simple Inventory

    # Name                        Acty Stck   Price    Cost              Message
--------------------------------------------------------------------------------
 4107 #8-32 Knurled Nut             -2    6    7.29    2.48     Trans. Completed
 4297 11th Century Anasazi Pot      -2   15   14.05   10.78     Trans. Completed
 2471 16MB 80ns SIMM Module         -3    0    6.28    5.79     Trans. Completed
 1996 2 lb Mallet                    7    8    6.52    1.59     Trans. Completed
 2028 5 lb 3.5" AOL Diskettes        0   13   16.72   15.97             Not Used
 2258 80386 CPU, 20MHz             -21    0   13.41   11.28   Stock Insufficient
 3809 Cayley                        -4    0   18.42   14.77   Stock Insufficient
 3055 Humphrey (Beanie), mint      -12    0   19.73   17.65   Stock Insufficient
 1613 Microsoft Office 4.0          -6    3   10.89    7.39     Trans. Completed
 3600 Myst                          70   91   21.16   18.18     Trans. Completed
 2720 Project Adhesive, tube       -16    2    7.21    5.41     Trans. Completed
 2029 Rubber Mallet                  0    3    3.50    0.77             Not Used
 1730 Scroll Saw Blade 10-Pack     -18    0   20.48   16.52   Stock Insufficient
 3381 Skydiving for Dummies        -14    2   13.96    9.09     Trans. Completed
 4377 Small Bevel Square           -12    8   20.01   19.92     Trans. Completed
 3045 Stainless Steel Foo Bar        0    1    3.71    2.10             Not Used
 3212 VAX VMS User Manual set        0    1    8.40    3.44             Not Used
 1691 WDC31600 Hard Disk           -16    2    9.16    5.48     Trans. Completed
 4071 Widget Snaffler, Green        -8    4   18.38   14.23     Trans. Completed
--------------------------------------------------------------------------------
                                   211  159 2799.11 2337.55

As usual, the first line of your output should identify you by name, as shown. The second line should include the title "Project UFDA! - Simple Inventory" only. The third line should be blank. The fourth and fifth lines should contain the specified column labels and a row of delimiters to mark the top of the table.Next your output file will contain a table, with a line of output for each inventory item. Each line should contain the item number, and description, the total activity on that item, the number of units of that item in stock, the price and cost of that item and the final message for that item..

After the last line of the table, print a line of delimiters and then display the total number of units sold or bought (careful here..this is an absolut value..), and the total cost and total price of all units of all items that are in stock.

You are not required to use the exact horizontal spacing shown in the example above, but your output must satisfy the following requirements:

Programming Standards:

You'll be expected to observe good programming/documentation standards. All the discussions in class about formatting, structure, and commenting your code should be followed.

Documentation:

Coding: Your submission that receives the highest score will be graded for adherence to these requirements, whether it is your last submission or not. If two or more of your submissions are tied for highest, the earliest of those will be graded. Therefore: implement and comment your C++ source code with these requirements in mind from the beginning rather than planning to clean up and add comments later.

Testing:

Obviously, you should be certain that your program produces the output given above when you use the given input file. However, verifying that your program produces correct results on a single test case does not constitute a satisfactory testing regimen.

At minimum, you should test your program on all the posted input/output examples given along with this specification. The same program that will be used to test your solution generated those input/output examples. You could make up and try additional input files as well; of course, you'll have to determine by hand what the correct output would be.

Pledge:

Each of your submissions to the NAG must be pledged to conform to the Honor Code requirements for this course. Specifically, you must include the following pledge statement in the header comment for your program:

// On my honor:
//
// - I have not discussed the C++ language code in my program with
// anyone other than my instructor or the teaching assistants
// assigned to this course.
//
// - I have not used C++ language code obtained from another student,
// or any other unauthorized source, either modified or unmodified.
//
// - If any C++ language code or documentation used in my program
// was obtained from another source, such as a text book or course
// notes, that has been clearly noted with a proper citation in
// the comments of my program.
//
// - I have not designed this program in such a way as to defeat or
// interfere with the normal operation of the Automated Grader.
 
 

Failure to include this pledge in a submission is a violation of the Honor Code.