O'Reilly logo

Java & XML Data Binding by Brett McLaughlin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix B. Quick Source Files

This appendix contains the source files for classes into which the XML documents will be unmarshalled. The examples in Chapter 9 use these classes. Each demonstrates that in addition to data accessor and mutator methods, Quick is capable of converting XML into Java classes that have business methods. All of these classes are also available online at http://www.newInstance.com.

Example B-1 is the top of the object tree and represents a purchase order. This can, in turn, store several different orders and also has a business method on it (getTotalPrice()).

Example B-1. The PurchaseOrder class

package javajaxb.po;
  
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
  
public class PurchaseOrder {
  
    /** The list of <code>{@link Order}</code> objects */
    protected List orderList;
  
    public PurchaseOrder(  ) {
        this(new LinkedList(  ));
    }
    
    public PurchaseOrder(List orderList) {
        if (orderList != null) {
            this.orderList = orderList;
        } else {
            this.orderList = new LinkedList(  );
        }
    }
  
    public List getOrderList(  ) {
        return orderList;
    }
  
    public void setOrderList(List orderList) {
        this.orderList = orderList;
    }
  
    public void addOrder(Order order) {
        orderList.add(order);
    }
  
    public float getTotalPrice(  ) {
        if (orderList == null) {
            return 0;
        }
        
        float totalPrice = 0;
        for (Iterator i = orderList.iterator(); i.hasNext(  ); ) {
            Order order = (Order)i.next(  );
            totalPrice += order.getStock().getQuantity() * order.getPurchasePrice(  );
        }
        return totalPrice;
    }
}

Example ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required