Lab 7: File IO #2

1. Here is another text file IO assignment, student GPA.

2. Complete Project #10, at back of Chapter 10. Read pp. 626 to 632 to see how this might be accomplished. Your main class should be called "PersonDB".

Upon starting, the program should read the data file, "persons.dat".

Your program maintains a database of Persons, stored as an array of Persons. For purposes of this discussion, call the array myPersons. The file persons.dat will be used to store the contents of the database (i.e., the contents of myPersons across separate sessions of PersonDB.) Because the first time PersonDB is run there is not yet a database, it stands to reason that there is not yet persons.dat file. So, if when PersonDB runs there is no persons.dat file, the program simply starts with an empty myPersons. On subsequent runs, when there is a persons.dat file, myPersons can be initialized with the contents of the file.

I will test your program by running it several times. The first time I run it, there will be no persons.dat file. Consequently, I will add a few persons to the database, perhaps deleting and finding, then quit. This should create a binary persons.dat file. Then I will restart your program. I should see a database that is equivalent to what it was just before I quit the first time. I may repeat this several times.

The file persons.dat should be created via a call to writeObject, as explained in chapter 10. Do not create the file until you are ready to use writeObject, which will be when you are effecting the user's "quit" request.

You will want at least two classes: a driver class (PersonDB), and a database class (you can use any name--I used MyDatabase.) The driver class repeatedly prompts the user with the question: "delete, add, find, quit?" The user will respond with one of four forms: "delete name", "add name age", "find name", "quit". Upon quitting, the contents of the array should be dumped into persons.dat (thus overriding the original contents, if any.) The driver class parses the user's input and invokes an appropriate method of the database class. You may want to use a StringTokenizer obect to parse the input string, but there are other ways.

You can probably see that your database class will need methods for each of the user commands. The four declarations might be something like:

public boolean delete(String name) // Return true if successful
public void add(String name, int age)
public Person find(String name) 
public Person[] findAll(int youngestAge, int oldestAge) // Return array of all Persons whose age is in specified range

You'll need other methods, too. For example, you'll probably want a method such as loadDB and saveDB for use when you start (and there is a persons.dat file) and quit the application. These methods set MyDatabase's myPersons to the contents of persons.dat, and dump the content of the same array to that file, respectively. You may have an instance variable that keeps track of the number of Persons in myPersons. If so, you will need to recalculate that value after loadDB. Count the number of non-null elements in myPersons.