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: 2 additions & 1 deletion ProfCalculator/.classpath
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?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="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
</classpath>
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
Expand Up @@ -5,6 +5,7 @@
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;
Expand All @@ -20,17 +21,18 @@
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> {

private Expression expression = new CounterValue(this);

private GUIFactory guiFactory = new BlueFontGUIFactory();

private Label errorLabel = guiFactory.createLabel();

private TextField inputField = new TextField();

private Button additionButton = guiFactory.createButton("+");
private Button subButton = guiFactory.createButton("-");
private Button multiplicationButton = guiFactory.createButton("*");

private Label resultLabel = guiFactory.createLabel();
Expand All @@ -40,13 +42,14 @@ 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, subButton, multiplicationButton, resultLabel);
layout.setPadding(new Insets(20, 80, 20, 80));
Scene scene = new Scene(layout);

stage.setScene(scene);
stage.show();
additionButton.setOnAction(this);
subButton.setOnAction(this);
multiplicationButton.setOnAction(this);
updateGUI();
}
Expand All @@ -58,8 +61,10 @@ public void handle(ActionEvent event) {
if (event.getSource() == additionButton) {
expression = new Addition(expression, new Value(newValue));
Logger.getLogger().log("+ " + newValue);
}
else if (event.getSource() == multiplicationButton) {
} else if (event.getSource() == subButton) {
expression = new Sub(expression, new Value(newValue));
Logger.getLogger().log("- " + newValue);
} else if (event.getSource() == multiplicationButton) {
expression = new Multiplication(expression, new Value(newValue));
Logger.getLogger().log("* " + newValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public Addition(Expression left, Expression right) {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public CounterValue(ProfCalculator calc) {

@Override
public void run() {
while (value < 1) {
while (value < 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public Multiplication(Expression left, Expression right) {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ public class NecessaryBrackets extends Expression {
public NecessaryBrackets(Expression expression) {
this.expression = expression;
}

@Override
public String toString(Expression parent) {
String childString = expression.toString(parent);
if (expression instanceof Multiplication && parent instanceof Addition) {
if (parent instanceof Multiplication && (expression instanceof Addition || expression instanceof Sub)) {
childString = "(" + childString + ")";
}
return childString;
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 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) {
if (right.evaluate() < 0) {
return left.toString(this) + " - (" + right.toString(this) + ")";
}
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,79 @@
package de.uulm.sp.swt.profcalculator.expressions.test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import de.uulm.sp.swt.profcalculator.expressions.Addition;
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;

class TNecessaryBracketsWhiteBox {

@Test
void testAdditionWithinMultiplication() {
Expression child = new Addition(new Value(1), new Value(2));
Expression brackets = new NecessaryBrackets(child);
Expression parent = new Multiplication(brackets, new Value(3));
assertEquals(parent.toString(), "(1 + 2) * 3");
}

@Test
void testMultiplicationWithinAddition() {
Expression child = new Multiplication(new Value(1), new Value(2));
Expression brackets = new NecessaryBrackets(child);
Expression parent = new Addition(brackets, new Value(3));
assertEquals(parent.toString(), "1 * 2 + 3");
}

@Test
void testMultiplicationWithinMultiplication() {
Expression child = new Multiplication(new Value(1), new Value(2));
Expression brackets = new NecessaryBrackets(child);
Expression parent = new Multiplication(brackets, new Value(3));
assertEquals(parent.toString(), "1 * 2 * 3");
}

@Test
void testSubtractionWithinMultiplication() {
Expression child = new Sub(new Value(1), new Value(2));
Expression brackets = new NecessaryBrackets(child);
Expression parent = new Multiplication(brackets, new Value(3));
assertEquals(parent.toString(), "(1 - 2) * 3");
}

@Test
void testMultiplicationWithinSubtraction() {
Expression child = new Multiplication(new Value(1), new Value(2));
Expression brackets = new NecessaryBrackets(child);
Expression parent = new Sub(brackets, new Value(3));
assertEquals(parent.toString(), "1 * 2 - 3");
}

@Test
void testMultiplicationWithinSubtractionNegativeNumber() {
Expression child = new Multiplication(new Value(1), new Value(2));
Expression brackets = new NecessaryBrackets(child);
Expression parent = new Sub(brackets, new Value(-3));
assertEquals(parent.toString(), "1 * 2 - (-3)");
}

@Test
void testSubtractionWithinMultiplicationWithNegativeNumber() {
Expression child = new Sub(new Value(1), new Value(2));
Expression brackets = new NecessaryBrackets(child);
Expression parent = new Multiplication(brackets, new Value(-3));
assertEquals(parent.toString(), "(1 - 2) * (-3)");
}

@Test
void testMultiplicationWithinAdditionWithNegativeNumber() {
Expression child = new Multiplication(new Value(1), new Value(2));
Expression brackets = new NecessaryBrackets(child);
Expression parent = new Addition(brackets, new Value(-3));
assertEquals(parent.toString(), "1 * 2 + (-3)");
}
}