Arrays, Functions, and File I/O (checkout)
CS 161 - Spring 2008

Due Monday, April 28, at 10:00 pm.
This is an extra credit assignment.

Goals
- to use files to get information
- to use arrays to hold and access information
- to write functions and use parameters effectively
- to write your own class (optional)

Overview
For this assignment, you will write a program to simulate a grocery store checkout system. Information about store items (item code, name, cost per unit, and whether the item is sold by pounds or quantity) will be stored in a file. Standard input will contain the name of that file followed by lines representing items to be purchased. Your program will print a report of the purchased items and total cost.

Checkout Description
The first line of standard input will contain a file name. You are to open the file and read the contents to obtain the information on items for sale. The file will contain one line per item. The line will contain four values. The values are 1: item number (an iteger), 2: item name (a string), 3: item price (a double), and 4: item type (a string, either pounds or each). You may assume the item information file is properly formatted.

There will be no more than 1000 items in the file. If more are encountered, print an error message and terminate the program.

The item number can be any positive integer. Hint: Do NOT use them as an index since they have a very large range. The item name is a string of non-space characters. The item price is a positive real number. The item type is a string. It is either pounds, meaning the item is sold by the pound, or it is each, meaning the item is sold by units. Note that the lines in the file are not sorted in any way.

You should read the four values for each item into four arrays to hold the item data in memory. This is called a parallel array implementation. Each of the four values associated with an item are at the same index in the respective arrays.

An alternative implementation would be to create a class to define objects that hold each of the four values associated with an item, and then create an array of those objects. While nicer from a design perspective, this is more work. So only try this if you want a challenge. I will award an additional 20% extra credit if you do it this way.

After the item file name, the standard input will contain purchase information. Each line will contain an item number and a quantity or weight as appropriate for the item. You should write out a line of information for the purchase after reading each input line. To compute the appropriate output line, you will need to look up the item number in the item number array and consult the other arrays with the same index to obtain information about the item. If the item number is not in your array, print a message and skip the item. The input should be terminated when the Scanner reports no next item code (end-of-file).

After all input has been processed, write out a line with the total purchase price.

When you are done with the program, submit it as the checkout assignment.
 

Sample File

22 apple 0.69 pounds
10 gallonMilk 2.99 each

Sample Input and Output

Please enter file name: itemfile
Please enter purchase information:
22 3.1
apple 3.1 lb @ 0.69/lb = $2.14
10 2
gallonMilk 2 @ 2.99 each = $5.98
5 3
Unknown item code: 5

Total = $8.12