Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
android_20250623
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
android_20250623
Commits
09b85c99
Commit
09b85c99
authored
Jun 24, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dodatkowe klasy do obsługi kursów walut
parent
f19fdff9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
156 additions
and
0 deletions
+156
-0
ExchangeRatesTable.java
.../src/main/java/com/example/waluty/ExchangeRatesTable.java
+63
-0
ObslugaNBP.java
...ekt4/app/src/main/java/com/example/waluty/ObslugaNBP.java
+93
-0
No files found.
Projekt4/app/src/main/java/com/example/waluty/ExchangeRatesTable.java
0 → 100644
View file @
09b85c99
package
com
.
example
.
waluty
;
import
java.time.LocalDate
;
import
java.util.Collection
;
import
java.util.Map
;
import
java.util.TreeMap
;
public
class
ExchangeRatesTable
{
private
final
String
table
;
private
final
String
no
;
private
final
LocalDate
effectiveDate
;
private
final
Map
<
String
,
Rate
>
rates
=
new
TreeMap
<>();
public
ExchangeRatesTable
()
{
table
=
null
;
no
=
null
;
effectiveDate
=
null
;
}
public
ExchangeRatesTable
(
String
table
,
String
no
,
LocalDate
effectiveDate
)
{
this
.
table
=
table
;
this
.
no
=
no
;
this
.
effectiveDate
=
effectiveDate
;
}
public
String
getTable
()
{
return
table
;
}
public
String
getNo
()
{
return
no
;
}
public
LocalDate
getEffectiveDate
()
{
return
effectiveDate
;
}
@Override
public
String
toString
()
{
return
"Tabela "
+
table
+
" nr "
+
no
+
" z dnia "
+
effectiveDate
+
" ("
+
rates
.
size
()
+
" walut)"
;
}
public
void
add
(
Rate
waluta
)
{
rates
.
put
(
waluta
.
getCode
(),
waluta
);
}
public
Rate
find
(
String
kod
)
{
return
rates
.
get
(
kod
);
}
public
Collection
<
Rate
>
getRates
()
{
return
rates
.
values
();
}
private
static
final
String
[]
PUSTA_TABLICA
=
new
String
[
0
];
public
String
[]
getCodes
()
{
return
rates
.
keySet
().
toArray
(
PUSTA_TABLICA
);
}
}
Projekt4/app/src/main/java/com/example/waluty/ObslugaNBP.java
0 → 100644
View file @
09b85c99
package
com
.
example
.
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.w3c.dom.Document
;
import
org.w3c.dom.Node
;
import
org.w3c.dom.NodeList
;
import
org.xml.sax.SAXException
;
public
class
ObslugaNBP
{
private
static
final
String
ADRES
=
"http://api.nbp.pl/api/exchangerates/tables"
;
/** Pobiera tabelę z bieżącymi kursami walut.
* Zwraca null w przypadku błędów.
*/
public
static
ExchangeRatesTable
pobierzBiezaceKursy
()
{
Document
doc
=
wczytajXmlZAdresu
(
ADRES
+
"/A?format=xml"
);
return
tabelaZXml
(
doc
);
}
/** Pobiera tabelę z archiwalnymi kursami walut z określonej daty.
* Zwraca null w przypadku błędów, np. wtedy, gdy dla danej daty nie istnieje tabela.
*/
public
static
ExchangeRatesTable
pobierzKursyHistoryczne
(
String
data
)
{
Document
doc
=
wczytajXmlZAdresu
(
ADRES
+
"/A/"
+
data
+
"?format=xml"
);
return
tabelaZXml
(
doc
);
}
private
static
Document
wczytajXmlZAdresu
(
String
adres
)
{
// Document Object Model
try
{
DocumentBuilderFactory
dbf
=
DocumentBuilderFactory
.
newInstance
();
DocumentBuilder
db
=
dbf
.
newDocumentBuilder
();
URL
url
=
new
URL
(
adres
);
try
(
InputStream
in
=
url
.
openStream
())
{
return
db
.
parse
(
in
);
}
}
catch
(
ParserConfigurationException
|
SAXException
|
IOException
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
private
static
ExchangeRatesTable
tabelaZXml
(
Document
doc
)
{
if
(
doc
==
null
)
return
null
;
try
{
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
data
=
LocalDate
.
parse
(
xpath
.
evaluate
(
"//EffectiveDate"
,
doc
));
ExchangeRatesTable
tabela
=
new
ExchangeRatesTable
(
nazwaTabeli
,
numerTabeli
,
data
);
NodeList
rates
=
(
NodeList
)
xpath
.
evaluate
(
"//Rate"
,
doc
,
XPathConstants
.
NODESET
);
final
int
n
=
rates
.
getLength
();
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
Node
node
=
rates
.
item
(
i
);
String
kod
=
xpath
.
evaluate
(
"Code"
,
node
);
String
nazwa
=
xpath
.
evaluate
(
"Currency"
,
node
);
String
kurs
=
xpath
.
evaluate
(
"Mid"
,
node
);
BigDecimal
kursBD
=
new
BigDecimal
(
kurs
);
Rate
rate
=
new
Rate
(
kod
,
nazwa
,
kursBD
);
tabela
.
add
(
rate
);
}
return
tabela
;
}
catch
(
XPathExpressionException
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
}
\ 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