Skip to content
Snippets Groups Projects
Commit 59132e80 authored by Olav Finne Præsteng Larsen's avatar Olav Finne Præsteng Larsen
Browse files

Oppgave 9:

Implemented more than two numbers at the time in the calc
Added Pos and Neg tests
parent ef456304
No related branches found
No related tags found
No related merge requests found
......@@ -107,6 +107,18 @@
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<jersey.version>2.26</jersey.version>
......
......@@ -23,10 +23,9 @@ public class CalculatorResource {
@Produces(MediaType.TEXT_PLAIN)
public int calculate(String expression){
// Removes all whitespaces
try {
String expressionTrimmed = expression.replaceAll("\\s+", "");
int result = -1;
int result = 0;
/*
* .matches use regex expression to decide if a String matches a given pattern.
* [0-9]+[+][0-9]+ explained:
......@@ -37,10 +36,17 @@ public class CalculatorResource {
* 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);
if (expressionTrimmed.matches("^\\d+(?:\\s*[+]\\s*\\d+)*$")) result = sum(expressionTrimmed);
else if (expressionTrimmed.matches("^\\d+(?:\\s*[-]\\s*\\d+)*$")) result = subtraction(expressionTrimmed);
else if(expressionTrimmed.matches("^\\d+(?:\\s*[*]\\s*\\d+)*$")) result = multiplication(expressionTrimmed);
else if(expressionTrimmed.matches("^\\d+(?:\\s*[/]\\s*\\d+)*$")) result = division(expressionTrimmed);
return result;
}catch (Exception e){
throw new IllegalArgumentException("Wrong format");
}
}
/**
......@@ -51,10 +57,13 @@ public class CalculatorResource {
public int sum(String expression){
String[] split = expression.split("[+]");
int number1 = Integer.parseInt(split[0]);
int number2 = Integer.parseInt(split[1]);
int result = 0;
return number1 + number2;
for(int i = 0; i < split.length; i++) {
result += Integer.parseInt(split[i]);
}
return result;
}
/**
......@@ -65,10 +74,14 @@ public class CalculatorResource {
public int subtraction(String expression){
String[] split = expression.split("[-]");
int number1 = Integer.parseInt(split[0]);
int number2 = Integer.parseInt(split[1]);
return number1 - number2;
int result = Integer.parseInt(split[0]);
for(int i = 1; i < split.length; i++) {
result -= Integer.parseInt(split[i]);
}
return result;
}
/**
......@@ -81,10 +94,13 @@ public class CalculatorResource {
String[] split = equation.split("[*]");
int number1 = Integer.parseInt(split[0]);
int number2 = Integer.parseInt(split[1]);
int result = Integer.parseInt(split[0]);
for(int i = 1; i < split.length; i++) {
result *= Integer.parseInt(split[i]);
return number1 * number2;
}
return result;
}
/**
......@@ -96,9 +112,12 @@ public class CalculatorResource {
public int division(String equation){
String[] split = equation.split("[/]");
int number1 = Integer.parseInt(split[0]);
int number2 = Integer.parseInt(split[1]);
int result = Integer.parseInt(split[0]);
for(int i = 1; i < split.length; i++) {
result /= Integer.parseInt(split[i]);
return number1/number2;
}
return result;
}
}
import org.junit.Test;
import org.junit.jupiter.api.Nested;
import resources.CalculatorResource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class CalculatorResourceTest {
@Test
public void testCalculateManyOperations(){
CalculatorResource calculatorResource = new CalculatorResource();
String expression = "100+400+33";
assertEquals(533, calculatorResource.calculate(expression));
expression = "100-400-33";
assertEquals(-333, calculatorResource.calculate(expression));
expression = "2*10*33";
assertEquals(660, calculatorResource.calculate(expression));
expression = "100/10/10";
assertEquals(1, calculatorResource.calculate(expression));
try{
expression = "10s+2";
calculatorResource.calculate(expression);
}catch (IllegalArgumentException e){
assertTrue(true);
}
}
@Test
public void testCalculate() {
CalculatorResource calculatorResource = new CalculatorResource();
//Sum
String expression = "100+300";
assertEquals(400, calculatorResource.calculate(expression));
//Subtraction
expression = " 300 - 99 ";
assertEquals(201, calculatorResource.calculate(expression));
//Multiplication
expression = "3 * 9";
assertEquals(27, calculatorResource.calculate(expression));
//Division
expression = "6/3";
assertEquals(2, calculatorResource.calculate(expression));
}
@Test
public void testSum(){
CalculatorResource calculatorResource = new CalculatorResource();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment