Skip to content
Snippets Groups Projects
Commit 324dc23c authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

lf uten tester

parent c892ebfd
No related branches found
No related tags found
No related merge requests found
package groupid;
package app;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
......
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 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
private void switchToSecondary() throws IOException {
void handleOpDiv() {
performOperation(true, (op1, op2) -> op1 / op2);
}
}
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());
}
}
\ No newline at end of file
<?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.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">
<children>
<Label text="Primary View" />
<Button fx:id="primaryButton" text="Switch to Secondary View" onAction="#switchToSecondary"/>
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</VBox>
<GridPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.AppController"
alignment="CENTER" hgap="10.0" vgap="10.0" >
<ListView fx:id="operandsView" prefHeight="80.0"
GridPane.rowIndex="0" GridPane.columnIndex="0" GridPane.columnSpan="4"/>
<Label text="" fx:id="operandView"
GridPane.rowIndex="1" GridPane.columnIndex="0" GridPane.columnSpan="4"/>
<Button text="E&#10;n&#10;t&#10;e&#10;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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment