import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;

public class AveragePriceBooks extends DefaultHandler {

  private int count = 0;
  private boolean isFiction = false;
  private double totalPrice = 0.0;
  private StringBuffer content = new StringBuffer();

  private static double prices[] = new double[100];
  private int bid = -1;
    private int bookID = -1;
    private double price = 0.0;
    private boolean process_books = false;

  public AveragePriceBooks () {super();}

  public void determineAveragePrice() throws Exception 
  {
    XMLReader xr = new org.apache.xerces.parsers.SAXParser();
    AveragePriceBooks handler = new AveragePriceBooks();
    xr.setContentHandler(handler);
    xr.parse("priceList.xml");
    xr.parse("books.xml");

  }

  public void startElement(String uri, String localName, String rawName, Attributes atts) throws SAXException 
  {

    if (localName.equals("books"))
	{
	    process_books = true;
	}  
    else if (localName.equals("book")) 
    {
      String category = atts.getValue("category");
      isFiction = (category!=null && category.equals("fiction"));
      if (isFiction)
	 {
	     count++;
	     bookID = Integer.parseInt(atts.getValue("ID"));
	     price = prices[bookID];
	     totalPrice += price;
	 }
    }
    else  if (localName.equals("bPrice"))
    {
      bid = Integer.parseInt(atts.getValue("bookId"));      
    }
    content.setLength(0);
  }

  public void characters(char[] chars, int start, int len) throws SAXException 
  {
    content.append(chars, start, len);
  }

  public void endElement(String uri, String localName, String rawName) throws SAXException 
  {
    if (localName.equals("book") && isFiction) 
    {
	System.out.println(price +"\n");
	bookID = -1; 
	price = 0.0;
    }
    else if (localName.equals("author") && isFiction)
    {
	System.out.print(content.toString() +", ");
    }

    else if (localName.equals("title") && isFiction)
    {
	System.out.print(content.toString()+": " );
    }
    else  if (localName.equals("bPrice"))
    {
      try 
      { 
        double price = new Double(content.toString()).doubleValue();
	prices[bid] = price;
        bid = -1;
      }
      catch (java.lang.NumberFormatException err) 
      {
        throw new SAXException("Price is not numeric");
      }	
    }
    content.setLength(0);
  }

  public void endDocument() throws SAXException 
  { 
      if (process_books)
      {
	  if (count > 0) {
	      System.out.println("The average price of fiction books is " + totalPrice / count);
	  } 
	  else {
	      System.out.println("No fiction books found.");
	  }
      }
  }

  public static void main (String args[]) throws java.lang.Exception 
  {
    try 
    {
      (new AveragePriceBooks()).determineAveragePrice();
    }
    catch (SAXException err) 
    {
      System.err.println("Parsing failed: " + err.getMessage());
    }
  }
}
