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
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package de.uulm.sp.swt.profcalculator;

import de.uulm.sp.swt.profcalculator.expressions.Addition;
import de.uulm.sp.swt.profcalculator.expressions.Div;
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 de.uulm.sp.swt.profcalculator.gui.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
Expand All @@ -25,14 +26,16 @@ public class ProfCalculator extends Application implements EventHandler<ActionEv

private Expression expression = DEFAULT_VALUE;

private GUIFactory guiFactory = new BlueFontGUIFactory();
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();

Expand All @@ -41,34 +44,43 @@ 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());
double newValue = Double.parseDouble(inputField.getText());
if (event.getSource() == additionButton) {
expression = new Addition(expression, new Value(newValue));
Logger.getLogger().log("+ " + newValue);
}
else if (event.getSource() == multiplicationButton) {
} else if (event.getSource() == subtractionButton) {
expression = new Sub(expression, new Value(newValue));
} else if (event.getSource() == multiplicationButton) {
expression = new Multiplication(expression, new Value(newValue));
Logger.getLogger().log("* " + newValue);
} else if (event.getSource() == divisionButton) {
expression = new Div(expression, new Value(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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public String toString(Expression parent) {
return left.toString(this) + " + " + right.toString(this);
}

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

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

public class Div extends Expression {

public Expression left;
public Expression right;

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

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

public double evaluate() {
if (right.evaluate() == 0)
throw new ArithmeticException("Division by zero is not allowed!");
return left.evaluate() / right.evaluate();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public abstract class Expression {

public abstract int evaluate();
public abstract double evaluate();

public abstract String toString(Expression parent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public String toString(Expression parent) {
return left.toString(this) + " * " + right.toString(this);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ public NecessaryBrackets(Expression expression) {
@Override
public String toString(Expression parent) {
String childString = expression.toString(parent);
if (parent instanceof Multiplication && expression instanceof Addition) {
if ((parent instanceof Multiplication && (expression instanceof Addition || expression instanceof Sub)) ||
(parent instanceof Div && (expression instanceof Addition || expression instanceof Sub))) {
childString = "(" + childString + ")";
}
return childString;
}

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

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

public class Sub extends Expression {

public Expression left;
public Expression right;

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

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

public class Value extends Expression {

Integer value;
Double value;

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

Expand All @@ -13,8 +13,8 @@ public String toString(Expression parent) {
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

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

public class BlueFontGUIFactory implements GUIFactory {

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

public final static String BLUE_HEX_CODE = "#0000aa";

@Override
public Label createLabel() {
Label label = new Label();
Expand All @@ -23,4 +25,17 @@ public Button createButton(String title) {
return button;
}

@Override
public TextField createTextField() {
TextField textField = new TextField();
textField.setStyle("-fx-text-fill: " + BLUE_HEX_CODE + ";");
return textField;
}

@Override
public String getBackgroundColorStyle() {
//this way the default values are used
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

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

public interface GUIFactory {

Label createLabel();

Button createButton(String title);

TextField createTextField();

String getBackgroundColorStyle();

}
Loading