lf uten tester
This commit is contained in:
@@ -1,11 +1,136 @@
|
||||
package groupid;
|
||||
package app;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.function.BinaryOperator;
|
||||
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.Labeled;
|
||||
import javafx.scene.control.ListView;
|
||||
|
||||
public class AppController {
|
||||
|
||||
private Calc calc;
|
||||
|
||||
public AppController() {
|
||||
calc = new Calc(0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void switchToSecondary() throws IOException {
|
||||
private ListView<Double> operandsView;
|
||||
|
||||
@FXML
|
||||
private Label operandView;
|
||||
|
||||
@FXML
|
||||
void initialize() {
|
||||
updateOperandsView();
|
||||
}
|
||||
|
||||
private int minOperandCount = 2;
|
||||
|
||||
private void updateOperandsView() {
|
||||
while (calc.getOperandCount() < minOperandCount) {
|
||||
calc.pushOperand(calc.getOperandCount(), 0.0);
|
||||
}
|
||||
List<Double> operands = operandsView.getItems();
|
||||
operands.clear();
|
||||
for (int i = 0; i < minOperandCount; i++) {
|
||||
operands.add(calc.peekOperand(minOperandCount - i - 1));
|
||||
}
|
||||
}
|
||||
|
||||
private String getOperandString() {
|
||||
return operandView.getText();
|
||||
}
|
||||
|
||||
private boolean hasOperand() {
|
||||
return ! getOperandString().isBlank();
|
||||
}
|
||||
|
||||
private double getOperand() {
|
||||
return Double.valueOf(operandView.getText());
|
||||
}
|
||||
|
||||
private void setOperand(String operandString) {
|
||||
operandView.setText(operandString);
|
||||
}
|
||||
|
||||
private void setOperand(double d) {
|
||||
setOperand(Double.toString(d));
|
||||
}
|
||||
|
||||
private void clearOperand() {
|
||||
setOperand("");
|
||||
}
|
||||
|
||||
@FXML
|
||||
void handleEnter() {
|
||||
if (hasOperand()) {
|
||||
calc.pushOperand(getOperand());
|
||||
} else {
|
||||
calc.dup();
|
||||
}
|
||||
clearOperand();
|
||||
updateOperandsView();
|
||||
}
|
||||
|
||||
private void appendToOperand(String s) {
|
||||
setOperand(operandView.getText() + s);
|
||||
}
|
||||
|
||||
@FXML
|
||||
void handleDigit(ActionEvent ae) {
|
||||
if (ae.getSource() instanceof Labeled l) {
|
||||
appendToOperand(l.getText());
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
void handlePoint() {
|
||||
var operandString = getOperandString();
|
||||
if (operandString.contains(".")) {
|
||||
setOperand(operandString.substring(operandString.indexOf(".") + 1));
|
||||
} else {
|
||||
appendToOperand(".");
|
||||
}
|
||||
}
|
||||
|
||||
private void performOperation(boolean swap, BinaryOperator<Double> op) {
|
||||
if (hasOperand()) {
|
||||
calc.pushOperand(getOperand());
|
||||
clearOperand();
|
||||
}
|
||||
if (swap) {
|
||||
calc.swap();
|
||||
}
|
||||
calc.performOperation(op);
|
||||
updateOperandsView();
|
||||
}
|
||||
|
||||
@FXML
|
||||
void handlePi() {
|
||||
setOperand(Math.PI);
|
||||
}
|
||||
|
||||
@FXML
|
||||
void handleOpAdd() {
|
||||
performOperation(false, (op1, op2) -> op1 + op2);
|
||||
}
|
||||
|
||||
@FXML
|
||||
void handleOpSub() {
|
||||
performOperation(true, (op1, op2) -> op1 - op2);
|
||||
}
|
||||
|
||||
@FXML
|
||||
void handleOpMult() {
|
||||
performOperation(false, (op1, op2) -> op1 * op2);
|
||||
}
|
||||
|
||||
@FXML
|
||||
void handleOpDiv() {
|
||||
performOperation(true, (op1, op2) -> op1 / op2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user