Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20240528-BJava
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
20240528-BJava
Commits
1ae1f2f0
Commit
1ae1f2f0
authored
Jun 26, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Utworzenie klasy LogikaKalkulatora
parent
b75afa1a
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
7 deletions
+26
-7
KalkulatorController.java
...ing/src/main/java/alx/aplikacja/KalkulatorController.java
+4
-7
LogikaKalkulatora.java
...Spring/src/main/java/alx/aplikacja/LogikaKalkulatora.java
+22
-0
No files found.
PC32-Spring/src/main/java/alx/aplikacja/KalkulatorController.java
View file @
1ae1f2f0
package
alx
.
aplikacja
;
package
alx
.
aplikacja
;
import
org.apache.commons.lang3.ObjectUtils
;
import
org.apache.commons.lang3.ObjectUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
@Controller
@Controller
public
class
KalkulatorController
{
public
class
KalkulatorController
{
@Autowired
private
LogikaKalkulatora
logikaKalkulatora
;
@GetMapping
(
"/kalkulator"
)
@GetMapping
(
"/kalkulator"
)
public
String
dzialaj
(
Long
liczba1
,
Long
liczba2
,
String
operacja
,
Model
model
)
{
public
String
dzialaj
(
Long
liczba1
,
Long
liczba2
,
String
operacja
,
Model
model
)
{
if
(
ObjectUtils
.
allNotNull
(
liczba1
,
liczba2
,
operacja
))
{
if
(
ObjectUtils
.
allNotNull
(
liczba1
,
liczba2
,
operacja
))
{
long
wynik
=
switch
(
operacja
)
{
long
wynik
=
logikaKalkulatora
.
oblicz
(
liczba1
,
liczba2
,
operacja
);
case
"+"
->
liczba1
+
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
default
->
0
;
};
model
.
addAttribute
(
"wynik"
,
wynik
);
model
.
addAttribute
(
"wynik"
,
wynik
);
}
}
return
"kalkulator.html"
;
return
"kalkulator.html"
;
...
...
PC32-Spring/src/main/java/alx/aplikacja/LogikaKalkulatora.java
0 → 100644
View file @
1ae1f2f0
package
alx
.
aplikacja
;
import
org.springframework.stereotype.Component
;
/* Adnotacja @Component (albo alternatywnie @Service, @Repository) powoduje, że:
1. na starcie aplikacji Spring twozy obiekt tej klasy i go "rejestruje" jako komponent / bean
2. gdy inne klasy będą się odpowiednio odwoływać do tej klasy, to Spring "wstrzyknie" referencję do tego obiektu
*/
@Component
public
class
LogikaKalkulatora
{
public
long
oblicz
(
long
liczba1
,
long
liczba2
,
String
operacja
)
{
return
switch
(
operacja
)
{
case
"+"
->
liczba1
+
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
default
->
0
;
};
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment