First, create a lab08 directory:
$ cd cs190m $ mkdir lab08 $ cd lab08
Then, download Person.java and Lab08.java into the folder you just created.
Today's lab involves managing a list of people (a roster). Parts of the roster management have already been written for you; in particular, adding and removing of people (represented by the Person class). However, these rosters are not particularly useful if you cannot write them to more persistent storage than RAM. Your task is to implement saving/loading of rosters to disk.
There are several classes that you'll find useful: Scanner, PrintWriter, and File. You should already be familiar with Scanner; it is often used for reading input from the keyboard. However, in this case, it will be used to read input from a file instead. On the other hand, PrintWriter will be used for writing to a file. In both cases, a File object can be used to initialize objects of the respective types. There are three tasks in total.
Person FirstName Bob LastName Marley EndPerson
The createPerson method is given a Scanner object. It should:
Note: You cannot assume that the FirstName token will appear before the LastName token; obviously, the writeTo method will always save the first name first, but we want to allow the case that someone edits the file and accidently saves the fields backwards.
You'll notice in the Lab08 class that it is simply a large main method. Inside, there's a large switch statement; there are two cases which concern you, which are 5 and 6 (saving and loading). Part of the code for reading and writing is provided for you; however, you must fill in the blanks. That is, you should fill in the declaration for the PrintWriter and Scanner objects, as well as add exception handling and closing of the objects upon completion of the task.
Note: Closing PrintWriter and Scanner objects is not so simple as putting a call to close in the try block. In the event that an exception is thrown, the resource will not necessarily close; what Java language construct allows you to ensure that the resource is always closed?
The Lab08.java file can be used for testing your program. The main method is a simple, command-line menu, which should be self-explanatory. Sample output is provided below:
1. Add a person to the roster 2. Remove a person from the roster 3. Clear the roster 4. Print the roster 5. Save the roster 6. Load a roster 7. Quit Enter your choice: 1 Creating a new user... Enter the first name: Bob Enter the last name: Marley 1. Add a person to the roster 2. Remove a person from the roster 3. Clear the roster 4. Print the roster 5. Save the roster 6. Load a roster 7. Quit Enter your choice: 4 0. Person: Name-Bob Marley 1. Add a person to the roster 2. Remove a person from the roster 3. Clear the roster 4. Print the roster 5. Save the roster 6. Load a roster 7. Quit Enter your choice: 5 Enter a file to save to: test.txt 1. Add a person to the roster 2. Remove a person from the roster 3. Clear the roster 4. Print the roster 5. Save the roster 6. Load a roster 7. Quit Enter your choice: 3 Roster cleared. 1. Add a person to the roster 2. Remove a person from the roster 3. Clear the roster 4. Print the roster 5. Save the roster 6. Load a roster 7. Quit Enter your choice: 4 1. Add a person to the roster 2. Remove a person from the roster 3. Clear the roster 4. Print the roster 5. Save the roster 6. Load a roster 7. Quit Enter your choice: 6 Enter a roster to load: test.txt 1 entries loaded. 1. Add a person to the roster 2. Remove a person from the roster 3. Clear the roster 4. Print the roster 5. Save the roster 6. Load a roster 7. Quit Enter your choice: 4 0. Person: Name-Bob Marley 1. Add a person to the roster 2. Remove a person from the roster 3. Clear the roster 4. Print the roster 5. Save the roster 6. Load a roster 7. Quit Enter your choice: 7 Goodbye!
To turn in your lab:
$ cd ~/cs190m/lab08 $ turnin -v -c cs190m -p lab08 *.java
Many of you will probably run across problems while programming at some point during a lab. If that's the case, here are the resources you should use, in order:
Lab created by: Daniel Tang