CompositePart.java

package ProductConfig;
import java.io.PrintStream;
import java.util.*;

public class CompositePart extends BasicPart
{
// use a fixed algorithm for price
CompositePart(String name, String description, double price)
{
    super(name, description, price);
    m_useFixed = true;
    m_parts = new Vector();
}

// Use a calculated algorigthm for price
CompositePart(String name, String description)
{
    super(name, description, 0);
    m_useFixed = false;
    m_parts = new Vector();
}

private double sumChildrenPrices()
{
    double price = 0;
    for (Enumeration e = m_parts.elements() ;
      e.hasMoreElements() ; )
    {
        Part tmpPart = (Part)e.nextElement();
        price = price + tmpPart.getPrice();
    }
    return price;
}

public void addPart(Part part) throws NotACompositeException
{
    getResourceList().addResourceList(part.getResourceList());
    m_parts.addElement(part);
}

public void removePart(Part part) throws NotACompositeException
{
    getResourceList().removeResourceList(part.getResourceList());
    m_parts.removeElement(part);
}
public double getPrice()
{
// If we are using fixed then get the Parent
//base price, otherwise sum the children prices
    return  (m_useFixed) ? super.getPrice() :
             sumChildrenPrices();
}

public void displayInvoice(PrintStream str, int level)
{
    final int LEVEL_INCREMENT = 5;
    super.displayInvoice(str, level);

    for (Enumeration e = m_parts.elements() ;
      e.hasMoreElements() ; )
    {
        Part tmpPart = (Part)e.nextElement();
        tmpPart.displayInvoice(str, level +
           LEVEL_INCREMENT);
    }
}

private Vector m_parts;
private boolean m_useFixed;

}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.59.187