XML · Exception Handling · Validate User Input · ShopV5.0 · DVD3.0

Developing Shop V5.0

In this practical, you will create a new project called ShopV5.0, based on ShopV4.0. You will update the Driver, Store and Product classes (linked below) to enable the user to save the products to an XML file and also reload them (see Figure 1 for the updated menu system).

Figure 1: Menu System for ShopV5.0

Menu Items 9 and 10: Saving and Loading Products

Create a new project called ShopV5.0.

Copy the following classes into the project:

Setting up the Component for Serializing

Download the following XStream jar file from:

UPDATE: use XSTREAM jar file instead

In IntelliJ, on the ShopV5.0 project, right click and select New, followed by Directory. Call the new directory lib. Drag the xstream jar file already downloaded into the lib folder.

Your workspace should look like this:

From File menu, select Project Structure. Click on Libraries. To add a library to your build path, click on the green + :

Select Java and locate your library…click OK (a few times!).

Updating Store Class to serialize and deserialize products

In the Store class, create these two new methods:

    public void load() throws Exception
        XStream xstream = new XStream(new DomDriver());
        ObjectInputStream is = xstream.createObjectInputStream(new FileReader("products.xml"));
        products = (ArrayList<Product>) is.readObject();

    public void save() throws Exception
        XStream xstream = new XStream(new DomDriver());
        ObjectOutputStream out = xstream.createObjectOutputStream(new FileWriter("products.xml"));

You will notice that they don't compile; they are missing some needed packages. Import the following packages at the top of the store class:

import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

Adding load and save functionality to the menu

In the Driver class, add options 9 and 10 to the printed menu:

Figure 4: Menu System code for Shop V5.0

In the Driver class, make the following changes to implement case 9 and 10:

Figure 5: Code for case 9 and 10

Testing the load and store

You should be in a position now to test.

Start your app and create two products e.g.

Please enter the product description: 24 inch monitor
Please enter the product code: 3423
Please enter the product cost: 129.99
Is this product in your current line (y/n): y

Please enter the product description: 14 inch monitor
Please enter the product code: 2322
Please enter the product cost: 109.99
Is this product in your current line (y/n): y

Now try option 9 to save your products. Note: if you are using java 9 you are probably getting a series of red warning messages...the products will be saved, but they will still produce the warnings. The next step will show you how to suppress these warnings.
You should now see a products.xml file appearing in the root of your project.

Open this file and it should contain something similar to this:

      <productName>24 inch monitor</productName>
      <productName>14 inch monitor</productName>

Exit your application and run it again.

Test option 10 and make sure that the saved products are loaded back into your products ArrayList correctly.

Java 9 and XStream

This step ONLY applies if you are using JDK9. If you are using JDK8 or lower, you can move onto the next step. Chances are the majority of you are using JDK8.

If you are using JDK9, you are more than likely getting the following warning when saving your products:

Warning when saving XML file with JDK9

However, your products.xml file will still be successfully saved; it is just a warning.

Getting rid of the warning

We can get rid of the warning by passing suppression messages to the VM when we run our application. To do this:

  • From the Run menu, select Edit Configuratons...*

  • In the VM Options box, paste in the following VM arguments:

--add-opens java.base/java.util=ALL-UNNAMED 
--add-opens java.base/java.lang.reflect=ALL-UNNAMED 
--add-opens java.base/java.text=ALL-UNNAMED 
--add-opens java.desktop/java.awt.font=ALL-UNNAMED
  • Click the OK button.


Rerun the app and try the save again...the warning message should now be suppresed.

A note on XStream and Java

Java 9 was released in September 2017. However, the latest version of XStream (1.4.10) was released in May 2017, before the latest Java changes. When a new version of XStream is released, we probably won't have to pass these parameters to the VM.


The solution to the ShopV5.0 exercise is here.

The solution to DVD phase 3 is here.