Package pl.eternalsh.simplecalc.controller

Source Code of pl.eternalsh.simplecalc.controller.CalculateExpressionEventStrategy

package pl.eternalsh.simplecalc.controller;

import pl.eternalsh.simplecalc.common.events.ApplicationEvent;
import pl.eternalsh.simplecalc.common.events.CalculateExpressionEvent;
import pl.eternalsh.simplecalc.model.*;
import pl.eternalsh.simplecalc.view.View;

/**
* Handler for CalculateExpressionEvent.
*
* @author Amadeusz Kosik
*/
class CalculateExpressionEventStrategy extends AbstractEventStrategy
{
    /**
     * Creates new event handler.
     *
     * @param model instance of the model
     * @param view instance of the view
     */
    CalculateExpressionEventStrategy(final Model model, final View view)
    {
        super(model, view);
    }

    /**
     * Sends user's expression to the model and updates the result field in the view with expression's result calculated
     *  by the model.
     *
     * @param event event sent by the GUI.
     */
    void handle(final ApplicationEvent event)
    {
        CalculateExpressionEvent calculateExprEvent = (CalculateExpressionEvent) event;

        try
        {
            Result result = model.parseExpression(calculateExprEvent.getExpression());
            view.updateResultField(result.toString().concat("=").concat(calculateExprEvent.getExpression()));
            System.out.println(result.toString());
        }
        catch(IllegalInputStringException illegalInputException)
        {
            view.updateResultField("Illegal characters found.");
        }
        catch(MissingOperationException missingOperationException)
        {
            view.updateResultField("Unexpected operand.");
        }
        catch(MissingOperandException missingOperandException)
        {
            view.updateResultField("Unexpected operation.");
        }
        catch(UnmatchedClosingParenthesisException unmatchedClosingParenthesisException)
        {
            view.updateResultField("Unmatched closing parenthesis found.");
        }
        catch(UnmatchedOpeningParenthesisException unmatchedOpeningParenthesisException)
        {
            view.updateResultField("Unmatched opening parenthesis found.");
        }
    }
}
TOP

Related Classes of pl.eternalsh.simplecalc.controller.CalculateExpressionEventStrategy

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.