Package calculator
Class CalculatorModel
java.lang.Object
calculator.CalculatorModel
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 Summary
Modifier and TypeFieldDescriptionprivate boolean
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprivate static void
backspaceTextFeild
(StringBuilder input) This method removes the last character of the String value for the given StringBuilder.private void
clearTextFeild
(StringBuilder inputSB, boolean forTextField) This method clears the String value of the given StringBuilder.private void
evaluateExpression
(StringBuilder inputStringBuilder) This method evaluates the current expression.private String
executeOperation
(ArithmeticOperation handleOperation, boolean isDivisionOperation) This function executes the operation provided according the values passed to it.getAllChildren
(Container container) This function returns all the children of given container.void
handleOperation
(String operationName, StringBuilder argumentStringBuilder) This method will handle all the special operation of the calculator.private void
handleOperationsBasedOnPrecedence
(String operator, boolean isHighPrecedence) This method handles the operation based on the precedence.boolean
This method returns true when the evaluated value is displayed or not.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.static boolean
matchesRegex
(String regex, String targetString) This method will check whether the given target string matches the given regular expression.private static void
negateInputString
(StringBuilder input) This method negates the value of given StringBuilder.static void
replaceStringBuilderValue
(StringBuilder stringBuilder, String stringValue) This method takes an StringBuilder object and replace it's String value with given string value.private void
selecteArithmeticdOperation
(StringBuilder inputStringBuilder, String operator) Pushes the input TextField with operation into respective stacks.
-
Field Details
-
expression
-
operations
-
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
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
This function returns all the children of given container.- Parameters:
container
- [java.awt.Container] : Container object.- Returns:
- children[HashSet] : set of component children.
-
isNumericValue
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
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
This method negates the value of given StringBuilder.- Parameters:
input
- : a StringBuilder object containing data for the text field
-
replaceStringBuilderValue
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
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
Pushes the input TextField with operation into respective stacks.- Parameters:
inputStringBuilder
- : StringBuilder object of TextFeildoperator
- : String value of Operator.
-