Lab 11

A data file contains the current inventory of a small gift shop. Each record in the file corresponds to a specific item and includes the stock number, a description of the item, the quantity on hand, and the unit price. Write a program to read this data file and generate a neatly formatted report listing the stock number, description, and quantity on hand for each item in the inventory. Print an asterisk at the end of each line corresponding to an item for which the quantity on hand is 10 or less.

Output Specification

The output is in the form of a table which consists of two lines of headings followed by the list of inventory items. Items in short supply are marked by an asterisk at the end of the line. Here is the full report generated by a sample run:

Stock #  Description                          Qty
-------  -----------------------------------  ---
PC90394  10-pack Postcards                     24
TB00034  20-inch Teddy Bear                     1*
LH93094  4-pack Lace Hankies                   12
AF00938  Angel Figurine                         1*
CD89388  Chocolate Fudge                       24
CD83489  Caramel Chews                         11
MS83988  Vermont Maple Syrup                   15
VS03929  10-inch Ornamental Vase               10*
HP12902  Stuffed Hippo                         23

The stock number column is 7 characters wide, the description column is 35 characters wide and the quantity column is 3 characters wide (plus room for the asterisk when necessary). The space between two adjacent columns is 2 characters wide.

Input Specification

The file contains an unknown number of records. Each record consists of the stock number, the quantity on hand, the price, and the description of the item. These values are whitespace delimited with exactly one space between the price and the description. The description may contain space characters. Here are the first two records in the file used to generate the sample report shown above:

PC90394  24  2.49 10-pack Postcards
TB00034   1 10.95 20-inch Teddy Bear

I've provided a sample file for you. Just right-click on this sample file link, and select the "Save Link As..." option in Firefox or the "Save Target As..." option in Internet Explorer and save the file to your project folder. Of course, you could also create your own data file if you want.

This file format causes one minor problem. To scan a token that contains spaces, you must use the nextLine method of the scanner class which scans everything from the current position to the end of the line. When the description is scanned, the current position is the space character immediately following the price. Consequently, this space character will be the first character (the character at index 0) in the string returned by the nextLine method. However, this space character should not be part of the description. The description is a substring of the value returned by the nextLine method starting at the second character (the character at index 1):


Process Specification

Obviously, you'll need to use a selection structure in order to add an asterisk to the end of each line in the report for which the item is in short supply.

In the code that responds to the user clicking the Open button, there are a number of things that could go wrong. In your application, write that code as the try clause of try-catch statement:

if (source.equals(cmdOpen))
        code to respond to clicking the Open button
    catch (Exception e)
        generate error message dialog

Notice that the catch clause catches a generic Exception object which means it will catch any Exception thrown in the try clause. Since there are several different types of exceptions that might arise, you'll need to invoke the exception's toString method to display an appropriate error message. Finally, that error message is to be displayed in a message dialog. For example, if the file selected by the user doesn't exist, your program should display a message dialog like this:


If the file exists but contains an error, a different error message will be generated. For example, the following message dialog was generated when the data file was missing a price:


Interface Specification

This application should have a graphical user interface with three buttons and a text area. The Open button should allow the user to open the input file using a JFileChooser object. The Clear button should allow the user to clear the text area. The Exit button should exit the application. The report should be displayed in the text area. You'll need to use a mono-spaced font in the text area in order for the columns to line up correctly. The text area should be wide enough to display the full width of the report. If the report is too long to fit in the text area, a vertical scroll bar should be used to allow the user to scroll through the report. There should never be a horizontal scroll bar. After processing a file, the top of the report should be visible in the text area (i.e., the column headings should be visible).

With some resizing, an interface using a FlowLayout should look like this:

Initial GUI

After opening an inventory file and generating the report, the application should look as shown below. Note that in addition to displaying the report, the actionPerformed method should also change the title of the application window so that it indicates the name of the file that generated the report.

Application with Report

Clicking the "Clear" button should restore the interface to its initial state.

Bonus Points

I'll give you 5 bonus points if your application uses a GridBagLayout and looks like the illustration below without any resizing necessary.

Alternate Interface

Submitting Your Work

Submit your Lab11.java application as an attachment to an email message whose subject is "Lab11".