Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Assignment : Data that is written to a file is commonly structured as fields and records. In file terminology, a field is an individual

Java Assignment:

Data that is written to a file is commonly structured as fields and records. In file terminology, a field is an individual piece of data, such as a persons name or telephone number. A record is a collection of fields pertaining to a single item. For example, a record might consist of a specific persons name, age, address, and telephone number.

Quite often you can save the contents of an object as a record in a file. You do this by writing each of the objects fields to the file, one after the other. When you have saved all of the objects fields, a complete record has been written. When the fields from multiple objects have been saved, then multiple records have been written to the file.

Random access files are particularly useful for storing and retrieving records. However, the sizes of the items stored in a random access file must be known in order to calculate the position of a specific item. Records that are stored in a random access file must be the same size and all records must have the same length. This means that the size of a record cannot change. (Why?)

In Java, the sizes of the primitive data types are well documented and guaranteed to be the same on all systems. If an objects fields are all of the primitive data types, you can easily calculate the size of the record: it will be the sum of the sizes of all the fields. You can handle the case of a character string by making sure that a String field is always written as the same number of characters. If the string is too long, cut it off; if too short, pad with blanks.

NOTE: Remember that when Java writes a String to a file, it also precedes it with a 2-byte length value and expects to find that value when reading a String. This means that your record length will be increased by 2 bytes for each string in the record.

Roster File The purpose of this program is to accept information from the user and, using that info, create a file which contains information about fantasy game players. It will in essence be a database of football or basketball or baseball players. Each players data will make up a single record in the file. Each record will start with an integer ID, a name field (first and last), a team name, an integer skill level (0-99) and last, the date that the player was drafted. The operation of this program will be as follows:

a. Create a direct access file.

i. Ask the user for a file location and filename.

ii. Create the File object

iii. Open the file

iv. If the file does not already exist Initialize it to recognizable characters

b. Ask the user for a command.

c. Read the user command and proceed accordingly;

i.new make a new record

ii. old retrieve and display an existing record

iii. end terminate the program.

d. If the command was new

i. Ask the user for a positive integer ID number between 1 and 20, read the ID

ii. Ask the user for the players name, read the name

iii. Ask the user for the players team name, read the team name

iv. Ask the user for the players skill level (integer between 0 & 99), read the skill level

v. Ask the user for todays date, read the date

vi. Create a single string to contain all the data for one record. The field lengths will be as follows:

1. ID 5 characters

2. Name 26 characters

3. Team name 26 characters

4. Skill level 5 characters

5. Draft date 9 characters (ex: 25Jun2014)

vii. Convert the ID and skill level to Strings (5 chars)

viii. Make sure that the name, team name and date fields are the correct length. If a field is too short, pad it with blanks; if it is too long, cut it off!

ix. Move all fields to the single String created in step vi. above. This will result in a String of length 71

x. Write the record into the file, at the record location indicated by the ID.

xi. Go back to step b.

e. If the command was old

i. Ask the user for an ID, read the ID

ii. Use the ID to index into the file and retrieve the record

iii. Display the record to the user, formatted for readability.

iv. Go back to step b.

f. If the command was end

i. Close the file

ii. End the program.

Dont forget that the inventory records must all be the same length. Otherwise, the old command wont be able to find the record that the user wants to see. This will make a string that is 71 characters long (5 + 26 + 26 + 5 + 9) and will be written as an inventory record of length 73 bytes. You will write 71 bytes but Java will add a 2-byte length in front of the string, which you must know about when you are retrieving an old record. When you first create the file, you will create room for 20 inventory records, of length 73 each. This space must be created when the file is opened and then as each record is created by the user, it will be placed into a space that is already laid out.

The ID number will be an indication of where in the file the specific inventory record is stored. Thus, if the user asks for the record with ID number 13, the program should go to the 13th record in the file. The ID number will of course, also be stored inside the record itself in order to make it simple to display it.

Since the user will be entering integer values (ID number and skill level) there is the chance that the user will go temporarily insane and enter something that is not an integer. (Ex: 3.24 or XYZ). Your program will throw an exception called NumberFormatException. Your code should catch this exception, notify the user of the error and try again to get a valid value. Thus there should be no reason for this program ever to crash.

You can get the values from the user by either reading from the console, using a Scanner object or by using the MessageDialog and the InputDialog.

Testing:

When testing this program, please try invalid values as follows:

a. non-integer for the ID

b. negative number for the ID

c. ID larger than 20

d. non-integer for the skill level

e. name shorter than 26 characters

f. name longer than 26 characters

g. team name shorter than 26 characters

h. team name longer than 26 characters

Testing should include:

a. Attempt to write valid records with IDs of 1, 5, 4, 2 in that order.

b. Attempt to retrieve each record after it is written

c. Attempt to retrieve all records in the order - IDs of 1, 5, 4, 2

d. Attempt to retrieve record 3.

e. Attempt to retrieve all records in the file & display to user.

f. Close the program, restart it and find the data file unchanged.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

Students also viewed these Databases questions