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
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
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
Maiken Brechan
devops-workshop
Commits
384191f8
Commit
384191f8
authored
4 years ago
by
Martin Dolmen Helmersen
Browse files
Options
Downloads
Patches
Plain Diff
progress kalkulator "Task 9"
parent
ecd2c151
No related branches found
No related tags found
No related merge requests found
Pipeline
#114712
passed
4 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
+32
-17
32 additions, 17 deletions
src/main/java/resources/CalculatorResource.java
with
32 additions
and
17 deletions
src/main/java/resources/CalculatorResource.java
+
32
−
17
View file @
384191f8
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
;
}
}
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