BinaryOperation.java 1.39 KB
package kernel;

abstract public class BinaryOperation extends Formula {

    protected Cell leftCell;
    protected Cell rightCell;
    protected String operator;

    public BinaryOperation(Cell leftCell, Cell rightCell) {
        this.leftCell = leftCell;
        this.rightCell = rightCell;
    }

    public String getDevelopedFormula() {
        return this.leftCell.getDevelopedFormula() + " " + this.operator + " " + this.rightCell.getDevelopedFormula();
    }

    public String toString() {
        return this.leftCell.toString() + " " + this.operator + " " + this.rightCell.toString();
    }

    abstract public Double eval();

    public Boolean createCycle(Cell cell) {
        if (this.leftCell.containFormula() && !this.rightCell.containFormula())
            return this.leftCell.getFormula().createCycle(cell);
        else {
            if (!this.leftCell.containFormula() && this.rightCell.containFormula())
                return this.rightCell.getFormula().createCycle(cell);
            else {
                if (this.leftCell.containFormula() && this.rightCell.containFormula())
                    return this.leftCell.getFormula().createCycle(cell) && this.rightCell.getFormula().createCycle(cell);
                else {
                    return (cell.getId().equals(this.rightCell.getId()) || cell.getId().equals(this.leftCell.getId()));
                }
            }

        }
    }
}