fix: improve error handling and user feedback in calculation logic

This commit is contained in:
Dominik Krenn 2025-09-19 14:16:42 +02:00
parent fb05209009
commit 06f16c244f

View File

@ -69,7 +69,7 @@ public class Application extends Base {
return;
}
if (code == javafx.scene.input.KeyCode.BACK_SPACE || code == javafx.scene.input.KeyCode.DELETE) {
if (code == javafx.scene.input.KeyCode.BACK_SPACE || code == javafx.scene.input.KeyCode.DELETE) {
if (code == javafx.scene.input.KeyCode.DELETE) {
this.formula = "";
this.label.setText(formula);
@ -83,7 +83,6 @@ public class Application extends Base {
String[] tokens = trimmed.split(" ");
if (tokens.length > 0) {
// remove last token
tokens = Arrays.copyOf(tokens, tokens.length - 1);
this.formula = String.join(" ", tokens);
} else {
@ -137,16 +136,13 @@ public class Application extends Base {
}
if ("+-*/".contains(label)) {
if (label.equals("-") && (trimmed.isEmpty() || trimmed.matches(".*[\\+\\-\\*/]$"))) {
if (label.equals("-") && (trimmed.isEmpty() || trimmed.matches(".*[+\\-*/]$"))) {
this.formula += "-";
this.label.setText(formula);
return;
}
if (trimmed.isEmpty()) return;
if (trimmed.matches(".*[\\+\\-\\*/]$")) return;
this.formula += " " + label + " ";
} else if ("=".equals(label)) {
if (trimmed.isEmpty() || trimmed.matches(".*[\\+\\-\\*/]$")) return;
solve();
} else if ("CLR".equals(label)) {
this.formula = "";
@ -171,13 +167,16 @@ public class Application extends Base {
}
try {
double result = eval(expr);
if (result == (long) result) {
if (Double.isInfinite(result) || Double.isNaN(result)) {
this.formula = "ENGIE: DIVIDE BY ZERO, YA DUNCE!";
lastInputWasError = true;
} else if (result == (long) result) {
this.formula = String.valueOf((long) result);
} else {
this.formula = String.valueOf(result);
}
} catch (Exception e) {
this.formula = "ERR";
this.formula = "SOLDIER: YOU BROKE IT, MAGGOT!";
lastInputWasError = true;
}
}