HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Calculations with FX

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
calculationswithstackoverflow

Problem

There have been a lot of calculators lately. It seems like this community challenge all over again, I didn't make a proper one then, so I'm joining the calculatrain now.

Building from the lessons learned from a preceding far simpler calculator.

The goal was to encompass corner cases that are otherwise easy to miss. I'd like pinpoint whether I've missed anything and whether I'm doing something inefficiently that should/could be done in a cleaner way.

I'm also concerned about names and readability, I want to make this easy to follow and understand, despite it being a solo venture, the purpose is practice, and if CR has taught me anything it's that that is crucial.

Calculator.java

```
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Calculator extends Application {
static Operator currentOperator;
static boolean operatorSelected;
static boolean resultDisplayed;

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage stage) {
BorderPane layout = new BorderPane();

TextField auxiliary = new TextField();
auxiliary.setStyle("-fx-font-size: 15; -fx-text-fill: gray");
auxiliary.setMaxWidth(415); // 415 = total width, including margins of buttons
auxiliary.setEditable(false);

TextField result = new TextField();
result.setStyle("-fx-font-size: 40");
result.setMaxWidth(415);
result.setEditable(false);

VBox resultLayout = new VBox();
resultLayout.getChildren().addAll(auxiliary, result);
layout.setTop(resultLayout);

GridPane buttonLayout = new GridPane();
buttonLayout.setPadding(new Insets(10, 0, 0, 0));
buttonLa

Solution

There is no way around it, but start() is too lengthy... you should consider breaking it down. For example, you can consolidate your UI elements in the following manner:

  • Auxillary and main text fields



  • The Clear and "backspace" buttons



  • The numeric and dot buttons



  • The operator buttons



Then, start() will only have four such method calls, which can make its readability much better.

As for the Operator enum, you may want to consider using DoubleBinaryOperator instead of BinaryOperator to avoid the auto-boxing.

Context

StackExchange Code Review Q#98103, answer score: 2

Revisions (0)

No revisions yet.