Lab 13.5.1.2 Reading Customer Objects from a File
Estimated Time: 20 minutes
Learning objectives:
• In this lab activity, the student will read the Customer objects saved in the previous lab
using the ObjectInputStream.
Description/Business Rules:
• Create a class called CustomerFileReader to read customer objects from the file
“customer.dat”. The CustomerFileReader will have a method called readCustomers().
This method reads the customers stored in the file.
File Management:
Open BlueJ. Click on Project from the BlueJ main menu and select New. In the New
Project window and in the Look in: list box select c:\. Now, double click the javacourse
folder listed in the text window and a different New Project window opens with javacourse
in the Look in: list box. Now, double click the chap13 folder listed in the text window and a
different New Project window opens with chap13 in the Look in: list box. Type lab13.5.1.2
in the File name text box and click on Create to create a lab13.5.1.2 subfolder in the
chap13 folder. Import the JBANK classes from lab13.5.1.1.
Task:
Step 1 Reading Customer objects from the “c:\\customer.dat” file
a. To read objects from a file make use of ObjectInputStream. For this purpose the
Customer class and Account class should implement the interface Serializable. This is
already defined from the previous lab.
b. Using BlueJ create a class called CustomerFileReader. Define attributes to store
references to File, ObjectInputStream and FileInputStream objects. Implement a method
called readCustomers() that returns an array of Customer objects. This method
should throw an Exception if the file does not exist. In this method create a File object
called objectFile for the “customer.dat” file. Use File class methods to check if the
“customer.dat” file already exists. Read the Customer objects from the file. If the file
does not exist, throw an instance of the Exception object, setting the message of the
exception to display “ File does not exist”. You can do this with the following syntax:
throw new Exception(objectFile.getName() + " File does not exist");
(Note: If you created the file customer.dat in a different directory or under a different
name, you should modify your code to reflect this.)
c. The class holds a reference to an array of customers. The maximum number of
customers can be set using:
Customer[] customers = new Customer[Bank.maxNumOfCustomers];
d. In
a
try-catch block, read the Customer objects from the file. To read the Objects,
create an instance of ObjectInputStream. The constructor of the ObjectInputStream takes
an argument of type FileInputStream. Create an instance of FileInputStream called
fileInputStream and use the objectFile as the argument to the constructor. Use
FileInputStream object as an argument to create an instance of ObjectInputStream called
objectInputStream. Use try and catch blocks to handle the IOException and
ClassNotFoundException.
1 - 3
Fundamentals of Java Programming Lab 13.5.1.2
Copyright
2003, Cisco Systems, Inc.
e. In a loop, use the readObject() method of the ObjectInputStream and read the
Customer objects into a Customer array. The read method returns a reference to an
instance of the Object class. Include a cast expression to cast the object to that of a
Customer class. In the catch block (if the no objects read) rethrow the IOException.
The last statement of this method must return a reference to the Customer array. Do not
place this in the try block. Place it after the definition of the catch block or in the
finally block.
f. In the Teller class main method of the class, create a reference to a Customer[] array,
setting the size to the maxNumberOfCustomers static variable of the Bank class.
Create an instance of the CustomerFileReader class identified by custFileReader.
Call the readCustomers() method of the CustomerFileReader object referenced by
custFileReader. This method returns a reference to the Customer array object.
Assign this value to the Customer array object.
g. Include
a
for loop to traverse through the array. Keep in mind that you may not have
created the maximum number of customers. Be sure to include some tests that end the
loop if the Customer array element is null. The System.out.println() statement can
print each Customer object. If you will recall the Customer class overrides the
toString() method.
Step 2 Documentation
Using the Document “How to use UMLTestTool”, follow the instructions to verify your JBANK
classes match the JBANK UML diagram shown below.
a. Write javadoc comments to the classes introduced in this lab.
2 - 3
Fundamentals of Java Programming Lab 13.5.1.2
Copyright
2003, Cisco Systems, Inc.
3 - 3
Fundamentals of Java Programming Lab 13.5.1.2
Copyright
2003, Cisco Systems, Inc.