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

calc

parent 93d8813c
Branches
No related tags found
No related merge requests found
Pipeline #72013 passed
......@@ -13,8 +13,9 @@ import javax.ws.rs.core.MediaType;
public class CalculatorResource {
/**
* Method handling HTTP POST requests. The calculated answer to the expression will be sent to the client as
* plain/text.
* 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
*/
......@@ -29,66 +30,78 @@ public class CalculatorResource {
/*
* .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
* [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);
else if(expressionTrimmed.matches("[0-9]+[*][0-9]+")) result = multiplication(expressionTrimmed);
else if(expressionTrimmed.matches("[0-9]+[/][0-9]+")) result = division(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);
/* System.out.println(result); */
return result;
}
/**
* 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) {
System.out.println(expression);
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 number3 = Integer.parseInt(split[2]);
return number1 + number2;
return number1 + number2 + number3; */
}
/**
* Method used to calculate a subtraction expression.
*
* @param expression the expression to be calculated as a String
* @return the answer as an int
*/
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 sum = 0;
for (String tall: split) {
sum -= Integer.parseInt(tall);
}
return sum;
}
private int multiplication(String equation) {
String[] split = equation.split("[*]");
int number1 = Integer.parseInt(split[0]);
int number2 = Integer.parseInt(split[1]);
return number1 * number2;
int sum = 0;
for (String tall: split) {
sum *= Integer.parseInt(tall);
}
return sum;
}
private int division(String equation) {
String[] split = equation.split("[/]");
int number1 = Integer.parseInt(split[0]);
int number2 = Integer.parseInt(split[1]);
return number1 / number2;
int sum = 0;
for (String tall: split) {
sum /= Integer.parseInt(tall);
}
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