Skip to content
Snippets Groups Projects
Commit 384191f8 authored by Martin Dolmen Helmersen's avatar Martin Dolmen Helmersen
Browse files

progress kalkulator "Task 9"

parent ecd2c151
No related branches found
No related tags found
No related merge requests found
Pipeline #114712 passed
package resources;
import org.graalvm.compiler.hotspot.stubs.DivisionByZeroExceptionStub;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
......@@ -38,28 +40,37 @@ public int calculate(String expression) {
* 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;
}
public int multiplication(String equation) {
String[] split = equation.split("[*]");
int sum = 0;
for (int i = 0; i < split.length; i++) {
sum *= Integer.parseInt(split[i]);
}
int number1 = Integer.parseInt(split[0]);
int number2 = Integer.parseInt(split[1]);
return number1 * number2;
return sum;
}
public int division(String equation){
String[] split = equation.split("[/]");
try{
int sum = 0;
for (int i = 0; i < split.length; i++) {
sum = Integer.parseInt(split[i]);
}
int number1 = Integer.parseInt(split[0]);
int number2 = Integer.parseInt(split[1]);
return sum;
}catch (DivisionByZeroExceptionStub e){
}
return number1 / number2;
}
/**
......@@ -69,11 +80,13 @@ public int calculate(String expression) {
*/
public int sum (String expression){
String[] split = expression.split("[+]");
int sum = Integer.parseInt(split[0]);
for (int i = 1; i < split.length; i++) {
sum += Integer.parseInt(split[i]);
int number1 = Integer.parseInt(split[0]);
int number2 = Integer.parseInt(split[1]);
}
return number1 + number2;
return sum;
}
/**
......@@ -83,10 +96,12 @@ public int calculate(String expression) {
*/
public int subtraction (String expression){
String[] split = expression.split("[-]");
int sum = Integer.parseInt(split[0]);
for (int i = 1; i < split.length; i++) {
sum -= Integer.parseInt(split[i]);
}
int number1 = Integer.parseInt(split[0]);
int number2 = Integer.parseInt(split[1]);
return number1 - number2;
return sum;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment