Package calculator

Class CalculatorModel

java.lang.Object
calculator.CalculatorModel

public class CalculatorModel extends Object
This class represents the model in the MVC architecture for a calculator application. It handles all the functional operations and calculations.
Since:
24 Aug 2023
Version:
1.0.0
Author:
Shamith Nakka
  • Field Details

    • expression

      private final List<String> expression
    • operations

      private final Stack<String> operations
    • values

      private final Stack<String> values
    • hasDisplayed

      private boolean hasDisplayed
  • Constructor Details

    • CalculatorModel

      public CalculatorModel()
      Constructs a new CalculatorModel instance. This constructor initializes any required data or resources.
  • Method Details

    • backspaceTextFeild

      private static void backspaceTextFeild(StringBuilder input)
      This method removes the last character of the String value for the given StringBuilder.
      Parameters:
      input - : a StringBuilder object containing data for the text field.
    • getAllChildren

      public static Set<Component> getAllChildren(Container container)
      This function returns all the children of given container.
      Parameters:
      container - [java.awt.Container] : Container object.
      Returns:
      children[HashSet] : set of component children.
    • isNumericValue

      public static boolean isNumericValue(String inputString, boolean checkOnlyForDecimalValue)
      This methods takes a String input and return true if it's possible numeric/decimal value or else false.
      Parameters:
      inputString - : Input string that need to be checked.
      checkOnlyForDecimalValue - : a boolean value that decides the regex checking.
      Returns:
      isVaild[boolean] : Returns true if it's numeric value else false.
    • matchesRegex

      public static boolean matchesRegex(String regex, String targetString)
      This method will check whether the given target string matches the given regular expression.
      Parameters:
      regex - : Regular Expression.
      targetString - : String need to checked.
      Returns:
      [boolean]: return true if given string matches given regex.
    • negateInputString

      private static void negateInputString(StringBuilder input)
      This method negates the value of given StringBuilder.
      Parameters:
      input - : a StringBuilder object containing data for the text field
    • replaceStringBuilderValue

      public static void replaceStringBuilderValue(StringBuilder stringBuilder, String stringValue)
      This method takes an StringBuilder object and replace it's String value with given string value.
      Parameters:
      stringBuilder - : Given StringBuidler object.
      stringValue - : String value needed to be replaced.
    • clearTextFeild

      private void clearTextFeild(StringBuilder inputSB, boolean forTextField)
      This method clears the String value of the given StringBuilder.
      Parameters:
      inputSB - : a StringBuilder object containing data for the text field.
      forTextField - : boolean value that represents the origins of the method call.
    • handleOperationsBasedOnPrecedence

      private void handleOperationsBasedOnPrecedence(String operator, boolean isHighPrecedence) throws InvaildOperatorException, ArithmeticException
      This method handles the operation based on the precedence.
      Parameters:
      operator - : a String value representing operator.
      isHighPrecedence - : a boolean value to determine level of precedence.
      Throws:
      InvaildOperatorException - If any other option is selected that the one defined.
      ArithmeticException - To handle the "Division by zero" case.
    • evaluateExpression

      private void evaluateExpression(StringBuilder inputStringBuilder) throws InvaildOperatorException, ArithmeticException
      This method evaluates the current expression.
      Parameters:
      inputStringBuilder - : a string builder object representing TextField object.
      Throws:
      InvaildOperatorException - If any other option is selected that the one defined.
      ArithmeticException - To handle the "Division by zero" case.
    • executeOperation

      private String executeOperation(ArithmeticOperation handleOperation, boolean isDivisionOperation) throws ArithmeticException
      This function executes the operation provided according the values passed to it.
      Parameters:
      handleOperation - : Lambda function to perform provided operation.
      isDivisionOperation - : Special case for Division operation.
      Returns:
      value : a String value representing the end result of the operation.
      Throws:
      ArithmeticException - To handle the "Division by zero" case.
    • handleOperation

      public void handleOperation(String operationName, StringBuilder argumentStringBuilder) throws InvaildOperatorException, ArithmeticException
      This method will handle all the special operation of the calculator.
      Parameters:
      operationName - : Defines the type of operation.
      argumentStringBuilder - : User Input data.
      Throws:
      InvaildOperatorException - If any other option is selected that the one defined.
      ArithmeticException - To handle the "Division by zero" case.
    • hasDisplayedResult

      public boolean hasDisplayedResult()
      This method returns true when the evaluated value is displayed or not.
      Returns:
      hasDisplayedResult : returns true to denote the evaluated result has been displayed.
    • selecteArithmeticdOperation

      private void selecteArithmeticdOperation(StringBuilder inputStringBuilder, String operator)
      Pushes the input TextField with operation into respective stacks.
      Parameters:
      inputStringBuilder - : StringBuilder object of TextFeild
      operator - : String value of Operator.