| RHS => Allan => Basic Programming => Exercises => BorrowerFile |
Updated:
02/02/2004 23:36 WebMaster: allanhn@rhs.dk |
BorrowerFile
The mission
You are to develop a program, which can manage, control and handle collections
of borrowers at the Fantasy Library by using files. The management of borrowers
has already been implemented in a menu based program. It would be nice to extend
the program with the possibility to write/read the objects to/from
files.
We want a program, which permanently can register and keep
records of borrowers utilizing the previously made BorrowerCollectionVector and BorrowerMenu. In order to be more flexible
and reuseable we design the program to have a class for each functionality.
Due to the requirement specification the following functionality has been identified
for the file management functionality:
NO. |
FUNCTION NAME |
DESCRIPTION |
||
1 |
BorrowerWriteFile | Write the collection of borrowers to a file | ||
2 |
BorrowerReadFile | Read the file into the collection of borrowers | ||
3 |
BorrowerSplitFile | Split the file into two files depending on age | ||
4 |
BorrowerConcatenateFile | Reads two borrower files and concatenates them into one file |
The following diagram shows the structure for the Fantasy Library application divided in three main layers, Model - View - Controller:

Assignment 1: Model: Borrower
To be able to write objects from the Borrower class
to a ObjectOutputStream they need to be serializable.
Start by copying Borrower.java to the folder for
this exercise and change the class-definition to be like this:
public class Borrower implements Serializable {
//same as before
}
Compile the file.
Assignment 2: Controller:
BorrowerWriteFile constructor
Let's look at the class BorrowerWriteFile:
Datafields:
private BorrowerCollectionVector borrowers;
private String fileName;Constructor:
BorrowerWriteFile(BorrowerCollectionVector bcv, String fileName)Methods operating on the data fields:
public void doIt() //performs the write functionality
The class BorrowerWriteFile has
two private datafield, borrowers,
which is a variable (object reference) of the class BorrowerCollectionVector,
and the string fileName.
The constructor must initialize these variables.
Thus the start of the class
resembles the following:
public class BorrowerWriteFile {
//define the datafields, do it yourself
public BorrowerWriteFile (BorrowerCollectionVector bcv, String fileName) {
//initialize the datafields to the parameters
}// here follows the doIt() method
} // BorrowerWriteFile
Let's look at the doIt() method:
The method should do the work of the BorrowerWriteFile class:
Create and declare an ObjectOutputStream using a FileOutputStream as parameter to the constructor.
The FileOutputStream should be created using filename.
Write to the file the number of borrowers.
Traverse the collection of borrowers and write each object to the file.
Finally close the object output stream.
Catches an IOException in case of error with the file writing
Thus the doIt() looks like the following:
public void doIt() {
try {
//declare a ObjectOutputStream oos; do it yourselfint number = borrowers.getNoOfBorrowers();
oos.writeInt(number);
for (int index = 0; index < number; index++) {
Borrower borrower = borrowers.get(index);
oos.writeObject(borrower);
}//for
//close oos; do it yourself
}//try
catch (IOException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}// doIt
Assignment 3: View: BorrowerFilesMenu
Now save the old BorrowerMenu class in a new class,
BorrowerFilesMenu, and extend it with the following
method
- private void writeBorrowersToFile()
Reads in a file name from the keyboard. Declares and construct an object, writeFile of the class BorrowerWriteFile and write the borrowers
to this file by calling the method writeFile.doIt().
Extend the menu with an extra choice handling this method.
Compile!
Assignment 4: Application: BorrowerFilesMenuApp
Save the old BorrowerMenuApp class in a new class BorrowerFilesMenuApp and
change it to use BorrowerFilesMenu instead of BorrowerMenu.
Compile and run !
So far so good ! Now let's look at reading from a file.
Assignment 5: Controller: BorrowerReadFile
To read from a file create a new class BorrowerReadFile resembling
the following:
Datafields:
private BorrowerCollectionVector borrowers;
private String fileName;Constructor:
Methods operating on the data fields:
BorrowerReadFile(String fileName)
public void doIt()
public BorrowerCollectionVector getCollection()
Start by copying BorrowerWriteFile class to a new BorrowerReadFile class.
Adapt the name of the class and constructor.
Adapt the constructor as it now
has a parameter less - as the collection isn't a parameter the constructor
must initialise a new collection object.
Change the doIt() method to do the following:
Create and declare an ObjectInputStream using a FileInputStream as parameter to the constructor.
The FileInputStream should be created using filename.
Reads the number of borrowers from the file.
Traverses the file, reads the borrower objects and add them to the collection.
Finally closes the object input stream.
Catches an IOException in case of error with file reading
Catches a ClassNotFoundException in case of error with the type-casting
Now add the method public BorrowerCollectionVector getCollection()which returns the collection.
Compile!
Assignment 6: View: BorrowerFilesMenu
Extend the class BorrowerFilesMenu with the following method:
- private void readBorrowersFromFile()
Reads in a file name from the keyboard. Declares and construct an object, readFile of
the class BorrowerReadFile and read the borrowers
from this file by calling the method readFile.doIt().
Finally the method should get the collection (readFile.getCollection())
and assign this collection to the variable borrowers.
Extend the menu with an extra choice handling this method.
Compile and try to run the application class.
Assignment 7: Controller: BorrowerSplitFile
To split a file into two files create a new class BorrowerSplitFile resembling
the following:
Datafields:
private BorrowerCollectionVector borrowers;
private String inputFileName;
private String childrenFileName;
private String adultFileName;Constructor:
BorrowerSplitFile(String inputFileName,
String childrenFileName,
String adultFileName)Methods operating on the data fields:
public void doIt()
The doIt method has the following responsibility:
- public void doIt()
Reads the borrowers from the input file, storing the borrowers in a BorrowerCollectionVector,
counts depending on the age the number of children
and the number of adults (if
age < 7 it's a child).
Writes the number of children to the children file followed by all the child-borrowers.
Writes the number of adults to the adult file follwoed by all the adult-borrowers.
Closes all files.
Extend the BorrowerMenuFile to handle this functionality
Compile and run!
Assignment 8: Controller: BorrowerConcatenateFiles
Try to extend your program to concatenate two files into one! Create the class
BorrowerConcatenateFiles with the following characteristica:
Datafields:
private String inFileName1;
private String inFileName2;
private String outFileName;Constructor:
BorrowerSplitFile(String inFile1Name,
String inFile2Name,
String outFileName)Methods operating on the data fields:
public void doIt()
The doIt method has the following responsibility:
Create and declare an ObjectOutputStream using a FileOutputStream as parameter to the constructor.
The FileOutputStream should be created using outFileName.
Create and declare two ObjectInputStream's using two FileInputStream's as parameters to the constructors.
The FileInputStream's should be created using inFile1Name and inFile1Name.
Reads the number of borrowers from the two inputfiles. Writes the sum to the outputfile.
Traverses the two inputfiles, reads the borrower objects and adds them to the outputfile.
Finally closes all three streams.
Throw an IOException in case of errors with file writing
Throw a ClassNotFoundException incase of errors with the type-casting