Add controller logic
This commit is contained in:
parent
a0d1e71ed5
commit
e0cffd92fa
|
@ -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<Double> op) {
|
||||
// TODO
|
||||
this.pushOperandIfPossible();
|
||||
this.calc.performOperation(op);
|
||||
this.updateOperandsView();
|
||||
}
|
||||
|
||||
private void performOperation(boolean swap, BinaryOperator<Double> op) {
|
||||
if (hasOperand()) {
|
||||
// TODO push operand first
|
||||
// Removed `swap` parameter as described in
|
||||
// https://piazza.com/class/kssraggjcxm4at?cid=56
|
||||
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
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,21 +23,21 @@
|
|||
GridPane.rowIndex="2" GridPane.columnIndex="1"/>
|
||||
<Button text="9" onAction="#handleDigit"
|
||||
GridPane.rowIndex="2" GridPane.columnIndex="2"/>
|
||||
|
||||
|
||||
<Button text="4" onAction="#handleDigit"
|
||||
GridPane.rowIndex="3" GridPane.columnIndex="0"/>
|
||||
<Button text="5" onAction="#handleDigit"
|
||||
GridPane.rowIndex="3" GridPane.columnIndex="1"/>
|
||||
<Button text="6" onAction="#handleDigit"
|
||||
GridPane.rowIndex="3" GridPane.columnIndex="2"/>
|
||||
|
||||
|
||||
<Button text="1" onAction="#handleDigit"
|
||||
GridPane.rowIndex="4" GridPane.columnIndex="0"/>
|
||||
<Button text="2" onAction="#handleDigit"
|
||||
GridPane.rowIndex="4" GridPane.columnIndex="1"/>
|
||||
<Button text="3" onAction="#handleDigit"
|
||||
GridPane.rowIndex="4" GridPane.columnIndex="2"/>
|
||||
|
||||
|
||||
<Button text="0" onAction="#handleDigit"
|
||||
GridPane.rowIndex="5" GridPane.columnIndex="0"/>
|
||||
<Button text="." onAction="#handlePoint"
|
||||
|
@ -54,11 +54,10 @@
|
|||
<Button text="*" onAction="#handleOpMult"
|
||||
GridPane.rowIndex="6" GridPane.columnIndex="2"/>
|
||||
|
||||
<!-- TODO -->
|
||||
<Button text="/"
|
||||
<Button text="/" onAction="#handleOpDiv"
|
||||
GridPane.rowIndex="6" GridPane.columnIndex="3"/>
|
||||
<Button text="√"
|
||||
<Button text="√" onAction="#handleOpRoot"
|
||||
GridPane.rowIndex="7" GridPane.columnIndex="0"/>
|
||||
<Button text="π"
|
||||
<Button text="π" onAction="#handleOpPi"
|
||||
GridPane.rowIndex="7" GridPane.columnIndex="1"/>
|
||||
</GridPane>
|
||||
|
|
Loading…
Reference in New Issue