The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Java Binary Files(this is a tough one)Level 4 will suffice!

Posted by Brian on April 11, 2001 at 4:31 PM

Introduction
A �inventory� for a bookshop is a (binary) file in which each record describes a book together with the number of copies of the book in stock. A �transactions file� is a (text) file describing a sequence of transactions. A transaction is either an increase (buying in) or a decrease (selling off) in the number of copies of a particular book.

The exercise consists in writing a suite of two programs: one to display an inventory of books, and one to generate a new inventory by processing the transactions in a transactions file.


Structure of inventory
The inventory is a binary file (not a text file). Each record consists of four components, as follows:

String author; // author; exactly 30 characters
String title; // title; exactly 60 characters
String isbn; // isbn; exactly 10 characters
int level; // level; copies in stock (>=0)

An ISBN, which stands for �International Standard Book Number�, is a 10-digit identifying number unique to every publication (of course all copies of a particular book have the same ISBN). Actually, the final �digit� in an ISBN can be the letter �X�, as in 080539057X. The ISBNs occur in the inventory in increasing order. Note that letters come after digits in the standard ordering of characters; for example, 080539057X comes after 0805390578. Levels 1 to 4 need not necessarily exploit the ordering.


Stock display
The program to display the inventory should be called DisplayStock. It will be executed by the command

java DisplayStock BooksFile

The single command line argument is the name of the inventory. A typical output is:

0201403765 Jan Skansholm, Ada 95 from the Beginning, 100
0202535665 M. Ben-Ari, Software Engineering, 25
034565976X Michael Feldman, Program Construction, 12
080539057X M.A. Weiss, Data Structures, 30
0805645782 Ken Arnold, Java for Programmers, 15
0905297568 A. Badone, Chaos Theory, 10

Each record accounts for one line in the output. A line consists of the ISBN, author, title, and stock level, in that order, with commas separating some components as indicated. A sample inventory called BooksFile is provided. Note that your program must work for any inventory of the format described, and not just for the sample provided (e.g., you cannot assume any knowledge of the size of the file).


Stock update (Level 3)
The stock update program (Level 3) reads a sequence of transactions from a transaction file. The transaction file is a text file (not a binary file). Each line in the file refers to a single transaction on a book, a transaction being an increase or decrease in its stock level. Books are identified by ISBN�s. The accompanying diagram depicts a typical transaction file. The data on each line is guaranteed to be valid: every ISBN occurs in the inventory, and there are sufficient copies in stock for any specified reduction in stock. Prepare the transactions file using a text editor, saved in a file called trans.txt. Note that your program must work for any transaction file of the format and content described.


Your program should read the inventory into an array (close the file afterwards), process the transactions in the order in which they appear, and then create an updated inventory. The updated inventory should replace the original one. You can create the new inventory by re-opening the original one for output -- the original contents are deleted automatically. You may use linear search for searching the book table, but you may use binary search if you wish.

The program to update the inventory must be called UpdateStock. It will be executed by the command

java UpdateStock BooksFile trans.txt

0202535665 20
034565976X 15
080539057X �3
0805645782 18
0201403765 25
0201403765 10
0805645782 �5

The first argument is the name of the inventory, and the second is the name of the transaction file. As an example, using the inventory and transaction file described above, the new inventory will be:

0201403765 Jan Skansholm, Ada 95 from the Beginning, 135
0202535665 M. Ben-Ari, Software Engineering, 45
034565976X Michael Feldman, Program Construction, 27
080539057X M.A. Weiss, Data Structures, 27
0805645782 Ken Arnold, Java for Programmers, 28
0905297568 A. Badone, Chaos Theory, 10

Stock update (Level 4)
For Level 4, the diagram on the right depicts a typical transaction file. In this case, it is not guaranteed that the book referred to has an entry in the stock file, or that there are sufficient copies of the book to accommodate a given reduction in stock. Level 4 must cater for such erroneous transactions. In each case, no action should be taken other than to print an error message which includes the ISBN in question. Other details are as for Level 3.
0202535665 20
034565976X 15
080539057X -3
0805645782 18
0202535665 -100
0201403765 25
0201403765 10
0805645782 -5
134565976X 15


Stock update (Level 5)
For Level 5, you may assume that the ISBNs of the transactions are in ascending order, as shown. There may be erroneous transactions as described in Level 4, and they must be handled similarly. For Level 5, you should write the program for updating the inventory without using an array to store the records of the inventory. It is up to you to discover how to do this. Each record of the stock file, and each transaction in the transaction file, need only be read once. The advantage of this approach is that memory demands are greatly reduced, and inventories of unbounded size can be handled. Other details are as for Level 4.

0201403765 25
0201403765 10
0202535665 20
0202535665 -100
034565976X 15
080539057X -3
0805645782 18
0805645782 -5
134565976X 15

The suite will consist of three classes. Class Book encapsulates a book and all operations on it; class DisplayStock contains a main() which displays an inventory; and class UpdateStock contains a main() which updates an inventory. Other classes are not needed or expected, but you are not forbidden to introduce some. Classes may be placed in their own files.

Good luck!!!



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us