Question: Using the StockItemManager files in the File IO module, as a model, create a set of programs that: Instantiates and writes five Book objects to

Using the StockItemManager files in the File IO module, as a model, create a set of programs that: Instantiates and writes five Book objects to a file. (Hardcode the creation of Book objects with their attribute values.) Reads the original data from the file and displays it Updates the title for one Book Updates the price for another Book Reads the modified data from the updated file and displays it. Your Book class must have the following attributes: ISBN title yearPublished price It must also have set and get methods for these attributes, plus a toString method. The most difficult part of using Random Access Files is handling Strings. The variable lengths of Strings cause problems. Random Access Files require that every record have the same length. This enables a program to move the file pointer to a particular location. When a program wants to move the pointer to record five, lets say, it has to know how many bytes record five is from the beginning of the file. If all the records have different lengths, this would not be possible, and the program would put the file pointer in the wrong place. To relate this problem to this project: if one book has the title Java and another has the title Introduction to Java, the first title has 16 bytes (Java Strings use Unicode two-byte characters) and the second has 38 bytes. We need to make Strings occupy the same amount of space in a file, so we must decide on a standard length for String fields like title. Lets say we determine that titles should have 48 bytes. Then we would right pad the values of titles that have fewer than 48 bytes and truncate those that have more. The sample program demonstrates this with its setCorrectLength method.

Using the StockItemManager files in the File IO module, as a model, create a set of programs that: Instantiates and writes five Book objects to a file. (Hardcode the creation of Book objects with their attribute values.) Reads the original data from the file and displays it Updates the title for one Book Updates the price for another Book Reads the modified data from the updated file and displays it. Your Book class must have the following attributes: ISBN title yearPublished price It must also have set and get methods for these attributes, plus a toString method. The most difficult part of using Random Access Files is handling Strings. The variable lengths of Strings cause problems. Random Access Files require that every record have the same length. This enables a program to move the file pointer to a particular location. When a program wants to move the pointer to record five, lets say, it has to know how many bytes record five is from the beginning of the file. If all the records have different lengths, this would not be possible, and the program would put the file pointer in the wrong place. To relate this problem to this project: if one book has the title Java and another has the title Introduction to Java, the first title has 16 bytes (Java Strings use Unicode two-byte characters) and the second has 38 bytes. We need to make Strings occupy the same amount of space in a file, so we must decide on a standard length for String fields like title. Lets say we determine that titles should have 48 bytes. Then we would right pad the values of titles that have fewer than 48 bytes and truncate those that have more. The sample program demonstrates this with its setCorrectLength method.

StockItem.java - https://www.dropbox.com/s/6tcdt18rdyuwu7j/StockItem.java StockItemManager.java - https://www.dropbox.com/s/w0x09xmtmc0sbkz/StockItemManager.java StockFile.dat - https://www.dropbox.com/s/o95pcjd434aubbd/StockFile.dat

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!