Explorar el Código

Set up backend project and initial SimpleCalculator class

Harry de Boer hace 5 años
commit
a9e178a9ca

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+*.iml
+.idea
+target

+ 19 - 0
README.md

@@ -0,0 +1,19 @@
+# Simple calculator
+
+This calculator project contains a simple Angular based frontend a Spring based backend.
+
+## Environment
+
+To compile and run this project the following software needs to be installed. Versions used for
+development are listed.
+
+- Maven (3.6.3)
+- Java JDK (OpenJDK 11.0.9.1)
+ 
+## Compiling and running the backend
+
+TODO
+
+## Compiling and running the frontend
+
+TODO

+ 42 - 0
backend/pom.xml

@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>nl.quintor.hdboer.aegoncalc</groupId>
+    <artifactId>aegoncalc</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <maven.compiler.source>11</maven.compiler.source>
+        <maven.compiler.target>11</maven.compiler.target>
+    </properties>
+
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>org.junit</groupId>
+                <artifactId>junit-bom</artifactId>
+                <version>5.7.0</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.hamcrest</groupId>
+            <artifactId>hamcrest</artifactId>
+            <version>2.2</version>
+        </dependency>
+    </dependencies>
+
+</project>

+ 41 - 0
backend/src/main/java/nl/quintor/hdboer/aegoncalc/services/SimpleCalculator.java

@@ -0,0 +1,41 @@
+package nl.quintor.hdboer.aegoncalc.services;
+
+/**
+ * Class SimpleCalculator implements the basic arithmetic functions needed by this project.
+ */
+public class SimpleCalculator {
+
+    /**
+     * Add performs addition of parameters a and b and returns the result.
+     */
+    public double add(int a, int b) {
+        return (double) a + (double) b;
+    }
+
+    /**
+     * Divide performs the division of parameter a by b.
+     */
+    public double subtract(int a, int b) {
+        return (double) a - (double) b;
+    }
+
+    /**
+     * Multiply performs the multiplication of parameter a by b.
+     */
+    public double multiply(int a, int b) {
+        return (double) a * (double) b;
+    }
+
+    /**
+     * Divide performs the division of parameter a by b.
+     *
+     * @throws IllegalArgumentException in case b is zero
+     */
+    public double divide(int a, int b) {
+        if (b == 0) {
+            throw new IllegalArgumentException("cannot divide by 0");
+        }
+        return (double) a / (double) b;
+    }
+
+}

+ 47 - 0
backend/src/test/java/nl/quintor/hdboer/aegoncalc/services/SimpleCalculatorTest.java

@@ -0,0 +1,47 @@
+package nl.quintor.hdboer.aegoncalc.services;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+class SimpleCalculatorTest {
+
+    private final SimpleCalculator calculator = new SimpleCalculator();
+
+    @Test
+    @DisplayName("add: test integers do not over- or underflow")
+    void add() {
+        Assertions.assertAll(
+                () -> Assertions.assertEquals(calculator.add(1, Integer.MAX_VALUE), 2147483648.0),
+                () -> Assertions.assertEquals(calculator.add(-1, Integer.MIN_VALUE), -2147483649.0)
+        );
+    }
+
+    @Test
+    @DisplayName("subtract: test integers do not over- or underflow")
+    void subtract() {
+        Assertions.assertAll(
+                () -> Assertions.assertEquals(calculator.subtract(-1, Integer.MAX_VALUE), -2147483648.0),
+                () -> Assertions.assertEquals(calculator.subtract(1, Integer.MIN_VALUE), 2147483649.0)
+        );
+    }
+
+    @Test
+    @DisplayName("multiply: test integers do not overflow")
+    void multiply() {
+        Assertions.assertEquals(calculator.multiply(2, Integer.MAX_VALUE), 4294967294.0);
+    }
+
+    @Test
+    @DisplayName("divide: test non-integer fraction is retained when dividing")
+    void divide() {
+        Assertions.assertEquals(calculator.divide(10, 8), 1.25);
+    }
+
+    @Test
+    @DisplayName("divide: test division by 0 is handled")
+    void divideByZero() {
+        Assertions.assertThrows(IllegalArgumentException.class, () -> calculator.divide(1, 0));
+    }
+
+}