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:
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.
In this practical, you will continue working on Shop V5.0.
In the Driver class, put a try and catch block around each potential read of data that could throw an exception (nextLine() and next() are ok).
Revisit your lecture notes on this topic if you need guidance.
Run your program and test your user input to ensure that your exception handling is correctly implemented e.g. enter String data when a double or int is expected.
In this practical, you will create a new project and copy in the code from DVDLibraryV2.0. You will then extend the code to allow the user to save the DVDs to an XML file and to load the DVDs from an XML file.
Create a new project called DVDLibraryV3.0.
In your Windows Explorer / Mac Finder, copy the src java files from DVDLibraryV2.0 to the src folder in your new project, DVDLibraryV3.0.
NOTE: a copy of the completed DVDLibraryV2.0 is available here.
Download the following XStream jar file and incorporate it into your DVDLibraryV3.0 project:
In the Library class:
add a load() method that throws an Exception. This method should read the contents of the dvd.xml file into the ArrayList of DVDs.
add a save() method that throws an Exception. This method should write the contents of the ArrayList of DVDs to the dvd.xml file.
Add a fifth option to the menu: 5) Save DVDs to dvds.xml.
Add a case 5 to the switch statement. Then, within a try and catch block, call the save() method you wrote in the Library class.
Add a sixth option to the menu: 6) Load DVDs from dvds.xml.
Add a case 6 to the switch statement. Then, within a try and catch block, call the load() method you wrote in the Library class.
Run the project.
Test option 5 and make sure that your DVDs are stored to an XML file.
You should have a new XML file that looks something like this:
<object-stream>
<list>
<DVD>
<title>The Matrix</title>
</DVD>
<DVD>
<title>Sicario</title>
</DVD>
<DVD>
<title>The Grinch</title>
</DVD>
</list>
</object-stream>
Exit your application and run it again.
Each time you read in an int in the Driver class, add a try and catch so that your program won't crash if you enter a non-numeric value.