import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.TextArea; public class CalculatorControl { @FXML private TextArea display; private double firstNumber; private double secondNumber; private String operation; private boolean awaitingSecondNumber; @FXML void onClearButtonClicked(ActionEvent event) { display.clear(); firstNumber = 0; secondNumber = 0; operation = null; awaitingSecondNumber = false; } @FXML void onClearEntryButtonClicked(ActionEvent event) { display.clear(); } @FXML void onDeleteButtonClicked(ActionEvent event) { String currentText = display.getText(); if (currentText != null && !currentText.isEmpty()) { display.setText(currentText.substring(0, currentText.length() - 1)); } } @FXML void onEqualsButtonClicked(ActionEvent event) { if (awaitingSecondNumber) { String displayText = display.getText(); String[] parts = displayText.split(" "); try { secondNumber = Double.parseDouble(parts[parts.length - 1]); switch (operation) { case "+": display.setText(firstNumber + " + " + secondNumber + " = " + (firstNumber + secondNumber)); break; case "-": display.setText(firstNumber + " - " + secondNumber + " = " + (firstNumber - secondNumber)); break; case "*": display.setText(firstNumber + " * " + secondNumber + " = " + (firstNumber * secondNumber)); break; case "/": if (secondNumber == 0) { display.setText("Error"); } else { display.setText(firstNumber + " / " + secondNumber + " = " + (firstNumber / secondNumber)); } break; case "%": display.setText(firstNumber + " % " + secondNumber + " = " + (firstNumber % secondNumber)); break; } awaitingSecondNumber = false; } catch (NumberFormatException e) { display.setText("Error"); } } } @FXML void onNumberButtonClicked(ActionEvent event) { Button button = (Button) event.getSource(); String buttonText = button.getText(); display.appendText(buttonText); } @FXML void onOperationButtonClicked(ActionEvent event) { Button button = (Button) event.getSource(); String buttonText = button.getText(); if (!awaitingSecondNumber) { String displayText = display.getText(); String[] parts = displayText.split(" "); try { firstNumber = Double.parseDouble(parts[parts.length - 1]); operation = buttonText; awaitingSecondNumber = true; display.appendText(" " + buttonText + " "); } catch (NumberFormatException e) { display.setText("Error"); } } } }