diff --git a/oysteikt-calc/src/main/java/oysteikt/calc/CalcController.java b/oysteikt-calc/src/main/java/oysteikt/calc/CalcController.java index 047b383..dd8ad13 100644 --- a/oysteikt-calc/src/main/java/oysteikt/calc/CalcController.java +++ b/oysteikt-calc/src/main/java/oysteikt/calc/CalcController.java @@ -74,60 +74,95 @@ public class CalcController { updateOperandsView(); } + /* -------------------------------------------------------------------------- */ + /* Exercise Functions */ + /* -------------------------------------------------------------------------- */ + private void appendToOperand(String s) { - // TODO + this.setOperand(this.getOperandString() + s); } @FXML void handleDigit(ActionEvent ae) { - if (ae.getSource()instanceof Labeled l) { - // TODO append button label to operand + if (ae.getSource() instanceof Labeled l) { + this.appendToOperand(l.getText()); } } @FXML void handlePoint() { - var operandString = getOperandString(); + var operandString = this.getOperandString(); if (operandString.contains(".")) { - // TODO remove characters after point + String stringBeforeDot = operandString.substring(0, operandString.indexOf('.') + 1); + this.setOperand(stringBeforeDot); } else { - // TODO append point + this.appendToOperand("."); } } @FXML void handleClear() { - // TODO clear operand + this.setOperand(""); + } + + private void pushOperandIfPossible() { + if (this.hasOperand()) { + this.calc.pushOperand(this.getOperand()); + this.setOperand(""); + } } @FXML void handleSwap() { - // TODO clear operand + this.pushOperandIfPossible(); + this.calc.swap(); + this.updateOperandsView(); } private void performOperation(UnaryOperator op) { - // TODO + this.pushOperandIfPossible(); + this.calc.performOperation(op); + this.updateOperandsView(); } - private void performOperation(boolean swap, BinaryOperator op) { - if (hasOperand()) { - // TODO push operand first + // Removed `swap` parameter as described in + // https://piazza.com/class/kssraggjcxm4at?cid=56 + private void performOperation(BinaryOperator op) { + if (this.calc.getOperandCount() > 3) { // There should always be at least 3 times 0.0 in the calculator + this.pushOperandIfPossible(); + this.calc.performOperation(op); + this.updateOperandsView(); } - // TODO perform operation, but swap first if needed } @FXML void handleOpAdd() { - // TODO + this.performOperation((x, y) -> x + y); } @FXML void handleOpSub() { - // TODO + this.performOperation((x, y) -> x - y); } @FXML void handleOpMult() { - // TODO + this.performOperation((x, y) -> x * y); + } + + @FXML + void handleOpDiv() { + this.performOperation((x, y) -> x / y); + } + + @FXML + void handleOpRoot() { + this.performOperation((x) -> Math.sqrt(x)); + } + + @FXML + void handleOpPi() { + this.calc.pushOperand(Math.PI); + this.updateOperandsView(); } } diff --git a/oysteikt-calc/src/main/resources/oysteikt/calc/Calc.fxml b/oysteikt-calc/src/main/resources/oysteikt/calc/Calc.fxml index 2fb7647..9ea6887 100644 --- a/oysteikt-calc/src/main/resources/oysteikt/calc/Calc.fxml +++ b/oysteikt-calc/src/main/resources/oysteikt/calc/Calc.fxml @@ -23,21 +23,21 @@ GridPane.rowIndex="2" GridPane.columnIndex="1"/>