package candy; import java.lang.reflect.Constructor; /** * A candy machine that has the capacity of N basic candy machines. * by a base machine. * * @author CSC223 2004F * @version 0.0 of August 2004 */ public class BiggerCandyMachine implements CandyMachine { // +--------+------------------------------------------------------------ // | Fields | // +--------+ int machinesLeft = 5; CandyMachine base; CandyMachineFactory fac; // +--------------+------------------------------------------------------ // | Constructors | // +--------------+ public BiggerCandyMachine(CandyMachineFactory _fac) { this.fac = _fac; this.base = _fac.newMachine(); } // BiggerCandyMachine(CandyMachine) // +----------+---------------------------------------------------------- // | Mutators | // +----------+ public String get() throws CandyMachineException { // Build a new machine, if necessary if (this.base.isEmpty() && this.machinesLeft > 0) { this.base = this.newMachine(); --this.machinesLeft; } return base.get(); } // get() // +-----------+--------------------------------------------------------- // | Observers | // +-----------+ public boolean isEmpty() { // For the bigger machine to be empty, we need // no other machines left and the base machine to // be empty. return (this.machinesLeft == 0) && this.base.isEmpty(); } // isEmpty() // +-------+------------------------------------------------------------- // | Local | // +-------+ private CandyMachine newMachine() throws CandyMachineException { return this.fac.newMachine(); } // newMachine() } // class BiggerCandyMachine