Skip to content
Snippets Groups Projects
Commit d220df6f authored by Erlend Ryan's avatar Erlend Ryan
Browse files

calc

parent 93d8813c
No related branches found
No related tags found
No related merge requests found
Pipeline #72013 passed
...@@ -13,8 +13,9 @@ import javax.ws.rs.core.MediaType; ...@@ -13,8 +13,9 @@ import javax.ws.rs.core.MediaType;
public class CalculatorResource { public class CalculatorResource {
/** /**
* Method handling HTTP POST requests. The calculated answer to the expression will be sent to the client as * Method handling HTTP POST requests. The calculated answer to the expression
* plain/text. * will be sent to the client as plain/text.
*
* @param expression the expression to be solved 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 * @return solution to the expression as plain/text or -1 on error
*/ */
...@@ -29,66 +30,78 @@ public class CalculatorResource { ...@@ -29,66 +30,78 @@ public class CalculatorResource {
/* /*
* .matches use regex expression to decide if a String matches a given pattern. * .matches use regex expression to decide if a String matches a given pattern.
* [0-9]+[+][0-9]+ explained: * [0-9]+[+][0-9]+ explained: [0-9]+: a number from 0-9 one or more times. For
* [0-9]+: a number from 0-9 one or more times. For example 1, 12 and 123. * example 1, 12 and 123. [+]: the operator + one time. The total regex
* [+]: the operator + one time. * expression accepts for example: 12+34, 1+2, 10000+1000
* The total regex expression accepts for example:
* 12+34,
* 1+2,
* 10000+1000
*/ */
if(expressionTrimmed.matches("[0-9]+[+][0-9]+")) result = sum(expressionTrimmed); if (expressionTrimmed.matches("^\\d+(?:\\s*[+]\\s*\\d+)*$"))
else if(expressionTrimmed.matches("[0-9]+[-][0-9]+")) result = subtraction(expressionTrimmed); result = sum(expressionTrimmed);
else if(expressionTrimmed.matches("[0-9]+[*][0-9]+")) result = multiplication(expressionTrimmed); else if (expressionTrimmed.matches("^\\d+(?:\\s*[-]\\s*\\d+)*$"))
else if(expressionTrimmed.matches("[0-9]+[/][0-9]+")) result = division(expressionTrimmed); 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);
/* System.out.println(result); */
return result; return result;
} }
/** /**
* Method used to calculate a sum expression. * Method used to calculate a sum expression.
*
* @param expression the equation to be calculated as a String * @param expression the equation to be calculated as a String
* @return the answer as an int * @return the answer as an int
*/ */
public int sum(String expression) { public int sum(String expression) {
System.out.println(expression);
String[] split = expression.split("[+]"); String[] split = expression.split("[+]");
int number1 = Integer.parseInt(split[0]); int sum = 0;
for (String tall: split) {
sum += Integer.parseInt(tall);
}
return sum;
/* int number1 = Integer.parseInt(split[0]);
int number2 = Integer.parseInt(split[1]); int number2 = Integer.parseInt(split[1]);
int number3 = Integer.parseInt(split[2]);
return number1 + number2; return number1 + number2 + number3; */
} }
/** /**
* Method used to calculate a subtraction expression. * Method used to calculate a subtraction expression.
*
* @param expression the expression to be calculated as a String * @param expression the expression to be calculated as a String
* @return the answer as an int * @return the answer as an int
*/ */
public int subtraction(String expression) { public int subtraction(String expression) {
String[] split = expression.split("[-]"); String[] split = expression.split("[-]");
int number1 = Integer.parseInt(split[0]); int sum = 0;
int number2 = Integer.parseInt(split[1]); for (String tall: split) {
sum -= Integer.parseInt(tall);
return number1 - number2; }
return sum;
} }
private int multiplication(String equation) { private int multiplication(String equation) {
String[] split = equation.split("[*]"); String[] split = equation.split("[*]");
int number1 = Integer.parseInt(split[0]); int sum = 0;
int number2 = Integer.parseInt(split[1]); for (String tall: split) {
sum *= Integer.parseInt(tall);
return number1 * number2; }
return sum;
} }
private int division(String equation) { private int division(String equation) {
String[] split = equation.split("[/]"); String[] split = equation.split("[/]");
int number1 = Integer.parseInt(split[0]); int sum = 0;
int number2 = Integer.parseInt(split[1]); for (String tall: split) {
sum /= Integer.parseInt(tall);
return number1 / number2; }
return sum;
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment