XML · Exception Handling · Validate User Input · ShopV5.0 · DVD3.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).
Create a new project called ShopV5.0.
Copy the following classes into the project:
Download the following XStream jar file from:
UPDATE: use XSTREAM 1.4.11.1 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!).
In the Store class, create these two new methods:
@SuppressWarnings("unchecked")
public void load() throws Exception
{
XStream xstream = new XStream(new DomDriver());
ObjectInputStream is = xstream.createObjectInputStream(new FileReader("products.xml"));
products = (ArrayList<Product>) is.readObject();
is.close();
}
public void save() throws Exception
{
XStream xstream = new XStream(new DomDriver());
ObjectOutputStream out = xstream.createObjectOutputStream(new FileWriter("products.xml"));
out.writeObject(products);
out.close();
}
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;
In the Driver class, add options 9 and 10 to the printed menu:
In the Driver class, make the following changes to implement case 9 and 10:
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:
<object-stream>
<list>
<Product>
<productName>24 inch monitor</productName>
<productCode>3423</productCode>
<unitCost>129.99</unitCost>
<inCurrentProductLine>true</inCurrentProductLine>
</Product>
<Product>
<productName>14 inch monitor</productName>
<productCode>2322</productCode>
<unitCost>109.99</unitCost>
<inCurrentProductLine>true</inCurrentProductLine>
</Product>
</list>
</object-stream>
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.
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:
However, your products.xml file will still be successfully saved; it is just a 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
Rerun the app and try the save again...the warning message should now be suppresed.
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.