Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
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
javab_20230617
Commits
e29ae9eb
Commit
e29ae9eb
authored
Jul 15, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Kursy walut - 1. wersja działająca napisana bardzo brzydko
parent
82a51b9f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
6 deletions
+68
-6
WalutyController.java
...c/main/java/com/example/demo/waluty/WalutyController.java
+60
-6
waluty.html
PC25-Spring/src/main/resources/templates/waluty.html
+8
-0
No files found.
PC25-Spring/src/main/java/com/example/demo/waluty/WalutyController.java
View file @
e29ae9eb
package
com
.
example
.
demo
.
waluty
;
package
com
.
example
.
demo
.
waluty
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigDecimal
;
import
java.net.URL
;
import
java.time.LocalDate
;
import
javax.xml.parsers.DocumentBuilder
;
import
javax.xml.parsers.DocumentBuilderFactory
;
import
javax.xml.parsers.ParserConfigurationException
;
import
javax.xml.xpath.XPath
;
import
javax.xml.xpath.XPathConstants
;
import
javax.xml.xpath.XPathExpressionException
;
import
javax.xml.xpath.XPathFactory
;
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
;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Node
;
import
org.w3c.dom.NodeList
;
import
org.xml.sax.SAXException
;
@Controller
@Controller
public
class
WalutyController
{
public
class
WalutyController
{
@GetMapping
(
"/waluty"
)
@GetMapping
(
"/waluty"
)
public
String
wyswietlWaluty
(
String
data
,
Model
model
)
{
public
String
wyswietlWaluty
(
String
data
,
Model
model
)
{
TabelaWalut
tabela
=
pobierzTabele
(
data
);
TabelaWalut
tabela
=
null
;
String
adres
=
"https://api.nbp.pl/api/exchangerates/tables/a"
;
if
(
data
!=
null
)
{
adres
+=
"/"
+
data
;
}
adres
+=
"?format=xml"
;
try
{
DocumentBuilderFactory
dbf
=
DocumentBuilderFactory
.
newInstance
();
DocumentBuilder
db
=
dbf
.
newDocumentBuilder
();
Document
doc
=
null
;
URL
url
=
new
URL
(
adres
);
try
(
InputStream
in
=
url
.
openStream
())
{
doc
=
db
.
parse
(
in
);
}
XPathFactory
xpf
=
XPathFactory
.
newInstance
();
XPath
xpath
=
xpf
.
newXPath
();
// String numer = xpath.evaluate(
// "/ArrayOfExchangeRatesTable/ExchangeRatesTable/No", doc);
String
nazwaTabeli
=
xpath
.
evaluate
(
"//Table"
,
doc
);
String
numerTabeli
=
xpath
.
evaluate
(
"//No"
,
doc
);
LocalDate
dataTabeli
=
LocalDate
.
parse
(
xpath
.
evaluate
(
"//EffectiveDate"
,
doc
));
tabela
=
new
TabelaWalut
(
nazwaTabeli
,
numerTabeli
,
dataTabeli
);
NodeList
rates
=
(
NodeList
)
xpath
.
evaluate
(
"//Rate"
,
doc
,
XPathConstants
.
NODESET
);
final
int
n
=
rates
.
getLength
();
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
Node
rate
=
rates
.
item
(
i
);
String
kod
=
xpath
.
evaluate
(
"Code"
,
rate
);
String
nazwa
=
xpath
.
evaluate
(
"Currency"
,
rate
);
BigDecimal
kurs
=
new
BigDecimal
(
xpath
.
evaluate
(
"Mid"
,
rate
));
Waluta
waluta
=
new
Waluta
(
kod
,
nazwa
,
kurs
);
tabela
.
dodajWalute
(
waluta
);
}
}
catch
(
ParserConfigurationException
|
SAXException
|
XPathExpressionException
|
IOException
e
)
{
e
.
printStackTrace
();
}
model
.
addAttribute
(
"tabela"
,
tabela
);
model
.
addAttribute
(
"tabela"
,
tabela
);
return
"waluty.html"
;
return
"waluty.html"
;
}
}
private
TabelaWalut
pobierzTabele
(
String
data
)
{
// TODO Auto-generated method stub
return
null
;
}
}
}
PC25-Spring/src/main/resources/templates/waluty.html
View file @
e29ae9eb
...
@@ -15,6 +15,14 @@
...
@@ -15,6 +15,14 @@
<button>
Pobierz
</button>
<button>
Pobierz
</button>
</form>
</form>
<table
th:if=
"${tabela != null}"
class=
"tabela-walut"
>
<tr><th>
Kod
</th><th>
Nazwa
</th><th>
Kurs
</th></tr>
<tr
th:each=
"w : ${tabela.waluty}"
>
<td
th:text=
"${w.kod}"
>
EUR
</td>
<td
th:text=
"${w.nazwa}"
>
euro
</td>
<td
th:text=
"${w.kurs}"
>
4.5432
</td>
</tr>
</table>
</body>
</body>
</html>
</html>
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