Serialize


 Serialization

OpenFileTools.java

Bunny.java Serializable class
SerializeFileExample.java serialize just one
SerializeMultipleBunnys.java serialize multiple
SerializeList.java serialize List as a single object

Read chapter 15


Classwork

(hint: a lot of this is just using the right incantations for the libraries we are using, so it is probably simpler to cannibalize chunks of the example code but make them work for your classes, instead of starting from scratch)

Create a class HCCClass that stores a program, course number, number of credits, and number of times student has failed. Include a method fail() that increases the number of fails and only include this number in the toString if it is positive.

Make HCCClass Serializable, but make the number of fails transient (the school keeps track but this does not get saved on the permanent transcript).

In a class with a main called FirstMain, create a List and put in it at least 5 HCCClasses; have the student fail one of them 3 times. Print the list.

Using a JFileChooser, let the user choose a location and file name to save the List to.

Since List is also serializable, you can serialize the whole list with one call to writeObject, and in that case you don't need an extra null

Add another class SecondMain with a main (in the same package).  In this main, use a JFileChooser to let the user choose a file of serialized HCCClasses  to read into an List and then print them.  Test that this works on the file created by FirstMain.

(note: you can't check instanceof on List<HCCClass> but you can check on List; technically we should first cast the object we get to a general list, and then check its contents to see if it has classes, but for the purposes of this assignment you can cast straight to List<HCCClass>)


[EC] Add two more mains; this time, instead of serializing and deserializing the whole list as one object, we will loop and do them one by one. The third main creates a list of HCCClasses and serializes the HCCClasses one by one (all to the same file) but does not save any class the student failed more than 3 times. It also writes a null to the end of the file to support looping when you read it later. The fourth main reads them from the file one by one, putting them in a list, looping until it reads the null. Then print the list.

 

[EC] To create the original List of HCCClasses, instead of hardcoding them, create a CSV file of HCCClasses called transcript.csv (at the top of your project directory) and read this file into FirstMain.