/*
 * @topic T10184 Feb 7, 2017 Inheritance Demo
 * @brief class ProgrammableAppliance keeps ArrayList of the binary op objects
*/
package week03;
import java.util.ArrayList;

public class ProgrammableAppliance {
    //------------------------------
    // data attributes
    //------------------------------
    private ArrayList<BaseBinaryOp> operations =
            new ArrayList< BaseBinaryOp >();

    private int result = 0;

    //------------------------------
    // operations
    //------------------------------
    public void addOperation(BaseBinaryOp op)
    {
        operations.add( op );
    }//addOperation
    
    public void compute() {
        result = 0; // prepare for computation
        if ( operations.isEmpty() ) {
            // nothing to do
            return;
        }
        for ( BaseBinaryOp op : operations ) {
            if ( op != operations.get( 0 ) ) {
                // pass intermidiate result
                // as left-hand-side of the next
                // operation
                op.setLeft( result );
            }
            op.compute();
            result = op.getResult();
        }
    }//compute

    public void computeUgly() {
        result = 0; // prepare for computation
        if ( operations.isEmpty() ) {
            // nothing to do
            return;
        }
        for ( BaseBinaryOp op : operations ) {
            if ( op != operations.get( 0 ) ) {
                // pass intermidiate result
                // as left-hand-side of the next
                // operation
                op.setLeft(result);
            }
            if ( op instanceof BinaryOp ) {
                BinaryOp bop = (BinaryOp)op;
                bop.compute();
                result = bop.getResult();
                
            } else if ( op instanceof MultiplyOp ) {
                MultiplyOp mop = (MultiplyOp)op;
                mop.compute();
                result = mop.getResult();
            }
        }
    }//computeUgly
    
    public int getResult() {
        return result;
    }//getResult

}//class ProgrammableAppliance