Purpose:
The purpose of this tutorial is to give you practice saving data to
disk and reading data back from disk. This tutorial is based on chapter
10 of the course notes and in particular section 10.5. In this tutorial
we will use a text-based user interface which allows the user to save
and load object. We will use of a DVD collection example. The ideas a similar to the Car and Autoshow example
presented in section 10.5, use that as a template for the code you need
to write.
Instructions:
Follow the steps below one by one until you are done and then
demonstrate to the T.A. that your code works.
- Open the demonstration classes DVD.java,
DVDCollection.java and DVDUI.java.
Examine the code.
- Compile the code and run the program (i.e., DVDUI.java).
- Modify the showMainMenu() method in the DVDUI class so that it asks the user "Are you sure you want to quit the
application ? (Y/N)" and then waits for the user to type in
either "Y", "y", "N" or "n". If "Y" or "y" is entered, then the program
should quit, otherwise it should continue in the main loop of the showMainMenu() method.
- Create a method in the
DVD class that takes a PrintWriter
object and then saves a single DVD to the given file (i.e., the PrintWriter)
on one line ... something like this:
Rush Hour 2,2001,118. Create an
additional method in the DVDCollection class that also takes a PrintWriter
object and then saves ALL of the dvd's to the given file
with 1 dvd per line in the file, making use of the previous method you
wrote in the DVD class.
- Write a saveData()
method in the DVDUI class that makes use of the
methods you just wrote to save the model (i.e., the DVDCollection) to a file called dvds.txt when the user decides to quit the
program. The name of the file should be hard-coded or fixed in the
program, do NOT prompt the user for the filename. You will need to
catch any necessary exceptions. Don't forget to close the file.
- Try starting the program, entering a few dvds,
quitting. Then look for the file
dvds.txt on you disk in the same directory where your code is
and then open the dvds.txt file with
Windows Notepad to confirm that everything was saved properly. Restart
the program and notice that none of the dvds you entered are there when
you try listing them. This is because we have not written the code to
read them back in again.
- Write a public static method in the DVD class called: parseFrom(String
s) which returns a new DVD object based on the string data
supplied. Assume that the string is in the following format: Rush Hour 2,2001,118 where the title, year
and duration are all separated by commas, just like in your file. Your
code should make use of the String
method split() to break the data
string into tokens. For example, the following code:
String s = "Star Wars,1977,83";
String[] tokens = s.split(",");
for(int i=0; i<tokens.length; i++) {
System.out.println("token = " + tokens[i]);
}
Will produce the following output:
token = Star Wars
token = 1977
token = 83
Your method should create and return a new
DVD object with the
data read in from the string supplied. Write the following main method
in your DVD class just to test if your parse
method works:
public static void main(String args[]) {
System.out.println(DVD.parseFrom("Rush
Hour 2,2001,118"));
}
- Write a method in the
DVDCollection class
called loadFrom() which takes a BufferedReader as a parameter and then repeatedly
loads dvds from the file and
places them into the collection. Make sure to handle any exceptions
with an appropriate message. The method should load until the readLine() method returns null. It should make use of the parseFrom(String s) method that you just
wrote.
- Modify the showMainMenu()
method so that it calls a loadData()
method before it starts the main interface loop. This method should
open the dvds.txt file and then call the loadData()
method you wrote in the DVDCollection class which loads the dvds from
the dvds.txt file and stores them in
the model. your code should now load up any saved dvds upon startup.
- Re-test everything by running the program,
entering a dvd, quitting and then re-starting to see if the added dvd was there. Also, ensure that you
test the delete feature to make sure that dvds are properly deleted
from the file as well.
- Demonstrate to the T.A. that your code works fine
in order to get credit for the tutorial.
Extra Fun:
If you have
extra time, try the following:
Add another attribute to the DVD model class such as price ... indicating the price you paid
for the dvd (i.e., a float) and then modify all of the necessary
code to allow this new information to be added, displayed in the list,
saved and loaded.
Try altering the saving and loading code to save/load
the data as binary or using an
ObjectStream and notice
the difference in file sizes and readability with notepad.