Skip to content

Instantly share code, notes, and snippets.

@BMU-Verlag
Created October 10, 2019 15:43
Show Gist options
  • Save BMU-Verlag/a4a0e36c1d19775a6d95e19747d966d6 to your computer and use it in GitHub Desktop.
Save BMU-Verlag/a4a0e36c1d19775a6d95e19747d966d6 to your computer and use it in GitHub Desktop.
@FXML
private void evaluate() {
if (nextOperation == Operation.none) {
input.setText("");
lastResult.setText("");
return;
}
var lastResultFloat = Float.parseFloat(lastResult.getText());
var inputFloat = Float.parseFloat(input.getText());
var result = 0f;
switch (nextOperation) {
case ADD:
result = lastResultFloat + inputFloat;
break;
case SUBTRACT:
result = lastResultFloat - inputFloat;
break;
case MULTIPLY:
result = lastResultFloat * inputFloat;
break;
case DIVIDE:
if (inputFloat == 0) {
lastResult.setText("Error");
input.setText("");
nextOperation = Operation.NONE;
return;
}
result = lastResultFloat / inputFloat;
break;
default:
return;
}
input.setText("");
lastResult.setText(String.valueOf(result));
nextOperation = Operation.none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment