package ProductConfig; public class PartBuilder { public PartBuilder(PartList partList) { m_partList = partList; } public void beginPart(String name , String description ) { // We will use a composite part as the holding queue // if it turns out to be simple we will simply // transform it later m_pendingPart = new CompositePart(name,description); m_isComposite = false; } public void beginPart(String name , String description , double price) { // We will use a composite part as the holding queue // if it turns out to be simple we will simply // transform it later m_pendingPart = new CompositePart(name,description,price); m_isComposite = false; } public void endPart() { // if it's not composite transform it if (!m_isComposite) { String name = m_pendingPart.getName(); String description = m_pendingPart.getDescription(); double price = m_pendingPart.getPrice(); ResourceList rList = m_pendingPart.getResourceList(); m_pendingPart = new SimplePart(name, description, price); m_pendingPart.getResourceList().addResourceList(rList); } m_partList.addPart(m_pendingPart); // After adding it invalidate it from memory m_pendingPart = null; } public void addSubPart(String name) { try { m_isComposite = true; m_pendingPart.addPart(m_partList.getPart(name)); } catch (Exception NotACompositeException) { m_isComposite = false; // TBD: Recovery kept simple for now: just ignore } } public void addResource(String name, int count) { m_pendingPart.getResourceList().addResource(name, count); } public void removeResource(String name, int count) { m_pendingPart.getResourceList().removeResource(name, count); } private PartList m_partList; private boolean m_isComposite; private Part m_pendingPart; }
/** * This class can take a variable number of parameters on the command * line. Program execution begins with the main() method. The class * constructor is not invoked unless an object of type 'Class1' * created in the main() method. */ package ProductConfig; public class Driver { public static void fillPartList(PartBuilder builder) { builder.beginPart("Aplo2", "Apollo 2 Motherboard",109); builder.addResource("PCI Slots",5); builder.addResource("DIMM",3); builder.addResource("AGP Port",1); builder.addResource("Slot 1",1); builder.removeResource("Motherboard",1); builder.endPart(); builder.beginPart("Sota24", "Sota 24",115); builder.addResource("PCI Slots",5); builder.addResource("DIMM",3); builder.addResource("Slot 1",1); builder.removeResource("Motherboard",1); builder.endPart(); builder.beginPart("MX24","Std Mid-sized Tower Case",59); builder.addResource("Motherboard",1); builder.endPart(); builder.beginPart("FX24","Std Full-sized Tower Case",79); builder.addResource("Motherboard",1); builder.endPart(); builder.beginPart("I550","550Mhz Slot 1 Processor",300); builder.removeResource("Slot 1",1); builder.endPart(); builder.beginPart("I600","600Mhz Slot 1 Processor",350); builder.removeResource("Slot 1",1); builder.endPart(); builder.beginPart("D128","128MB Dimm Memory",120); builder.removeResource("DIMM",1); builder.endPart(); builder.beginPart("D256","256MB Dimm Memory - 1 slot",270); builder.removeResource("DIMM",1); builder.endPart(); builder.beginPart("D256-2","256MB Dimm Memory - 2 slots",230); builder.removeResource("DIMM",2); builder.endPart(); builder.beginPart("BBC1","BareBones Config 1",300); builder.addSubPart("Sota24"); builder.addSubPart("I550"); builder.addSubPart("MX24"); builder.endPart(); } public static void main (String[] args) { PartList list = new PartList(); PartBuilder builder = new PartBuilder(list); fillPartList(builder); builder.beginPart("Galaxy Pro 200","Basic setup system"); builder.addSubPart("BBC1"); builder.addSubPart("D256"); builder.endPart(); Part base = list.getPart("Galaxy Pro 200"); base.displayInvoice(System.out, 0); System.out.println("Resource are: " + (base.getResourceList().isValid() ? "Valid" : "Invalid")); base.getResourceList().displayResources(System.out); } }
3.149.233.72