Question
Products list: Java Murach's Beginning Java Murach's Java Servlets and JSP $49.50 $49.50 sps XML Tester has been added to the XML document Products list:
Products list: Java Murach's Beginning Java Murach's Java Servlets and JSP $49.50 $49.50 sps XML Tester has been added to the XML document Products list: $49.50 $49.50 $77.77 java sps test Murach's Beginning Java Murach' s Java Servlets and JSP XML Tester XML Tester has been deleted from the XML document Products list: java Murach's Beginning Java jsps Murach' s Java Servlets and JSP $49.50 $49.50
Stuck on this project. Appears to be pretty simple in just getting xml to work with java
Insturctions:
Add code to the readProducts method that reads an XML document from the products.xml file and stores it in an array list. Be sure to catch any exceptions that may be thrown. Then, test the class. At this point, the application should print three identical product lists, and those lists should match the data thats stored inthe products.xml file.
Add code to the writeProducts method that writes the XML document to the products.xml file. Then, remove the comments from the code in the main method that adds and removes a product from the list. Finally, test the application again. When you do, it should display the messages shown below:
XML:
build.xml:
Builds, tests, and runs the project ch19_ex1_XMLTester.
products.xml:
Murach's Beginning Java49.5Murach's Java Servlets and JSP49.5
Code:
Product:
import java.text.NumberFormat;
public class Product
{
private String code;
private String description;
private double price;
public Product(String code, String description, double price)
{
this.code = code;
this.description = description;
this.price = price ;
}
public Product()
{
this("", "", 0);
}
public void setCode(String code)
{
this.code = code;
}
public String getCode(){
return code;
}
public void setDescription(String description)
{
this.description = description;
}
public String getDescription()
{
return description;
}
public void setPrice(double price)
{
this.price = price;
}
public double getPrice()
{
return price;
}
public String getFormattedPrice()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}
public boolean equals(Object object)
{
if (object instanceof Product)
{
Product product2 = (Product) object;
if
(
code.equals(product2.getCode()) &&
description.equals(product2.getDescription()) &&
price == product2.getPrice()
)
return true;
}
return false;
}
public String toString()
{
return "Code: " + code + " " +
"Description: " + description + " " +
"Price: " + this.getFormattedPrice() + " ";
}
}
StringUtils:
public class StringUtils
{
public static String padWithSpaces(String s, int length)
{
if (s.length()
{
StringBuilder sb = new StringBuilder(s);
while(sb.length()
{
sb.append(" ");
}
return sb.toString();
}
else
{
return s.substring(0, length);
}
}
}
XMLTesterApp:
import java.util.ArrayList;
import java.io.*;
import javax.xml.stream.*; // StAX API
public class XMLTesterApp
{
private static String productsFilename = "products.xml";
public static void main(String[] args)
{
System.out.println("Products list:");
ArrayList products = readProducts();
printProducts(products);
/*
Product p1 = new Product("test", "XML Tester", 77.77);
products.add(p1);
writeProducts(products);
System.out.println("XML Tester has been added to the XML document. ");
*/
System.out.println("Products list:");
products = readProducts();
printProducts(products);
/*
products.remove(2);
writeProducts(products);
System.out.println("XML Tester has been deleted from the XML document. ");
*/
System.out.println("Products list:");
products = readProducts();
printProducts(products);
}
private static ArrayList readProducts()
{
ArrayList products = new ArrayList();
Product p = null;
// add code that reads the XML document from the products.xml file
return products;
}
private static void writeProducts(ArrayList products)
{
// add code that writes the XML document to the products.xml file
}
private static void printProducts(ArrayList products)
{
for (Product p : products)
{
printProduct(p);
}
System.out.println();
}
private static void printProduct(Product p)
{
String productString =
StringUtils.padWithSpaces(p.getCode(), 8) +
StringUtils.padWithSpaces(p.getDescription(), 44) +
p.getFormattedPrice();
System.out.println(productString);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started