Class Logic

java.lang.Object
com.project.calculator.Logic

public class Logic extends Object
The Logic class handles the core operations and event handling for the calculator.

It performs the following tasks:

  • Processes button click events.
  • Handles mathematical operations and displays the result.
  • Manages clearing the display and maintaining history.

This class interacts with both the BaseUI and MainUI to display the results and maintain the input history.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final BaseUI
    Reference to the BaseUI instance for accessing display and history labels.
    private final MainUI
    Reference to the MainUI instance for handling input and result operations.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Logic(BaseUI baseUiInstance, MainUI mainUiInstance)
    Constructs the Logic object with references to the UI instances.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    Handles the evaluation of the expression and displays the result.
    protected void
    Handles the button press events and performs the appropriate action based on the input.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • baseUiInstance

      private final BaseUI baseUiInstance
      Reference to the BaseUI instance for accessing display and history labels.
    • mainUiInstance

      private final MainUI mainUiInstance
      Reference to the MainUI instance for handling input and result operations.
  • Constructor Details

    • Logic

      Logic(BaseUI baseUiInstance, MainUI mainUiInstance)
      Constructs the Logic object with references to the UI instances.
      Parameters:
      baseUiInstance - The BaseUI instance for display and history management.
      mainUiInstance - The MainUI instance for input and button handling.
  • Method Details

    • performAction

      protected void performAction(ActionEvent keyPressed)
      Handles the button press events and performs the appropriate action based on the input.

      It processes digits, operators, clear operations, and evaluates expressions.

      Parameters:
      keyPressed - The ActionEvent triggered by the button press.
    • handleResult

      private void handleResult()
      Handles the evaluation of the expression and displays the result.

      This method:

      • Converts the infix expression to postfix using InfixToPostfix class.
      • Evaluates the result and displays it.
      • Updates the input and history.