Add controller logic

This commit is contained in:
Oystein Kristoffer Tveit 2021-09-05 19:38:35 +02:00
parent a0d1e71ed5
commit e0cffd92fa
2 changed files with 57 additions and 23 deletions

View File

@ -74,60 +74,95 @@ public class CalcController {
updateOperandsView(); updateOperandsView();
} }
/* -------------------------------------------------------------------------- */
/* Exercise Functions */
/* -------------------------------------------------------------------------- */
private void appendToOperand(String s) { private void appendToOperand(String s) {
// TODO this.setOperand(this.getOperandString() + s);
} }
@FXML @FXML
void handleDigit(ActionEvent ae) { void handleDigit(ActionEvent ae) {
if (ae.getSource()instanceof Labeled l) { if (ae.getSource() instanceof Labeled l) {
// TODO append button label to operand this.appendToOperand(l.getText());
} }
} }
@FXML @FXML
void handlePoint() { void handlePoint() {
var operandString = getOperandString(); var operandString = this.getOperandString();
if (operandString.contains(".")) { if (operandString.contains(".")) {
// TODO remove characters after point String stringBeforeDot = operandString.substring(0, operandString.indexOf('.') + 1);
this.setOperand(stringBeforeDot);
} else { } else {
// TODO append point this.appendToOperand(".");
} }
} }
@FXML @FXML
void handleClear() { void handleClear() {
// TODO clear operand this.setOperand("");
}
private void pushOperandIfPossible() {
if (this.hasOperand()) {
this.calc.pushOperand(this.getOperand());
this.setOperand("");
}
} }
@FXML @FXML
void handleSwap() { void handleSwap() {
// TODO clear operand this.pushOperandIfPossible();
this.calc.swap();
this.updateOperandsView();
} }
private void performOperation(UnaryOperator<Double> op) { private void performOperation(UnaryOperator<Double> op) {
// TODO this.pushOperandIfPossible();
this.calc.performOperation(op);
this.updateOperandsView();
} }
private void performOperation(boolean swap, BinaryOperator<Double> op) { // Removed `swap` parameter as described in
if (hasOperand()) { // https://piazza.com/class/kssraggjcxm4at?cid=56
// TODO push operand first private void performOperation(BinaryOperator<Double> 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 @FXML
void handleOpAdd() { void handleOpAdd() {
// TODO this.performOperation((x, y) -> x + y);
} }
@FXML @FXML
void handleOpSub() { void handleOpSub() {
// TODO this.performOperation((x, y) -> x - y);
} }
@FXML @FXML
void handleOpMult() { 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();
} }
} }

View File

@ -54,11 +54,10 @@
<Button text="*" onAction="#handleOpMult" <Button text="*" onAction="#handleOpMult"
GridPane.rowIndex="6" GridPane.columnIndex="2"/> GridPane.rowIndex="6" GridPane.columnIndex="2"/>
<!-- TODO --> <Button text="/" onAction="#handleOpDiv"
<Button text="/"
GridPane.rowIndex="6" GridPane.columnIndex="3"/> GridPane.rowIndex="6" GridPane.columnIndex="3"/>
<Button text="√" <Button text="√" onAction="#handleOpRoot"
GridPane.rowIndex="7" GridPane.columnIndex="0"/> GridPane.rowIndex="7" GridPane.columnIndex="0"/>
<Button text="π" <Button text="π" onAction="#handleOpPi"
GridPane.rowIndex="7" GridPane.columnIndex="1"/> GridPane.rowIndex="7" GridPane.columnIndex="1"/>
</GridPane> </GridPane>