Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
devops-workshop
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Erlend Ryan
devops-workshop
Commits
d220df6f
Commit
d220df6f
authored
5 years ago
by
Erlend Ryan
Browse files
Options
Downloads
Patches
Plain Diff
calc
parent
93d8813c
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#72013
passed
5 years ago
Stage: test
Stage: deploy
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/resources/CalculatorResource.java
+47
-34
47 additions, 34 deletions
src/main/java/resources/CalculatorResource.java
with
47 additions
and
34 deletions
src/main/java/resources/CalculatorResource.java
+
47
−
34
View file @
d220df6f
...
...
@@ -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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment