From 0cc8ff0305f0d849faa2b5744ba7735ed72b2ecf Mon Sep 17 00:00:00 2001
From: Martin Dolmen Helmersen <marthelm@stud.ntnu.no>
Date: Mon, 8 Mar 2021 15:36:30 +0100
Subject: [PATCH] Fix in methods for multiplication and division

---
 .../java/resources/CalculatorResource.java    | 86 ++++++++++---------
 1 file changed, 44 insertions(+), 42 deletions(-)

diff --git a/src/main/java/resources/CalculatorResource.java b/src/main/java/resources/CalculatorResource.java
index 7cbe496..1118046 100644
--- a/src/main/java/resources/CalculatorResource.java
+++ b/src/main/java/resources/CalculatorResource.java
@@ -12,60 +12,62 @@ import javax.ws.rs.core.MediaType;
 @Path("/calculator")
 public class CalculatorResource {
 
-    /**
-     * Method handling HTTP POST requests. The calculated answer to the expression will be sent to the client as
-     * plain/text.
-     * @param expression the expression to be solved as plain/text.
-     * @return solution to the expression as plain/text or -1 on error
+/**
+ * Method handling HTTP POST requests. The calculated answer to the expression will be sent to the client as
+ * plain/text.
+ *
+ * @param expression the expression to be solved as plain/text.
+ * @return solution to the expression as plain/text or -1 on error
+ */
+@POST
+@Consumes(MediaType.TEXT_PLAIN)
+@Produces(MediaType.TEXT_PLAIN)
+public int calculate(String expression) {
+    // Removes all whitespaces
+    String expressionTrimmed = expression.replaceAll("\\s+", "");
+
+    int result = -1;
+
+    /*
+     * .matches use regex expression to decide if a String matches a given pattern.
+     * [0-9]+[+][0-9]+ explained:
+     * [0-9]+: a number from 0-9 one or more times. For example 1, 12 and 123.
+     * [+]: the operator + one time.
+     * The total regex expression accepts for example:
+     * 12+34,
+     * 1+2,
+     * 10000+1000
      */
-    @POST
-    @Consumes(MediaType.TEXT_PLAIN)
-    @Produces(MediaType.TEXT_PLAIN)
-    public int calculate(String expression){
-        // Removes all whitespaces
-        String expressionTrimmed = expression.replaceAll("\\s+","");
-
-        int result = -1;
-
-        /*
-         * .matches use regex expression to decide if a String matches a given pattern.
-         * [0-9]+[+][0-9]+ explained:
-         * [0-9]+: a number from 0-9 one or more times. For example 1, 12 and 123.
-         * [+]: the operator + one time.
-         * The total regex expression accepts for example:
-         * 12+34,
-         * 1+2,
-         * 10000+1000
-         */
-        if(expressionTrimmed.matches("[0-9]+[+][0-9]+")) result = sum(expressionTrimmed);
-        else if(expressionTrimmed.matches("[0-9]+[-][0-9]+")) result = subtraction(expressionTrimmed);
-
-        return result;
-    }
+    if (expressionTrimmed.matches("[0-9]+[+][0-9]+")) result = sum(expressionTrimmed);
+    else if (expressionTrimmed.matches("[0-9]+[-][0-9]+")) result = subtraction(expressionTrimmed);
 
-    private int multiplication(String equation){
-        String[] split = equation.split("*");
+    return result;
+}
 
-        int number1 = Integer.parseInt(split[0]);
-        int number2 = Integer.parseInt(split[1]);
+    public int multiplication(String equation) {
+    String[] split = equation.split("*");
 
-        return number1*number2;
-    }
+    int number1 = Integer.parseInt(split[0]);
+    int number2 = Integer.parseInt(split[1]);
 
-    private int division(String equation){
-        String[] split = equation.split("*");
+    return number1 * number2;
+}
 
-        int number1 = Integer.parseInt(split[0]);
-        int number2 = Integer.parseInt(split[1]);
+    public int division(String equation) {
+    String[] split = equation.split("/");
+
+    int number1 = Integer.parseInt(split[0]);
+    int number2 = Integer.parseInt(split[1]);
 
-        return number1/number2;
+    return number1 / number2;
+}
 
     /**
      * Method used to calculate a sum expression.
      * @param expression the equation to be calculated as a String
      * @return the answer as an int
      */
-    public int sum(String expression){
+    public int sum (String expression){
         String[] split = expression.split("[+]");
 
         int number1 = Integer.parseInt(split[0]);
@@ -79,7 +81,7 @@ public class CalculatorResource {
      * @param expression the expression to be calculated as a String
      * @return the answer as an int
      */
-    public int subtraction(String expression){
+    public int subtraction (String expression){
         String[] split = expression.split("[-]");
 
         int number1 = Integer.parseInt(split[0]);
-- 
GitLab