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
23 changes: 0 additions & 23 deletions ProfCalculator/src/de/uulm/sp/swt/profcalculator/Add.java

This file was deleted.

23 changes: 23 additions & 0 deletions ProfCalculator/src/de/uulm/sp/swt/profcalculator/Logger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.uulm.sp.swt.profcalculator;

public class Logger {

public static final boolean LOGGING = true;

private static Logger logger = new Logger();

private Logger() {
log("Logging enabled");
}

public void log(String message) {
if (LOGGING) {
System.out.println(message);
}
}

public static Logger getLogger() {
return logger;
}

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

public interface Observer {
void update();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
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.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.Value;
import de.uulm.sp.swt.profcalculator.gui.BlueFontGUIFactory;
import de.uulm.sp.swt.profcalculator.gui.GUIFactory;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
Expand All @@ -12,50 +21,59 @@
import javafx.scene.paint.Color;
import javafx.stage.Stage;

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

private final static Value DEFAULT_VALUE = new Value(0);
private Expression expression = new CounterValue(this);

private GUIFactory guiFactory = new BlueFontGUIFactory();

private Add addition = new Add(DEFAULT_VALUE, DEFAULT_VALUE);

private Label errorLabel = new Label();
private Label errorLabel = guiFactory.createLabel();

private TextField inputField = new TextField();

private Button addButton = new Button("+");
private Button additionButton = guiFactory.createButton("+");
private Button multiplicationButton = guiFactory.createButton("*");

private Label resultLabel = new Label();
private Label resultLabel = guiFactory.createLabel();

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

VBox layout = new VBox(10, errorLabel, inputField, addButton, resultLabel);
VBox layout = new VBox(10, errorLabel, inputField, additionButton, multiplicationButton, resultLabel);
layout.setPadding(new Insets(20, 80, 20, 80));
Scene scene = new Scene(layout);

stage.setScene(scene);
stage.show();
addButton.setOnAction(this);
additionButton.setOnAction(this);
multiplicationButton.setOnAction(this);
updateGUI();
}

@Override
public void handle(ActionEvent event) {
try {
int newValue = Integer.parseInt(inputField.getText());
int oldResult = addition.evaluate();
addition = new Add(new Value(oldResult), new Value(newValue));
if (event.getSource() == additionButton) {
expression = new Addition(expression, new Value(newValue));
Logger.getLogger().log("+ " + newValue);
}
else if (event.getSource() == multiplicationButton) {
expression = new Multiplication(expression, new Value(newValue));
Logger.getLogger().log("* " + newValue);
}
expression = new NecessaryBrackets(expression);
updateGUI();
inputField.requestFocus();
} catch (NumberFormatException e) {
errorLabel.setText("\"" + inputField.getText() + "\" is not a valid integer");
}
}

private void updateGUI() {
resultLabel.setText(addition.computeEquation());
public void updateGUI() {
resultLabel.setText(expression.computeEquation());
inputField.setText("");
errorLabel.setText("");
}
Expand All @@ -64,4 +82,9 @@ public static void main(String[] args) {
launch(args);
}

@Override
public void update() {
updateGUI();
}

}
7 changes: 7 additions & 0 deletions ProfCalculator/src/de/uulm/sp/swt/profcalculator/Subject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.uulm.sp.swt.profcalculator;

public interface Subject {
void register(Observer observer);
void unregister(Observer observer);
void notifyObservers();
}
18 changes: 0 additions & 18 deletions ProfCalculator/src/de/uulm/sp/swt/profcalculator/Value.java

This file was deleted.

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

public class Addition extends Expression {

public Expression left;
public Expression right;

public Addition(Expression left, Expression right) {
this.left = left;
this.right = right;
}

public String toString(Expression parent) {
return left.toString(this) + " + " + right.toString(this);
}

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

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

import java.util.LinkedList;

import de.uulm.sp.swt.profcalculator.Observer;
import de.uulm.sp.swt.profcalculator.ProfCalculator;
import de.uulm.sp.swt.profcalculator.Subject;
import javafx.application.Platform;

public class CounterValue extends Value implements Runnable, Subject {

private LinkedList<Observer> observerList = new LinkedList<Observer>();

public CounterValue(ProfCalculator calc) {
super(0);
observerList.add(calc);
new Thread(this).start();
}

@Override
public void run() {
while (value < 10) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
value++;
Platform.runLater(new Runnable() {
@Override
public void run() {
notifyObservers();
}
});
}
}

@Override
public void register(Observer observer) {
observerList.add(observer);
}

@Override
public void unregister(Observer observer) {
observerList.remove(observer);
}

@Override
public void notifyObservers() {
for(int i=0;i<observerList.size();i++) {
observerList.get(i).update();
}
}

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

public abstract class Expression {

public abstract int evaluate();

public abstract String toString(Expression parent);

public String toString() {
return toString(null);
}

public String computeEquation() {
return toString() + " = " + evaluate();
}

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

public class Multiplication extends Expression {

public Expression left;
public Expression right;

public Multiplication(Expression left, Expression right) {
this.left = left;
this.right = right;
}

public String toString(Expression parent) {
return left.toString(this) + " * " + right.toString(this);
}

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

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

public class NecessaryBrackets extends Expression {

private Expression expression;

public NecessaryBrackets(Expression expression) {
this.expression = expression;
}

@Override
public String toString(Expression parent) {
String childString = expression.toString(parent);
if (parent instanceof Multiplication && expression instanceof Addition) {
childString = "(" + childString + ")";
}
return childString;
}

@Override
public int evaluate() {
return expression.evaluate();
}

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

public class Value extends Expression {

Integer value;

public Value(int value) {
this.value = value;
}

public String toString(Expression parent) {
return value.toString();
}

@Override
public int evaluate() {
return value.intValue();
}

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

import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

public class BlueFontGUIFactory implements GUIFactory {

final static Color BLUE = Color.web("#0000AA");

@Override
public Label createLabel() {
Label label = new Label();
label.setTextFill(BLUE);
return label;
}

@Override
public Button createButton(String title) {
Button button = new Button(title);
button.setTextFill(BLUE);
return button;
}

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

import javafx.scene.control.Button;
import javafx.scene.control.Label;

public interface GUIFactory {

Label createLabel();

Button createButton(String title);

}
Loading