Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ProfCalculator/.classpath
.project
# Compiled class file
*.class

Expand All @@ -21,3 +23,101 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*


# Created by https://www.toptal.com/developers/gitignore/api/java,eclipse
# Edit at https://www.toptal.com/developers/gitignore?templates=java,eclipse

### Eclipse ###
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/*
.loadpath
.recommenders

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# PyDev specific (Python IDE for Eclipse)
*.pydevproject

# CDT-specific (C/C++ Development Tooling)
.cproject

# CDT- autotools
.autotools

# Java annotation processor (APT)
.factorypath

# PDT-specific (PHP Development Tools)
.buildpath

# sbteclipse plugin
.target

# Tern plugin
.tern-project

# TeXlipse plugin
.texlipse

# STS (Spring Tool Suite)
.springBeans

# Code Recommenders
.recommenders/

# Annotation Processing
.apt_generated/
.apt_generated_test/

# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet

# Uncomment this line if you wish to ignore the project description file.
# Typically, this file would be tracked if it contains build/dependency configurations:
#.project

### Eclipse Patch ###
# Spring Boot Tooling
.sts4-cache/

### Java ###
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# End of https://www.toptal.com/developers/gitignore/api/java,eclipse

3 changes: 1 addition & 2 deletions ProfCalculator/.classpath
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-13">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/java-11-openjdk">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JavaFx"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JavaFX"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
Expand Down
9 changes: 5 additions & 4 deletions ProfCalculator/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=13
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=13
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=13
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=11
6 changes: 3 additions & 3 deletions ProfCalculator/src/de/uulm/sp/swt/profcalculator/Logger.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package de.uulm.sp.swt.profcalculator;

public class Logger {

public static final boolean LOGGING = true;

private static Logger logger = new Logger();

private Logger() {
Expand All @@ -15,7 +15,7 @@ public void log(String message) {
System.out.println(message);
}
}

public static Logger getLogger() {
return logger;
}
Expand Down
40 changes: 40 additions & 0 deletions ProfCalculator/src/de/uulm/sp/swt/profcalculator/Observable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.uulm.sp.swt.profcalculator;

import java.util.LinkedList;

import de.uulm.sp.swt.profcalculator.expressions.Value;

public class Observable extends Value {

private LinkedList<Observer> observers = new LinkedList<>();
private boolean changed = false;

public Observable(double value) {
super(value);

}

public void addObserver(Observer o) {
observers.add(o);
}

public void removeObserver(Observer o) {
observers.remove(o);
}

public void notifyObservers() {
if (hasChanged()) {
for (Observer o : observers) {
o.update();
}
}
}

public boolean hasChanged() {
return changed;
}

public void setChanged() {
changed = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.uulm.sp.swt.profcalculator;

import javafx.application.Application;

public abstract class Observer extends Application {
public abstract void update();
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package de.uulm.sp.swt.profcalculator;

import de.uulm.sp.swt.profcalculator.expressions.Addition;
import de.uulm.sp.swt.profcalculator.expressions.CounterValue;
import de.uulm.sp.swt.profcalculator.expressions.Div;
import de.uulm.sp.swt.profcalculator.expressions.CounterValue;
import de.uulm.sp.swt.profcalculator.expressions.Expression;
import de.uulm.sp.swt.profcalculator.expressions.Multiplication;
import de.uulm.sp.swt.profcalculator.expressions.NecessaryBrackets;
import de.uulm.sp.swt.profcalculator.expressions.Sub;
import de.uulm.sp.swt.profcalculator.expressions.Value;
import de.uulm.sp.swt.profcalculator.gui.BlueFontGUIFactory;
import de.uulm.sp.swt.profcalculator.gui.GUIFactory;
import javafx.application.Application;
import de.uulm.sp.swt.profcalculator.gui.*;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
Expand All @@ -21,54 +21,74 @@
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ProfCalculator extends Application implements EventHandler<ActionEvent> {
public class ProfCalculator extends Observer implements EventHandler<ActionEvent> {

private CounterValue subject = new CounterValue();

private Expression expression = new CounterValue(this);
private GUIFactory guiFactory = new BlueFontGUIFactory();
private Expression expression = subject;

private GUIFactory guiFactory = new TerminalStyleFactory();

private Label errorLabel = guiFactory.createLabel();

private TextField inputField = new TextField();
private TextField inputField = guiFactory.createTextField();

private Button additionButton = guiFactory.createButton("+");
private Button subtractionButton = guiFactory.createButton("-");
private Button multiplicationButton = guiFactory.createButton("*");
private Button divisionButton = guiFactory.createButton("/");

private Label resultLabel = guiFactory.createLabel();

public ProfCalculator() {
subject.addObserver(this);
}

@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Professorial Calculator");
errorLabel.setTextFill(Color.web("#AA0000"));

VBox layout = new VBox(10, errorLabel, inputField, additionButton, multiplicationButton, resultLabel);
VBox layout = new VBox(10, errorLabel, inputField, additionButton, subtractionButton, multiplicationButton,
divisionButton, resultLabel);
layout.setPadding(new Insets(20, 80, 20, 80));
layout.setStyle(guiFactory.getBackgroundColorStyle());
Scene scene = new Scene(layout);

stage.setScene(scene);
stage.show();
additionButton.setOnAction(this);
subtractionButton.setOnAction(this);
multiplicationButton.setOnAction(this);
divisionButton.setOnAction(this);

updateGUI();
}

@Override
public void handle(ActionEvent event) {
try {
int newValue = Integer.parseInt(inputField.getText());
if (event.getSource() == additionButton) {
double newValue = Double.parseDouble(inputField.getText());
Button pressedButton = (Button) event.getSource();

if (pressedButton == additionButton) {
expression = new Addition(expression, new Value(newValue));
Logger.getLogger().log("+ " + newValue);
}
else if (event.getSource() == multiplicationButton) {
} else if (pressedButton == subtractionButton) {
expression = new Sub(expression, new Value(newValue));
} else if (pressedButton == multiplicationButton) {
expression = new Multiplication(expression, new Value(newValue));
Logger.getLogger().log("* " + newValue);
} else if (pressedButton == divisionButton) {
expression = new Div(expression, new Value(newValue));
}

Logger.getLogger().log(pressedButton.getText() + " " + newValue);
expression = new NecessaryBrackets(expression);
updateGUI();
inputField.requestFocus();
} catch (NumberFormatException e) {
errorLabel.setText("\"" + inputField.getText() + "\" is not a valid integer");
} catch (ArithmeticException e) {
errorLabel.setText(e.getMessage());
}
}

Expand All @@ -82,4 +102,17 @@ public static void main(String[] args) {
launch(args);
}

@Override
public void update() {
expression = new Value(subject.getState());

Platform.runLater(new Runnable() {
@Override
public void run() {
updateGUI();

}
});

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.uulm.sp.swt.profcalculator.expressions;

public class Addition extends Expression {

public Expression left;
public Expression right;

Expand All @@ -14,8 +14,8 @@ public String toString(Expression parent) {
return left.toString(this) + " + " + right.toString(this);
}

public int evaluate() {
public double evaluate() {
return left.evaluate() + right.evaluate();
}

}
Loading