Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
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
java_weekendowa_20221008
Commits
eb1a7b45
Commit
eb1a7b45
authored
Nov 13, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Historia działań
parent
a48016ea
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
45 additions
and
10 deletions
+45
-10
Kalkulator.java
PC26-Spring/src/main/java/com/example/demo/Kalkulator.java
+9
-3
LogikaKalkulatora.java
...ing/src/main/java/com/example/demo/LogikaKalkulatora.java
+18
-7
historia.html
PC26-Spring/src/main/resources/templates/historia.html
+15
-0
kalkulator.html
PC26-Spring/src/main/resources/templates/kalkulator.html
+3
-0
No files found.
PC26-Spring/src/main/java/com/example/demo/Kalkulator.java
View file @
eb1a7b45
...
@@ -13,9 +13,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
...
@@ -13,9 +13,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
// Gdyby w którejś metodzie też był podany adres, byłoby to traktowane jako dalszy ciąg adresu, np. /kalkulator/historia
// Gdyby w którejś metodzie też był podany adres, byłoby to traktowane jako dalszy ciąg adresu, np. /kalkulator/historia
public
class
Kalkulator
{
public
class
Kalkulator
{
/* Wstrzykiwanie zależności (dependency injection).
/* Wstrzykiwanie zależności (dependency injection).
W jednym komponencie chcemy uzywać innego komponentu.
W jednym komponencie chcemy używać innego komponentu.
Zamiast samodzielnie tworzyć obiekty i je ze sobą wiązać (co w większych aplikacjach jest żmudne),
Zamiast samodzielnie tworzyć obiekty i je ze sobą wiązać (co w większych aplikacjach jest żmudne), możemy "poprosić" o to Springa.
możemy "poprosić" o to Springa.
Do pola w tej klasie Spring automatycznie wpisze referencję do obiektu tamtej klasy.
Do pola w tej klasie Spring automatycznie wpisze referencję do obiektu tamtej klasy.
Dowiązania z różnych miejsc aplikacji domyślnie prowadzą do jednego wspólnego obiektu (tego singletona / beana / komponentu).
Dowiązania z różnych miejsc aplikacji domyślnie prowadzą do jednego wspólnego obiektu (tego singletona / beana / komponentu).
...
@@ -38,5 +37,12 @@ public class Kalkulator {
...
@@ -38,5 +37,12 @@ public class Kalkulator {
return
"kalkulator.html"
;
return
"kalkulator.html"
;
}
}
// Ta metoda będzie działać pod adresem /kalkulator/historia
@GetMapping
(
"/historia"
)
public
String
wyswietlHistorie
(
Model
model
)
{
model
.
addAttribute
(
"historia"
,
logika
.
getHistoriaDzialan
());
return
"historia.html"
;
}
}
}
PC26-Spring/src/main/java/com/example/demo/LogikaKalkulatora.java
View file @
eb1a7b45
package
com
.
example
.
demo
;
package
com
.
example
.
demo
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
/* LogikaKalkulatora jest przykładem "komponentu".
/* LogikaKalkulatora jest przykładem "komponentu".
...
@@ -12,14 +16,21 @@ import org.springframework.stereotype.Component;
...
@@ -12,14 +16,21 @@ import org.springframework.stereotype.Component;
@Component
@Component
public
class
LogikaKalkulatora
{
public
class
LogikaKalkulatora
{
private
final
List
<
String
>
historiaDzialan
=
Collections
.
synchronizedList
(
new
ArrayList
<>());
public
long
oblicz
(
long
liczba1
,
long
liczba2
,
String
operacja
)
{
public
long
oblicz
(
long
liczba1
,
long
liczba2
,
String
operacja
)
{
switch
(
operacja
)
{
long
wynik
=
switch
(
operacja
)
{
case
"+"
:
return
liczba1
+
liczba2
;
case
"+"
->
liczba1
+
liczba2
;
case
"-"
:
return
liczba1
-
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"*"
:
return
liczba1
*
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"/"
:
return
liczba1
/
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
default
:
return
0
;
default
->
0
;
}
};
historiaDzialan
.
add
(
String
.
format
(
"%d %s %d = %d"
,
liczba1
,
operacja
,
liczba2
,
wynik
));
return
wynik
;
}
public
List
<
String
>
getHistoriaDzialan
()
{
return
Collections
.
unmodifiableList
(
historiaDzialan
);
}
}
}
}
PC26-Spring/src/main/resources/templates/historia.html
0 → 100644
View file @
eb1a7b45
<!DOCTYPE html>
<html
xmlns:th=
"http://www.thymeleaf.org"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Historia działań
</title>
<link
rel=
"stylesheet"
type=
"text/css"
th:href=
"@{/styl.css}"
href=
"../static/styl.css"
>
</head>
<body>
<h1>
Historia działań kalkulatora
</h1>
<ul>
<li
th:each=
"element: ${historia}"
th:text=
"${element}"
>
2 + 2 = 4
</li>
</ul>
<p><a
th:href=
"@{/kalkulator}"
>
wróć do kalkulatora
</a></p>
</body>
</html>
PC26-Spring/src/main/resources/templates/kalkulator.html
View file @
eb1a7b45
...
@@ -29,5 +29,7 @@ https://www.baeldung.com/thymeleaf-select-option
...
@@ -29,5 +29,7 @@ https://www.baeldung.com/thymeleaf-select-option
[[${param.liczba1}]] [[${param.operacja}]] [[${param.liczba2}]] =
<strong>
[[${wynik}]]
</strong>
[[${param.liczba1}]] [[${param.operacja}]] [[${param.liczba2}]] =
<strong>
[[${wynik}]]
</strong>
</div>
</div>
<div><a
th:href=
"@{/kalkulator/historia}"
>
historia działań
</a></div>
</body>
</body>
</html>
</html>
\ No newline at end of file
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