lf uten tester
This commit is contained in:
parent
c892ebfdec
commit
324dc23cf7
|
@ -1,4 +1,4 @@
|
||||||
package groupid;
|
package app;
|
||||||
|
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
|
|
|
@ -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.fxml.FXML;
|
||||||
|
import javafx.scene.control.Label;
|
||||||
|
import javafx.scene.control.Labeled;
|
||||||
|
import javafx.scene.control.ListView;
|
||||||
|
|
||||||
public class AppController {
|
public class AppController {
|
||||||
|
|
||||||
|
private Calc calc;
|
||||||
|
|
||||||
|
public AppController() {
|
||||||
|
calc = new Calc(0.0, 0.0, 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
@FXML
|
@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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
package app;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.BinaryOperator;
|
||||||
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
|
public class Calc {
|
||||||
|
|
||||||
|
private List<Double> operandStack = new ArrayList<>();
|
||||||
|
|
||||||
|
public Calc(Double... operands) {
|
||||||
|
operandStack.addAll(List.of(operands));
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOperandCount() {
|
||||||
|
return operandStack.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pushOperand(int n, double d) {
|
||||||
|
operandStack.add(operandStack.size() - n, d);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pushOperand(double d) {
|
||||||
|
pushOperand(0, d);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double peekOperand(int n) {
|
||||||
|
return operandStack.get(operandStack.size() - n - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double peekOperand() {
|
||||||
|
return peekOperand(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double popOperand(int n) {
|
||||||
|
return operandStack.remove(operandStack.size() - n - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double popOperand() {
|
||||||
|
return popOperand(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double performOperation(UnaryOperator<Double> op) {
|
||||||
|
var op1 = popOperand();
|
||||||
|
var result = op.apply(op1);
|
||||||
|
pushOperand(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double performOperation(BinaryOperator<Double> op) {
|
||||||
|
var op1 = popOperand();
|
||||||
|
var op2 = popOperand();
|
||||||
|
var result = op.apply(op1, op2);
|
||||||
|
pushOperand(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void swap() {
|
||||||
|
var op1 = popOperand();
|
||||||
|
var op2 = popOperand();
|
||||||
|
pushOperand(op1);
|
||||||
|
pushOperand(op2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dup() {
|
||||||
|
pushOperand(peekOperand());
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,16 +1,52 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<?import javafx.scene.layout.VBox?>
|
<?import javafx.scene.layout.GridPane?>
|
||||||
<?import javafx.scene.control.Label?>
|
<?import javafx.scene.control.Label?>
|
||||||
<?import javafx.scene.control.Button?>
|
<?import javafx.scene.control.Button?>
|
||||||
<?import javafx.geometry.Insets?>
|
<?import javafx.scene.control.ListView?>
|
||||||
|
|
||||||
<VBox alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.AppController">
|
<GridPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.AppController"
|
||||||
<children>
|
alignment="CENTER" hgap="10.0" vgap="10.0" >
|
||||||
<Label text="Primary View" />
|
|
||||||
<Button fx:id="primaryButton" text="Switch to Secondary View" onAction="#switchToSecondary"/>
|
<ListView fx:id="operandsView" prefHeight="80.0"
|
||||||
</children>
|
GridPane.rowIndex="0" GridPane.columnIndex="0" GridPane.columnSpan="4"/>
|
||||||
<padding>
|
<Label text="" fx:id="operandView"
|
||||||
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
|
GridPane.rowIndex="1" GridPane.columnIndex="0" GridPane.columnSpan="4"/>
|
||||||
</padding>
|
|
||||||
</VBox>
|
<Button text="E n t e r" onAction="#handleEnter"
|
||||||
|
GridPane.rowIndex="2" GridPane.columnIndex="3" GridPane.rowSpan="4"/>
|
||||||
|
|
||||||
|
<Button text="7" onAction="#handleDigit"
|
||||||
|
GridPane.rowIndex="2" GridPane.columnIndex="0"/>
|
||||||
|
<Button text="8" onAction="#handleDigit"
|
||||||
|
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"
|
||||||
|
GridPane.rowIndex="5" GridPane.columnIndex="1"/>
|
||||||
|
<Button text="π" onAction="#handlePi"
|
||||||
|
GridPane.rowIndex="5" GridPane.columnIndex="2"/>
|
||||||
|
|
||||||
|
<Button text="+" onAction="#handleOpAdd"
|
||||||
|
GridPane.rowIndex="6" GridPane.columnIndex="0"/>
|
||||||
|
<Button text="-" onAction="#handleOpSub"
|
||||||
|
GridPane.rowIndex="6" GridPane.columnIndex="1"/>
|
||||||
|
<Button text="*" onAction="#handleOpMult"
|
||||||
|
GridPane.rowIndex="6" GridPane.columnIndex="2"/>
|
||||||
|
<Button text="/" onAction="#handleOpDiv"
|
||||||
|
GridPane.rowIndex="6" GridPane.columnIndex="3"/>
|
||||||
|
</GridPane>
|
||||||
|
|
Loading…
Reference in New Issue