HomeProjectsContactGitHub

Calculator

Repository

This is a semi-advanced calculator written in Java and CSS for styling using the JavaFX GUI library.

Operator Actions

This snippet shows how the operator buttons are handled. It uses a Map to store the buttons and their corresponding operators, and then iterates through the map to set the action for each button.

1//Operator Button Actions
2Map<Button, String> operatorButtons = new HashMap<>();
3operatorButtons.put(addBtn, "+");
4operatorButtons.put(subtractBtn, "-");
5operatorButtons.put(multiplyBtn, "*");
6operatorButtons.put(divideBtn, "/");
7
8operatorButtons.forEach((button, operator) ->
9        button.setOnAction(e -> {
10            if (!calc.currentNumber.isEmpty()) {
11                calc.allNumbers.add(calc.currentNumber);
12                calc.currentNumber = "";
13                calc.allOperations.add(operator);
14            } else if (!calc.allOperations.isEmpty()) {
15                calc.allOperations.set(calc.allOperations.size() - 1, operator);
16            } else {
17                calc.allOperations.add(operator);
18            }
19            equationLabel.setText(calc.getCurrentEquation());
20        })
21);
22
23//Negate button
24negateBtn.setOnAction(e -> {
25    if (calc.currentNumber.isEmpty()) {
26        calc.currentNumber += "-";
27    } else {
28        if (calc.currentNumber.charAt(0) == '-') {
29            calc.currentNumber = calc.currentNumber.substring(1);
30        } else {
31            calc.currentNumber = "-" + calc.currentNumber;
32        }
33    }
34    output.setText(calc.getCurrentNumber());
35    equationLabel.setText(calc.getCurrentEquation());
36});

Calculation Logic

The calculation logic handles the order of operations (multiplication and division first, then addition and subtraction). It splits the equation into parts and then iterates through them to perform the calculations.

1public void calculateAll() {
2    if (correctNumberAndOperationAmount()) {
3        try {
4            String[] parts = currentEquation.split(" ");
5            System.out.println("Parts: " + Arrays.toString(parts));
6
7            for (int i = 1; i < parts.length; i++) {
8                if (parts[i].equals("*") || parts[i].equals("/")) {
9                    double prevNum = Double.parseDouble(parts[i - 1]);
10                    double nextNum = Double.parseDouble(parts[i + 1]);
11                    double resultPart = 0;
12
13                    if (parts[i].equals("*")) {
14                        resultPart = prevNum * nextNum;
15                    } else if (parts[i].equals("/")) {
16                        if (nextNum == 0) {
17                            result = "#ERROR#";
18                            return;
19                        }
20                        resultPart = prevNum / nextNum;
21                    }
22
23                    parts[i - 1] = String.valueOf(resultPart);
24                    for (int j = i; j < parts.length - 2; j++) {
25                        parts[j] = parts[j + 2];
26                    }
27                    parts = Arrays.copyOf(parts, parts.length - 2);
28                    i--;
29                }
30            }
31        } catch (Exception e) {
32            result = "#ERROR#";
33        }
34    } else {
35        result = "#ERROR#";
36    }
37}

Try it out yourself!

Download

If you want to try this app out for yourself, click on the download button and install from GitHub.

Video Demonstration

This video requires optional cookies (YouTube).