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
d419757d
Commit
d419757d
authored
Jun 25, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Inne przykłady pobierania danych z sieci
parent
dfa0e9e3
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
1724 additions
and
3 deletions
+1724
-3
Pobieranie1_URL.java
...-RestKlient/src/main/java/pobieranie/Pobieranie1_URL.java
+34
-0
Pobieranie2_HttpClient_DoPliku.java
.../main/java/pobieranie/Pobieranie2_HttpClient_DoPliku.java
+41
-0
Pobieranie3_HttpClient_String.java
...c/main/java/pobieranie/Pobieranie3_HttpClient_String.java
+36
-0
Pobieranie4_JSON.java
...RestKlient/src/main/java/pobieranie/Pobieranie4_JSON.java
+64
-0
PobierzPogode1.java
PC28-RestKlient/src/main/java/pogoda/PobierzPogode1.java
+28
-0
PobierzPogode2.java
PC28-RestKlient/src/main/java/pogoda/PobierzPogode2.java
+38
-0
PobierzPogodeDlaMiasta.java
...stKlient/src/main/java/pogoda/PobierzPogodeDlaMiasta.java
+84
-0
WyswietlPogodeDlaMiasta.java
...tKlient/src/main/java/pogoda/WyswietlPogodeDlaMiasta.java
+314
-0
WyswietlPogodeDlaWspolrzednych.java
.../src/main/java/pogoda/WyswietlPogodeDlaWspolrzednych.java
+188
-0
Klient24_Interaktywna_Edycja.java
...c/main/java/rest_klient/Klient24_Interaktywna_Edycja.java
+4
-3
BladAplikacji.java
PC28-RestKlient/src/main/java/waluty/BladAplikacji.java
+21
-0
DrukujWaluty.java
PC28-RestKlient/src/main/java/waluty/DrukujWaluty.java
+20
-0
Pobieranie.java
PC28-RestKlient/src/main/java/waluty/Pobieranie.java
+28
-0
PobieranieJSON.java
PC28-RestKlient/src/main/java/waluty/PobieranieJSON.java
+62
-0
PobieranieXML.java
PC28-RestKlient/src/main/java/waluty/PobieranieXML.java
+77
-0
ProstyPrzelicznik.java
PC28-RestKlient/src/main/java/waluty/ProstyPrzelicznik.java
+52
-0
PrzelicznikKonsolowy.java
...RestKlient/src/main/java/waluty/PrzelicznikKonsolowy.java
+88
-0
PrzelicznikWalutOkno.java
...RestKlient/src/main/java/waluty/PrzelicznikWalutOkno.java
+443
-0
TabelaWalut.java
PC28-RestKlient/src/main/java/waluty/TabelaWalut.java
+60
-0
Waluta.java
PC28-RestKlient/src/main/java/waluty/Waluta.java
+42
-0
No files found.
PC28-RestKlient/src/main/java/pobieranie/Pobieranie1_URL.java
0 → 100644
View file @
d419757d
package
pobieranie
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URL
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.StandardCopyOption
;
public
class
Pobieranie1_URL
{
public
static
void
main
(
String
[]
args
)
{
final
String
adres
=
"http://api.nbp.pl/api/exchangerates/tables/A/?format=json"
;
try
{
URL
url
=
new
URL
(
adres
);
// Od Java 21 bardziej zalecane jest:
// URI uri = new URI(adres);
// URL url = uri.toURL();
try
(
InputStream
input
=
url
.
openStream
())
{
// z input stream dane można czytać porcjami jako ciągi bajtów
// byte[] tablica_bajtow = new byte[1024];
// int ile = input.read(tablica_bajtow);
// System.out.println(new String(tablica_bajtow, 0, ile));
// ale tutaj całość pobrnaych danych zapiszemy do pliku
Files
.
copy
(
input
,
Path
.
of
(
"waluty.json"
),
StandardCopyOption
.
REPLACE_EXISTING
);
System
.
out
.
println
(
"Gotowe"
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
PC28-RestKlient/src/main/java/pobieranie/Pobieranie2_HttpClient_DoPliku.java
0 → 100644
View file @
d419757d
package
pobieranie
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.StandardCopyOption
;
public
class
Pobieranie2_HttpClient_DoPliku
{
public
static
void
main
(
String
[]
args
)
{
final
String
adres
=
"http://api.nbp.pl/api/exchangerates/tables/A/?format=json"
;
try
{
URI
uri
=
new
URI
(
adres
);
HttpClient
client
=
HttpClient
.
newHttpClient
();
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
InputStream
>
response
=
client
.
send
(
request
,
BodyHandlers
.
ofInputStream
());
System
.
out
.
println
(
"response: "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"content-type"
).
orElse
(
"--"
));
if
(
response
.
statusCode
()
!=
200
)
{
return
;
}
// body jest takiego typu, jaki wynika z użytego BodyHandler
try
(
InputStream
input
=
response
.
body
())
{
Files
.
copy
(
input
,
Path
.
of
(
"waluty.json"
),
StandardCopyOption
.
REPLACE_EXISTING
);
System
.
out
.
println
(
"Gotowe"
);
}
}
catch
(
IOException
|
URISyntaxException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC28-RestKlient/src/main/java/pobieranie/Pobieranie3_HttpClient_String.java
0 → 100644
View file @
d419757d
package
pobieranie
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
public
class
Pobieranie3_HttpClient_String
{
public
static
void
main
(
String
[]
args
)
{
final
String
adres
=
"http://api.nbp.pl/api/exchangerates/tables/A/?format=json"
;
// spróbuj xml
try
{
URI
uri
=
new
URI
(
adres
);
HttpClient
client
=
HttpClient
.
newHttpClient
();
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
String
>
response
=
client
.
send
(
request
,
BodyHandlers
.
ofString
());
System
.
out
.
println
(
"response: "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"content-type"
).
orElse
(
"--"
));
// if(response.statusCode() != 200) {
// return;
// }
// body jest takiego typu, jaki wynika z użytego BodyHandler
String
body
=
response
.
body
();
System
.
out
.
println
(
"Długość stringa: "
+
body
.
length
());
System
.
out
.
println
(
body
);
}
catch
(
IOException
|
URISyntaxException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC28-RestKlient/src/main/java/pobieranie/Pobieranie4_JSON.java
0 → 100644
View file @
d419757d
package
pobieranie
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigDecimal
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
import
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
import
jakarta.json.JsonValue
;
public
class
Pobieranie4_JSON
{
public
static
void
main
(
String
[]
args
)
{
final
String
adres
=
"http://api.nbp.pl/api/exchangerates/tables/A/?format=json"
;
try
{
URI
uri
=
new
URI
(
adres
);
HttpClient
client
=
HttpClient
.
newHttpClient
();
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
InputStream
>
response
=
client
.
send
(
request
,
BodyHandlers
.
ofInputStream
());
System
.
out
.
println
(
"response: "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"content-type"
).
orElse
(
"--"
));
if
(
response
.
statusCode
()
!=
200
)
{
return
;
}
// parsowanie JSONa z pomocą biblioteki Jakarta JSON (utworzone w Java EE 7 i dalej rozwijane)
// alternatywy: GSON, org.json, Jackson
JsonArray
json
;
try
(
JsonReader
reader
=
Json
.
createReader
(
response
.
body
()))
{
json
=
reader
.
readArray
();
}
// tutaj już zamykamy połączenie
// zerowym (i jedynym) elementem tablicy jest {obiekt JSONowy} odpowiadający tabeli walut
JsonObject
tabela
=
json
.
getJsonObject
(
0
);
// bezpośrednio w tym obiekcie są takie pola, jak numer i data
System
.
out
.
println
(
"Numer tabeli: "
+
tabela
.
getString
(
"no"
));
System
.
out
.
println
(
"Data: "
+
tabela
.
getString
(
"effectiveDate"
));
// w polu rates znajduje się tablica zawierająca dane poszczególnych walut
JsonArray
rates
=
tabela
.
getJsonArray
(
"rates"
);
System
.
out
.
println
(
"Liczba walut: "
+
rates
.
size
());
System
.
out
.
println
();
for
(
JsonValue
rate
:
rates
)
{
// System.out.println(rate);
JsonObject
rateObj
=
rate
.
asJsonObject
();
String
nazwa
=
rateObj
.
getString
(
"currency"
);
String
kod
=
rateObj
.
getString
(
"code"
);
BigDecimal
kurs
=
rateObj
.
getJsonNumber
(
"mid"
).
bigDecimalValue
();
System
.
out
.
println
(
kod
+
" "
+
nazwa
+
": "
+
kurs
);
}
}
catch
(
IOException
|
URISyntaxException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC28-RestKlient/src/main/java/pogoda/PobierzPogode1.java
0 → 100644
View file @
d419757d
package
pogoda
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
// Pobieramy pogodę dla konkretnej lokalizacji (współrzędne Warszawy)
// i wypisujemy surowe dane JSON.
public
class
PobierzPogode1
{
public
static
void
main
(
String
[]
args
)
{
String
adres
=
"https://api.open-meteo.com/v1/forecast?latitude=52&longitude=21¤t_weather=true"
;
try
{
HttpClient
client
=
HttpClient
.
newHttpClient
();
HttpRequest
request
=
HttpRequest
.
newBuilder
(
new
URI
(
adres
)).
build
();
HttpResponse
<
String
>
response
=
client
.
send
(
request
,
HttpResponse
.
BodyHandlers
.
ofString
());
String
tekstJSON
=
response
.
body
();
System
.
out
.
println
(
tekstJSON
);
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC28-RestKlient/src/main/java/pogoda/PobierzPogode2.java
0 → 100644
View file @
d419757d
package
pogoda
;
import
jakarta.json.Json
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
// Pobieramy pogodę dla konkretnej lokalizacji (współrzędne Warszawy)
// parsujemy JSONa i wyciągamy informację o temperaturze
public
class
PobierzPogode2
{
public
static
void
main
(
String
[]
args
)
{
String
adres
=
"https://api.open-meteo.com/v1/forecast?latitude=52&longitude=21¤t_weather=true"
;
try
{
HttpClient
client
=
HttpClient
.
newHttpClient
();
HttpRequest
request
=
HttpRequest
.
newBuilder
(
new
URI
(
adres
)).
build
();
HttpResponse
<
InputStream
>
response
=
client
.
send
(
request
,
HttpResponse
.
BodyHandlers
.
ofInputStream
());
JsonReader
reader
=
Json
.
createReader
(
response
.
body
());
JsonObject
json
=
reader
.
readObject
();
System
.
out
.
println
(
"Cały JSON: "
+
json
);
JsonObject
weather
=
json
.
getJsonObject
(
"current_weather"
);
System
.
out
.
println
(
"weather: "
+
weather
);
double
temperatura
=
weather
.
getJsonNumber
(
"temperature"
).
doubleValue
();
System
.
out
.
println
(
"temperatura: "
+
temperatura
);
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC28-RestKlient/src/main/java/pogoda/PobierzPogodeDlaMiasta.java
0 → 100644
View file @
d419757d
package
pogoda
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URL
;
import
java.util.Scanner
;
import
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
import
jakarta.json.JsonValue
;
public
class
PobierzPogodeDlaMiasta
{
private
Scanner
scanner
=
new
Scanner
(
System
.
in
);
private
double
lat
;
private
double
lon
;
private
String
miasto
;
public
static
void
main
(
String
[]
args
)
{
new
PobierzPogodeDlaMiasta
().
run
();
}
public
void
run
()
{
try
{
System
.
out
.
print
(
"Podaj nazwę miasta: "
);
String
szukane
=
scanner
.
nextLine
();
if
(
znajdzMiasto
(
szukane
))
{
pobierzIWypiszPogode
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
private
boolean
znajdzMiasto
(
String
szukane
)
throws
IOException
{
URL
url
=
new
URL
(
"https://geocoding-api.open-meteo.com/v1/search?name="
+
szukane
);
try
(
InputStream
input
=
url
.
openStream
();
JsonReader
reader
=
Json
.
createReader
(
input
))
{
JsonObject
json
=
reader
.
readObject
();
JsonArray
results
=
json
.
getJsonArray
(
"results"
);
if
(
results
.
isEmpty
())
{
System
.
out
.
println
(
"Brak znalezionych lokalizacji"
);
return
false
;
}
System
.
out
.
println
(
"Znalezione lokalizacje:"
);
int
i
=
0
;
for
(
JsonValue
result
:
results
)
{
JsonObject
resulto
=
result
.
asJsonObject
();
//System.out.print(result);
System
.
out
.
printf
(
"%2d: <%+.3f %+.3f> → %s, %s%n"
,
i
++,
resulto
.
getJsonNumber
(
"latitude"
).
doubleValue
(),
resulto
.
getJsonNumber
(
"longitude"
).
doubleValue
(),
resulto
.
getString
(
"name"
),
resulto
.
getString
(
"country"
));
}
System
.
out
.
print
(
"Wybierz numer lokalizacji: "
);
i
=
scanner
.
nextInt
();
if
(
i
<
0
||
i
>=
results
.
size
())
{
return
false
;
}
JsonObject
loc
=
results
.
getJsonObject
(
i
);
lat
=
loc
.
getJsonNumber
(
"latitude"
).
doubleValue
();
lon
=
loc
.
getJsonNumber
(
"longitude"
).
doubleValue
();
miasto
=
loc
.
getString
(
"name"
);
return
true
;
}
}
private
void
pobierzIWypiszPogode
()
throws
IOException
{
URL
url
=
new
URL
(
"https://api.open-meteo.com/v1/forecast?latitude="
+
lat
+
"&longitude="
+
lon
+
"¤t_weather=true&hourly=temperature_2m"
);
try
(
InputStream
input
=
url
.
openStream
();
JsonReader
reader
=
Json
.
createReader
(
input
))
{
JsonObject
json
=
reader
.
readObject
();
JsonObject
weather
=
json
.
getJsonObject
(
"current_weather"
);
System
.
out
.
printf
(
"Bieżąca pogoda dla miasta %s:%n"
,
miasto
);
System
.
out
.
println
(
weather
);
System
.
out
.
printf
(
"Temperatura %.1f °C%n"
,
weather
.
getJsonNumber
(
"temperature"
).
doubleValue
());
System
.
out
.
printf
(
"Wiatr: %.1f km/h%n"
,
weather
.
getJsonNumber
(
"windspeed"
).
doubleValue
());
}
}
}
PC28-RestKlient/src/main/java/pogoda/WyswietlPogodeDlaMiasta.java
0 → 100644
View file @
d419757d
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package
pogoda
;
import
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
import
java.awt.Color
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
/**
*
* @author kurs
*/
public
class
WyswietlPogodeDlaMiasta
extends
javax
.
swing
.
JFrame
{
/**
* Creates new form WyswietlPogodeDlaWspolrzednych
*/
public
WyswietlPogodeDlaMiasta
()
{
initComponents
();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings
(
"unchecked"
)
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private
void
initComponents
()
{
jSpinner1
=
new
javax
.
swing
.
JSpinner
();
jSpinner2
=
new
javax
.
swing
.
JSpinner
();
jLabel1
=
new
javax
.
swing
.
JLabel
();
jLabel2
=
new
javax
.
swing
.
JLabel
();
jButton1
=
new
javax
.
swing
.
JButton
();
jLabel3
=
new
javax
.
swing
.
JLabel
();
jLabel_Temperatura
=
new
javax
.
swing
.
JLabel
();
jLabel4
=
new
javax
.
swing
.
JLabel
();
jTextField1
=
new
javax
.
swing
.
JTextField
();
jLabel5
=
new
javax
.
swing
.
JLabel
();
jLabel6
=
new
javax
.
swing
.
JLabel
();
jLabel7
=
new
javax
.
swing
.
JLabel
();
jLabel8
=
new
javax
.
swing
.
JLabel
();
jLabel9
=
new
javax
.
swing
.
JLabel
();
jLabel10
=
new
javax
.
swing
.
JLabel
();
setDefaultCloseOperation
(
javax
.
swing
.
WindowConstants
.
EXIT_ON_CLOSE
);
jSpinner1
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
18
));
// NOI18N
jSpinner1
.
setModel
(
new
javax
.
swing
.
SpinnerNumberModel
(
52.0d
,
-
90.0d
,
90.0d
,
1.0d
));
jSpinner2
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
18
));
// NOI18N
jSpinner2
.
setModel
(
new
javax
.
swing
.
SpinnerNumberModel
(
21.0d
,
-
180.0d
,
180.0d
,
1.0d
));
jLabel1
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
18
));
// NOI18N
jLabel1
.
setText
(
"Szerokość geograficzna"
);
jLabel2
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
18
));
// NOI18N
jLabel2
.
setText
(
"Długość geograficzna"
);
jButton1
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
1
,
24
));
// NOI18N
jButton1
.
setText
(
"Pobierz pogodę"
);
jButton1
.
addActionListener
(
new
java
.
awt
.
event
.
ActionListener
()
{
public
void
actionPerformed
(
java
.
awt
.
event
.
ActionEvent
evt
)
{
jButton1ActionPerformed
(
evt
);
}
});
jLabel3
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
18
));
// NOI18N
jLabel3
.
setText
(
"Temperatura"
);
jLabel_Temperatura
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
1
,
18
));
// NOI18N
jLabel_Temperatura
.
setText
(
"jLabel4"
);
jLabel4
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
18
));
// NOI18N
jLabel4
.
setText
(
"Miasto:"
);
jTextField1
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
18
));
// NOI18N
jTextField1
.
setText
(
"miasto"
);
jTextField1
.
addActionListener
(
new
java
.
awt
.
event
.
ActionListener
()
{
public
void
actionPerformed
(
java
.
awt
.
event
.
ActionEvent
evt
)
{
jTextField1ActionPerformed
(
evt
);
}
});
jLabel5
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
15
));
// NOI18N
jLabel5
.
setText
(
"Ludność:"
);
jLabel6
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
15
));
// NOI18N
jLabel6
.
setText
(
"Wysokość npm"
);
jLabel7
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
15
));
// NOI18N
jLabel7
.
setText
(
"jLabel7"
);
jLabel8
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
15
));
// NOI18N
jLabel8
.
setText
(
"jLabel8"
);
jLabel9
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
18
));
// NOI18N
jLabel9
.
setText
(
"Prędkość wiatru:"
);
jLabel10
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
1
,
18
));
// NOI18N
jLabel10
.
setText
(
"jLabel10"
);
javax
.
swing
.
GroupLayout
layout
=
new
javax
.
swing
.
GroupLayout
(
getContentPane
());
getContentPane
().
setLayout
(
layout
);
layout
.
setHorizontalGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)
.
addGroup
(
layout
.
createSequentialGroup
()
.
addContainerGap
()
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)
.
addGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
TRAILING
,
layout
.
createSequentialGroup
()
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)
.
addComponent
(
jLabel5
)
.
addComponent
(
jLabel6
))
.
addPreferredGap
(
javax
.
swing
.
LayoutStyle
.
ComponentPlacement
.
RELATED
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
Short
.
MAX_VALUE
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
,
false
)
.
addComponent
(
jLabel8
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
252
,
Short
.
MAX_VALUE
)
.
addComponent
(
jLabel7
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
Short
.
MAX_VALUE
)))
.
addGroup
(
layout
.
createSequentialGroup
()
.
addComponent
(
jLabel4
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
74
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addGap
(
33
,
33
,
33
)
.
addComponent
(
jTextField1
))
.
addGroup
(
layout
.
createSequentialGroup
()
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
,
false
)
.
addComponent
(
jButton1
,
javax
.
swing
.
GroupLayout
.
Alignment
.
TRAILING
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
Short
.
MAX_VALUE
)
.
addGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
TRAILING
,
layout
.
createSequentialGroup
()
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)
.
addComponent
(
jLabel1
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
228
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
jLabel2
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
228
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
))
.
addPreferredGap
(
javax
.
swing
.
LayoutStyle
.
ComponentPlacement
.
UNRELATED
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)
.
addComponent
(
jSpinner2
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
119
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
jSpinner1
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
119
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)))
.
addGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
TRAILING
,
layout
.
createSequentialGroup
()
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)
.
addComponent
(
jLabel3
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
114
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
jLabel9
))
.
addPreferredGap
(
javax
.
swing
.
LayoutStyle
.
ComponentPlacement
.
RELATED
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
Short
.
MAX_VALUE
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
,
false
)
.
addComponent
(
jLabel_Temperatura
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
169
,
Short
.
MAX_VALUE
)
.
addComponent
(
jLabel10
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
Short
.
MAX_VALUE
))))
.
addGap
(
0
,
0
,
Short
.
MAX_VALUE
)))
.
addContainerGap
())
);
layout
.
setVerticalGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)
.
addGroup
(
layout
.
createSequentialGroup
()
.
addGap
(
13
,
13
,
13
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
BASELINE
)
.
addComponent
(
jLabel4
)
.
addComponent
(
jTextField1
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
37
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
))
.
addPreferredGap
(
javax
.
swing
.
LayoutStyle
.
ComponentPlacement
.
UNRELATED
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
BASELINE
)
.
addComponent
(
jLabel5
)
.
addComponent
(
jLabel7
))
.
addPreferredGap
(
javax
.
swing
.
LayoutStyle
.
ComponentPlacement
.
UNRELATED
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
BASELINE
)
.
addComponent
(
jLabel6
)
.
addComponent
(
jLabel8
))
.
addGap
(
18
,
18
,
18
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
BASELINE
)
.
addComponent
(
jSpinner1
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
jLabel1
))
.
addPreferredGap
(
javax
.
swing
.
LayoutStyle
.
ComponentPlacement
.
UNRELATED
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
BASELINE
)
.
addComponent
(
jSpinner2
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
jLabel2
))
.
addGap
(
18
,
18
,
18
)
.
addComponent
(
jButton1
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
64
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addGap
(
18
,
18
,
18
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
BASELINE
)
.
addComponent
(
jLabel3
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
31
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
jLabel_Temperatura
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
31
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
))
.
addPreferredGap
(
javax
.
swing
.
LayoutStyle
.
ComponentPlacement
.
RELATED
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
BASELINE
)
.
addComponent
(
jLabel9
)
.
addComponent
(
jLabel10
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
30
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
))
.
addContainerGap
(
53
,
Short
.
MAX_VALUE
))
);
pack
();
}
// </editor-fold>//GEN-END:initComponents
private
void
jButton1ActionPerformed
(
java
.
awt
.
event
.
ActionEvent
evt
)
{
//GEN-FIRST:event_jButton1ActionPerformed
double
lat
=
(
Double
)
jSpinner1
.
getValue
();
double
lon
=
(
Double
)
jSpinner2
.
getValue
();
String
adres
=
"https://api.open-meteo.com/v1/forecast?latitude="
+
lat
+
"&longitude="
+
lon
+
"¤t_weather=true"
;
try
{
HttpClient
client
=
HttpClient
.
newHttpClient
();
HttpRequest
request
=
HttpRequest
.
newBuilder
(
new
URI
(
adres
)).
build
();
HttpResponse
<
InputStream
>
response
=
client
.
send
(
request
,
HttpResponse
.
BodyHandlers
.
ofInputStream
());
JsonReader
reader
=
Json
.
createReader
(
response
.
body
());
JsonObject
json
=
reader
.
readObject
();
System
.
out
.
println
(
json
);
JsonObject
weather
=
json
.
getJsonObject
(
"current_weather"
);
double
temperatura
=
weather
.
getJsonNumber
(
"temperature"
).
doubleValue
();
double
wiatr
=
weather
.
getJsonNumber
(
"windspeed"
).
doubleValue
();
jLabel_Temperatura
.
setText
(
String
.
valueOf
(
temperatura
));
jLabel10
.
setText
(
String
.
valueOf
(
wiatr
));
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
//GEN-LAST:event_jButton1ActionPerformed
private
void
jTextField1ActionPerformed
(
java
.
awt
.
event
.
ActionEvent
evt
)
{
//GEN-FIRST:event_jTextField1ActionPerformed
// to się wykona po naciśnięciu enter w polu tekstowym
String
miasto
=
jTextField1
.
getText
().
strip
();
String
adres
=
"https://geocoding-api.open-meteo.com/v1/search?name="
+
miasto
.
replace
(
" "
,
"%20"
);
try
{
HttpClient
client
=
HttpClient
.
newHttpClient
();
HttpRequest
request
=
HttpRequest
.
newBuilder
(
new
URI
(
adres
)).
build
();
HttpResponse
<
InputStream
>
response
=
client
.
send
(
request
,
HttpResponse
.
BodyHandlers
.
ofInputStream
());
JsonReader
reader
=
Json
.
createReader
(
response
.
body
());
JsonObject
json
=
reader
.
readObject
();
System
.
out
.
println
(
json
);
JsonArray
listaWynikow
=
json
.
getJsonArray
(
"results"
);
if
(
listaWynikow
==
null
||
listaWynikow
.
isEmpty
())
{
jTextField1
.
setForeground
(
Color
.
RED
);
}
else
{
JsonObject
daneMiasta
=
listaWynikow
.
getJsonObject
(
0
);
jTextField1
.
setForeground
(
Color
.
BLACK
);
jTextField1
.
setText
(
daneMiasta
.
getString
(
"name"
,
miasto
));
double
lat
=
daneMiasta
.
getJsonNumber
(
"latitude"
).
doubleValue
();
double
lon
=
daneMiasta
.
getJsonNumber
(
"longitude"
).
doubleValue
();
jSpinner1
.
setValue
(
lat
);
jSpinner2
.
setValue
(
lon
);
if
(
daneMiasta
.
containsKey
(
"population"
))
{
jLabel7
.
setText
(
String
.
valueOf
(
daneMiasta
.
getJsonNumber
(
"population"
)));
}
else
{
jLabel7
.
setText
(
""
);
}
if
(
daneMiasta
.
containsKey
(
"elevation"
))
{
jLabel8
.
setText
(
String
.
valueOf
(
daneMiasta
.
getJsonNumber
(
"elevation"
)));
}
else
{
jLabel8
.
setText
(
""
);
}
}
jButton1ActionPerformed
(
evt
);
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
//GEN-LAST:event_jTextField1ActionPerformed
/**
* @param args the command line arguments
*/
public
static
void
main
(
String
args
[])
{
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try
{
for
(
javax
.
swing
.
UIManager
.
LookAndFeelInfo
info
:
javax
.
swing
.
UIManager
.
getInstalledLookAndFeels
())
{
if
(
"Nimbus"
.
equals
(
info
.
getName
()))
{
javax
.
swing
.
UIManager
.
setLookAndFeel
(
info
.
getClassName
());
break
;
}
}
}
catch
(
ClassNotFoundException
ex
)
{
java
.
util
.
logging
.
Logger
.
getLogger
(
WyswietlPogodeDlaMiasta
.
class
.
getName
()).
log
(
java
.
util
.
logging
.
Level
.
SEVERE
,
null
,
ex
);
}
catch
(
InstantiationException
ex
)
{
java
.
util
.
logging
.
Logger
.
getLogger
(
WyswietlPogodeDlaMiasta
.
class
.
getName
()).
log
(
java
.
util
.
logging
.
Level
.
SEVERE
,
null
,
ex
);
}
catch
(
IllegalAccessException
ex
)
{
java
.
util
.
logging
.
Logger
.
getLogger
(
WyswietlPogodeDlaMiasta
.
class
.
getName
()).
log
(
java
.
util
.
logging
.
Level
.
SEVERE
,
null
,
ex
);
}
catch
(
javax
.
swing
.
UnsupportedLookAndFeelException
ex
)
{
java
.
util
.
logging
.
Logger
.
getLogger
(
WyswietlPogodeDlaMiasta
.
class
.
getName
()).
log
(
java
.
util
.
logging
.
Level
.
SEVERE
,
null
,
ex
);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java
.
awt
.
EventQueue
.
invokeLater
(
new
Runnable
()
{
public
void
run
()
{
new
WyswietlPogodeDlaMiasta
().
setVisible
(
true
);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private
javax
.
swing
.
JButton
jButton1
;
private
javax
.
swing
.
JLabel
jLabel1
;
private
javax
.
swing
.
JLabel
jLabel10
;
private
javax
.
swing
.
JLabel
jLabel2
;
private
javax
.
swing
.
JLabel
jLabel3
;
private
javax
.
swing
.
JLabel
jLabel4
;
private
javax
.
swing
.
JLabel
jLabel5
;
private
javax
.
swing
.
JLabel
jLabel6
;
private
javax
.
swing
.
JLabel
jLabel7
;
private
javax
.
swing
.
JLabel
jLabel8
;
private
javax
.
swing
.
JLabel
jLabel9
;
private
javax
.
swing
.
JLabel
jLabel_Temperatura
;
private
javax
.
swing
.
JSpinner
jSpinner1
;
private
javax
.
swing
.
JSpinner
jSpinner2
;
private
javax
.
swing
.
JTextField
jTextField1
;
// End of variables declaration//GEN-END:variables
}
PC28-RestKlient/src/main/java/pogoda/WyswietlPogodeDlaWspolrzednych.java
0 → 100644
View file @
d419757d
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package
pogoda
;
import
jakarta.json.Json
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
/**
*
* @author kurs
*/
public
class
WyswietlPogodeDlaWspolrzednych
extends
javax
.
swing
.
JFrame
{
/**
* Creates new form WyswietlPogodeDlaWspolrzednych
*/
public
WyswietlPogodeDlaWspolrzednych
()
{
initComponents
();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings
(
"unchecked"
)
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private
void
initComponents
()
{
jSpinner1
=
new
javax
.
swing
.
JSpinner
();
jSpinner2
=
new
javax
.
swing
.
JSpinner
();
jLabel1
=
new
javax
.
swing
.
JLabel
();
jLabel2
=
new
javax
.
swing
.
JLabel
();
jButton1
=
new
javax
.
swing
.
JButton
();
jLabel3
=
new
javax
.
swing
.
JLabel
();
jLabel_Temperatura
=
new
javax
.
swing
.
JLabel
();
setDefaultCloseOperation
(
javax
.
swing
.
WindowConstants
.
EXIT_ON_CLOSE
);
jSpinner1
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
18
));
// NOI18N
jSpinner1
.
setModel
(
new
javax
.
swing
.
SpinnerNumberModel
(
52.0d
,
-
90.0d
,
90.0d
,
1.0d
));
jSpinner2
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
18
));
// NOI18N
jSpinner2
.
setModel
(
new
javax
.
swing
.
SpinnerNumberModel
(
21.0d
,
-
180.0d
,
180.0d
,
1.0d
));
jLabel1
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
18
));
// NOI18N
jLabel1
.
setText
(
"Szerokość geograficzna"
);
jLabel2
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
18
));
// NOI18N
jLabel2
.
setText
(
"Długość geograficzna"
);
jButton1
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
1
,
24
));
// NOI18N
jButton1
.
setText
(
"Pobierz pogodę"
);
jButton1
.
addActionListener
(
new
java
.
awt
.
event
.
ActionListener
()
{
public
void
actionPerformed
(
java
.
awt
.
event
.
ActionEvent
evt
)
{
jButton1ActionPerformed
(
evt
);
}
});
jLabel3
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
0
,
18
));
// NOI18N
jLabel3
.
setText
(
"Temperatura"
);
jLabel_Temperatura
.
setFont
(
new
java
.
awt
.
Font
(
"Segoe UI"
,
1
,
18
));
// NOI18N
jLabel_Temperatura
.
setText
(
"jLabel4"
);
javax
.
swing
.
GroupLayout
layout
=
new
javax
.
swing
.
GroupLayout
(
getContentPane
());
getContentPane
().
setLayout
(
layout
);
layout
.
setHorizontalGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)
.
addGroup
(
layout
.
createSequentialGroup
()
.
addContainerGap
()
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
,
false
)
.
addComponent
(
jButton1
,
javax
.
swing
.
GroupLayout
.
Alignment
.
TRAILING
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
Short
.
MAX_VALUE
)
.
addGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
TRAILING
,
layout
.
createSequentialGroup
()
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)
.
addComponent
(
jLabel1
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
228
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
jLabel2
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
228
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
))
.
addPreferredGap
(
javax
.
swing
.
LayoutStyle
.
ComponentPlacement
.
UNRELATED
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)
.
addComponent
(
jSpinner2
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
119
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
jSpinner1
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
119
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)))
.
addGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
TRAILING
,
layout
.
createSequentialGroup
()
.
addComponent
(
jLabel3
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
114
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addPreferredGap
(
javax
.
swing
.
LayoutStyle
.
ComponentPlacement
.
RELATED
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
Short
.
MAX_VALUE
)
.
addComponent
(
jLabel_Temperatura
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
169
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)))
.
addContainerGap
(
18
,
Short
.
MAX_VALUE
))
);
layout
.
setVerticalGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)
.
addGroup
(
layout
.
createSequentialGroup
()
.
addGap
(
41
,
41
,
41
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
BASELINE
)
.
addComponent
(
jSpinner1
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
jLabel1
))
.
addGap
(
29
,
29
,
29
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
BASELINE
)
.
addComponent
(
jSpinner2
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
jLabel2
))
.
addGap
(
18
,
18
,
18
)
.
addComponent
(
jButton1
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
64
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addGap
(
18
,
18
,
18
)
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
BASELINE
)
.
addComponent
(
jLabel3
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
31
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
jLabel_Temperatura
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
31
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
))
.
addContainerGap
(
61
,
Short
.
MAX_VALUE
))
);
pack
();
}
// </editor-fold>//GEN-END:initComponents
private
void
jButton1ActionPerformed
(
java
.
awt
.
event
.
ActionEvent
evt
)
{
//GEN-FIRST:event_jButton1ActionPerformed
double
lat
=
(
Double
)
jSpinner1
.
getValue
();
double
lon
=
(
Double
)
jSpinner2
.
getValue
();
String
adres
=
"https://api.open-meteo.com/v1/forecast?latitude="
+
lat
+
"&longitude="
+
lon
+
"¤t_weather=true"
;
try
{
HttpClient
client
=
HttpClient
.
newHttpClient
();
HttpRequest
request
=
HttpRequest
.
newBuilder
(
new
URI
(
adres
)).
build
();
HttpResponse
<
InputStream
>
response
=
client
.
send
(
request
,
HttpResponse
.
BodyHandlers
.
ofInputStream
());
JsonReader
reader
=
Json
.
createReader
(
response
.
body
());
JsonObject
json
=
reader
.
readObject
();
JsonObject
weather
=
json
.
getJsonObject
(
"current_weather"
);
double
temperatura
=
weather
.
getJsonNumber
(
"temperature"
).
doubleValue
();
jLabel_Temperatura
.
setText
(
String
.
valueOf
(
temperatura
));
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
//GEN-LAST:event_jButton1ActionPerformed
/**
* @param args the command line arguments
*/
public
static
void
main
(
String
args
[])
{
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try
{
for
(
javax
.
swing
.
UIManager
.
LookAndFeelInfo
info
:
javax
.
swing
.
UIManager
.
getInstalledLookAndFeels
())
{
if
(
"Nimbus"
.
equals
(
info
.
getName
()))
{
javax
.
swing
.
UIManager
.
setLookAndFeel
(
info
.
getClassName
());
break
;
}
}
}
catch
(
ClassNotFoundException
ex
)
{
java
.
util
.
logging
.
Logger
.
getLogger
(
WyswietlPogodeDlaWspolrzednych
.
class
.
getName
()).
log
(
java
.
util
.
logging
.
Level
.
SEVERE
,
null
,
ex
);
}
catch
(
InstantiationException
ex
)
{
java
.
util
.
logging
.
Logger
.
getLogger
(
WyswietlPogodeDlaWspolrzednych
.
class
.
getName
()).
log
(
java
.
util
.
logging
.
Level
.
SEVERE
,
null
,
ex
);
}
catch
(
IllegalAccessException
ex
)
{
java
.
util
.
logging
.
Logger
.
getLogger
(
WyswietlPogodeDlaWspolrzednych
.
class
.
getName
()).
log
(
java
.
util
.
logging
.
Level
.
SEVERE
,
null
,
ex
);
}
catch
(
javax
.
swing
.
UnsupportedLookAndFeelException
ex
)
{
java
.
util
.
logging
.
Logger
.
getLogger
(
WyswietlPogodeDlaWspolrzednych
.
class
.
getName
()).
log
(
java
.
util
.
logging
.
Level
.
SEVERE
,
null
,
ex
);
}
//</editor-fold>
/* Create and display the form */
java
.
awt
.
EventQueue
.
invokeLater
(
new
Runnable
()
{
public
void
run
()
{
new
WyswietlPogodeDlaWspolrzednych
().
setVisible
(
true
);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private
javax
.
swing
.
JButton
jButton1
;
private
javax
.
swing
.
JLabel
jLabel1
;
private
javax
.
swing
.
JLabel
jLabel2
;
private
javax
.
swing
.
JLabel
jLabel3
;
private
javax
.
swing
.
JLabel
jLabel_Temperatura
;
private
javax
.
swing
.
JSpinner
jSpinner1
;
private
javax
.
swing
.
JSpinner
jSpinner2
;
// End of variables declaration//GEN-END:variables
}
PC28-RestKlient/src/main/java/rest_klient/Klient24_Interaktywna_Edycja.java
View file @
d419757d
...
@@ -48,9 +48,10 @@ public class Klient24_Interaktywna_Edycja {
...
@@ -48,9 +48,10 @@ public class Klient24_Interaktywna_Edycja {
if
(
zmianaCeny
.
compareTo
(
BigDecimal
.
ZERO
)
!=
0
)
{
if
(
zmianaCeny
.
compareTo
(
BigDecimal
.
ZERO
)
!=
0
)
{
BigDecimal
newPrice
=
product
.
getPrice
().
add
(
zmianaCeny
);
BigDecimal
newPrice
=
product
.
getPrice
().
add
(
zmianaCeny
);
System
.
out
.
println
(
"PUT nowej ceny..."
);
System
.
out
.
println
(
"PUT nowej ceny..."
);
Response
odpPut
=
path
.
path
(
"price"
).
resolveTemplate
(
"id"
,
id
).
request
()
try
(
Response
odpPut
=
path
.
path
(
"price"
).
resolveTemplate
(
"id"
,
id
).
request
()
.
put
(
Entity
.
entity
(
newPrice
,
MediaType
.
TEXT_PLAIN_TYPE
));
.
put
(
Entity
.
entity
(
newPrice
,
MediaType
.
TEXT_PLAIN_TYPE
)))
{
System
.
out
.
println
(
"PUT zakończył się kodem "
+
odpPut
.
getStatus
());
System
.
out
.
println
(
"PUT zakończył się kodem "
+
odpPut
.
getStatus
());
}
}
}
}
else
{
}
else
{
System
.
out
.
println
(
"nie mogę odczytać"
);
System
.
out
.
println
(
"nie mogę odczytać"
);
...
...
PC28-RestKlient/src/main/java/waluty/BladAplikacji.java
0 → 100644
View file @
d419757d
package
waluty
;
public
class
BladAplikacji
extends
Exception
{
public
BladAplikacji
()
{
super
();
}
public
BladAplikacji
(
String
message
,
Throwable
cause
)
{
super
(
message
,
cause
);
}
public
BladAplikacji
(
String
message
)
{
super
(
message
);
}
public
BladAplikacji
(
Throwable
cause
)
{
super
(
cause
);
}
}
PC28-RestKlient/src/main/java/waluty/DrukujWaluty.java
0 → 100644
View file @
d419757d
package
waluty
;
public
class
DrukujWaluty
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Startujemy..."
);
try
{
Pobieranie
pobieranie
=
Pobieranie
.
utworz
(
"JSON"
);
TabelaWalut
tabelaWalut
=
pobieranie
.
pobierzBiezaceKursy
();
System
.
out
.
println
(
tabelaWalut
);
for
(
Waluta
waluta
:
tabelaWalut
.
getRates
())
{
System
.
out
.
println
(
waluta
);
}
}
catch
(
BladAplikacji
e
)
{
e
.
printStackTrace
();
}
}
}
PC28-RestKlient/src/main/java/waluty/Pobieranie.java
0 → 100644
View file @
d419757d
package
waluty
;
import
java.time.LocalDate
;
public
abstract
class
Pobieranie
{
protected
static
final
String
ADRES_BAZOWY
=
"https://api.nbp.pl/api/exchangerates/tables"
;
public
abstract
TabelaWalut
pobierzBiezaceKursy
()
throws
BladAplikacji
;
public
abstract
TabelaWalut
pobierzArchiwalneKursy
(
String
data
)
throws
BladAplikacji
;
// Jeśli jakaś część implementacji jest wpólna dla wszystkich podklas,
// to można umieścić ja w klasie abstrakcyjnej
public
TabelaWalut
pobierzArchiwalneKursy
(
LocalDate
data
)
throws
BladAplikacji
{
return
pobierzArchiwalneKursy
(
data
.
toString
());
}
// fabryka nowych obiektów
public
static
Pobieranie
utworz
(
String
format
)
{
return
switch
(
format
.
toUpperCase
())
{
case
"XML"
->
new
PobieranieXML
();
case
"JSON"
->
new
PobieranieJSON
();
default
->
throw
new
IllegalArgumentException
(
"Nieznany format "
+
format
);
};
}
}
PC28-RestKlient/src/main/java/waluty/PobieranieJSON.java
0 → 100644
View file @
d419757d
package
waluty
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
import
java.time.LocalDate
;
import
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
import
jakarta.json.JsonValue
;
public
class
PobieranieJSON
extends
Pobieranie
{
public
TabelaWalut
pobierzBiezaceKursy
()
throws
BladAplikacji
{
String
adres
=
ADRES_BAZOWY
+
"/A?format=json"
;
return
pobierzZAdresu
(
adres
);
}
public
TabelaWalut
pobierzArchiwalneKursy
(
String
data
)
throws
BladAplikacji
{
return
pobierzZAdresu
(
ADRES_BAZOWY
+
"/A/"
+
data
+
"?format=json"
);
}
private
TabelaWalut
pobierzZAdresu
(
String
adres
)
throws
BladAplikacji
{
try
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
URI
uri
=
new
URI
(
adres
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
InputStream
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofInputStream
());
if
(
response
.
statusCode
()
!=
200
)
{
throw
new
BladAplikacji
(
"Zapytanie HTTP zwróciło "
+
response
.
statusCode
());
}
// dane odebrane z sieci "parsujemy" jako JSON
try
(
JsonReader
reader
=
Json
.
createReader
(
response
.
body
()))
{
JsonArray
array
=
reader
.
readArray
();
JsonObject
tabela
=
array
.
getJsonObject
(
0
);
String
no
=
tabela
.
getString
(
"no"
);
LocalDate
effectiveDate
=
LocalDate
.
parse
(
tabela
.
getString
(
"effectiveDate"
));
TabelaWalut
tabelaWalut
=
new
TabelaWalut
(
no
,
effectiveDate
);
JsonArray
rates
=
tabela
.
getJsonArray
(
"rates"
);
for
(
JsonValue
rate
:
rates
)
{
JsonObject
rateObj
=
rate
.
asJsonObject
();
Waluta
waluta
=
new
Waluta
(
rateObj
.
getString
(
"code"
),
rateObj
.
getString
(
"currency"
),
rateObj
.
getJsonNumber
(
"mid"
).
bigDecimalValue
());
tabelaWalut
.
addRate
(
waluta
);
}
return
tabelaWalut
;
}
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
throw
new
BladAplikacji
(
e
);
}
}
}
PC28-RestKlient/src/main/java/waluty/PobieranieXML.java
0 → 100644
View file @
d419757d
package
waluty
;
import
java.io.InputStream
;
import
java.math.BigDecimal
;
import
java.net.URI
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
import
javax.xml.parsers.DocumentBuilder
;
import
javax.xml.parsers.DocumentBuilderFactory
;
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
;
public
class
PobieranieXML
extends
Pobieranie
{
private
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
private
DocumentBuilderFactory
dbf
=
DocumentBuilderFactory
.
newInstance
();
private
XPathFactory
xpf
=
XPathFactory
.
newInstance
();
@Override
public
TabelaWalut
pobierzBiezaceKursy
()
throws
BladAplikacji
{
return
pobierzZAdresu
(
ADRES_BAZOWY
+
"/A"
+
"?format=xml"
);
}
@Override
public
TabelaWalut
pobierzArchiwalneKursy
(
String
data
)
throws
BladAplikacji
{
return
pobierzZAdresu
(
ADRES_BAZOWY
+
"/A/"
+
data
+
"?format=xml"
);
}
private
TabelaWalut
pobierzZAdresu
(
String
adres
)
throws
BladAplikacji
{
try
{
URI
uri
=
new
URI
(
adres
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
InputStream
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofInputStream
());
if
(
response
.
statusCode
()
!=
200
)
{
throw
new
BladAplikacji
(
"Brak danych w odpowiedzi HTTP, kod "
+
response
.
statusCode
());
}
DocumentBuilder
builder
=
dbf
.
newDocumentBuilder
();
Document
document
=
builder
.
parse
(
response
.
body
());
return
tabelaZXML
(
document
);
}
catch
(
BladAplikacji
e
)
{
throw
e
;
}
catch
(
Exception
e
)
{
throw
new
BladAplikacji
(
"Błąd podczas pobierania XML: "
+
e
.
getMessage
(),
e
);
}
}
private
TabelaWalut
tabelaZXML
(
Document
document
)
throws
XPathExpressionException
{
XPath
xpath
=
xpf
.
newXPath
();
// String no = xpath.evaluate("/ArrayOfExchangeRatesTable/ExchangeRatesTable/No", document);
// String date = xpath.evaluate("/ArrayOfExchangeRatesTable/ExchangeRatesTable/EffectiveDate", document);
String
no
=
xpath
.
evaluate
(
"//No"
,
document
);
String
date
=
xpath
.
evaluate
(
"//EffectiveDate"
,
document
);
TabelaWalut
tabela
=
new
TabelaWalut
(
no
,
date
);
NodeList
rates
=
(
NodeList
)
xpath
.
evaluate
(
"//Rate"
,
document
,
XPathConstants
.
NODESET
);
final
int
length
=
rates
.
getLength
();
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
Node
rate
=
rates
.
item
(
i
);
String
currency
=
xpath
.
evaluate
(
"Currency"
,
rate
);
String
code
=
xpath
.
evaluate
(
"Code"
,
rate
);
BigDecimal
mid
=
new
BigDecimal
(
xpath
.
evaluate
(
"Mid"
,
rate
));
Waluta
waluta
=
new
Waluta
(
code
,
currency
,
mid
);
tabela
.
addRate
(
waluta
);
}
return
tabela
;
}
}
PC28-RestKlient/src/main/java/waluty/ProstyPrzelicznik.java
0 → 100644
View file @
d419757d
package
waluty
;
import
java.math.BigDecimal
;
import
java.util.Locale
;
import
java.util.Optional
;
import
java.util.Scanner
;
/* Napisz program, który pobiera aktualne kursy (dodatkowa opcja - możliwość podania daty i pobranie archowalnego)
*
* Następnie: użytkownik podaje kod waluty oraz kwotę, a program przelicza kwotę w tej walucie na złote
* (ew. przeliczanie w drugą stronę)
*/
public
class
ProstyPrzelicznik
{
public
static
void
main
(
String
[]
args
)
{
Pobieranie
pobieranie
=
Pobieranie
.
utworz
(
"JSON"
);
Scanner
scanner
=
new
Scanner
(
System
.
in
);
scanner
.
useLocale
(
Locale
.
US
);
System
.
out
.
println
(
"Podaj datę lub naciśnij enter, aby pobrać najnowsze kursy:"
);
String
data
=
scanner
.
nextLine
();
try
{
TabelaWalut
tabela
=
data
.
isBlank
()
?
pobieranie
.
pobierzBiezaceKursy
()
:
pobieranie
.
pobierzArchiwalneKursy
(
data
);
System
.
out
.
println
(
"Pobrano tabelę: "
+
tabela
);
System
.
out
.
println
(
"\nPodawaj kwoty do przeliczenia w postaci 1000 USD , a żeby zakończyć wpisz Q"
);
while
(
true
)
{
System
.
out
.
print
(
"> "
);
if
(
scanner
.
hasNextBigDecimal
())
{
BigDecimal
kwota
=
scanner
.
nextBigDecimal
();
String
kod
=
scanner
.
next
().
toUpperCase
();
Optional
<
Waluta
>
found
=
tabela
.
findByCode
(
kod
);
if
(
found
.
isPresent
())
{
Waluta
waluta
=
found
.
get
();
BigDecimal
wynik
=
waluta
.
przeliczNaZlote
(
kwota
);
System
.
out
.
println
(
wynik
+
" PLN"
);
}
else
{
System
.
out
.
println
(
"Nieznana waluta"
);
}
}
else
{
if
(
scanner
.
next
().
equalsIgnoreCase
(
"Q"
))
break
;
}
}
}
catch
(
BladAplikacji
e
)
{
System
.
out
.
println
(
"Nastąpił błąd: "
+
e
);
e
.
printStackTrace
(
System
.
out
);
}
System
.
out
.
println
(
"Do widzenia"
);
}
}
PC28-RestKlient/src/main/java/waluty/PrzelicznikKonsolowy.java
0 → 100644
View file @
d419757d
package
waluty
;
import
java.math.BigDecimal
;
import
java.util.Locale
;
import
java.util.Optional
;
import
java.util.Scanner
;
public
class
PrzelicznikKonsolowy
{
private
Scanner
scanner
;
private
Pobieranie
pobieranie
;
public
PrzelicznikKonsolowy
()
{
scanner
=
new
Scanner
(
System
.
in
);
scanner
.
useLocale
(
Locale
.
US
);
}
public
static
void
main
(
String
[]
args
)
{
new
PrzelicznikKonsolowy
().
run
();
}
public
void
run
()
{
System
.
out
.
println
(
"Wybierz format danych: XML / JSON"
);
String
format
=
scanner
.
nextLine
().
trim
().
toUpperCase
();
switch
(
format
)
{
case
"XML"
->
pobieranie
=
new
PobieranieXML
();
case
""
,
"JSON"
->
pobieranie
=
new
PobieranieJSON
();
default
->
{
System
.
out
.
println
(
"Nieznany format, kończę program."
);
return
;
}
}
program:
while
(
true
)
{
System
.
out
.
println
(
"\nWpisz datę w formacie YYYY-MM-DD lub naciśnij ENTER, aby pobrać bieżące kursy."
);
System
.
out
.
println
(
"Możesz też wpisać Q , aby zakończyć program"
);
String
data
=
scanner
.
nextLine
();
try
{
TabelaWalut
tabela
;
switch
(
data
)
{
case
"q"
,
"Q"
->
{
break
program
;}
case
""
->
tabela
=
pobieranie
.
pobierzBiezaceKursy
();
default
->
tabela
=
pobieranie
.
pobierzArchiwalneKursy
(
data
);
}
dzialajDlaWybranejTabeli
(
tabela
);
}
catch
(
BladAplikacji
e
)
{
System
.
out
.
println
(
"Wyjątek! "
+
e
.
getMessage
());
}
}
System
.
out
.
println
(
"Koniec programu"
);
}
private
void
dzialajDlaWybranejTabeli
(
TabelaWalut
tabela
)
{
System
.
out
.
println
(
tabela
);
while
(
true
)
{
System
.
out
.
println
(
"\nPodaj kod waluty, lub Q aby zakończyć pracę z bieżącą tabelą"
);
String
code
=
scanner
.
nextLine
().
toUpperCase
();
if
(
"Q"
.
equals
(
code
))
{
break
;
}
Optional
<
Waluta
>
found
=
tabela
.
findByCode
(
code
);
if
(
found
.
isEmpty
())
{
System
.
out
.
println
(
"Nie ma waluty o kodzie "
+
code
);
continue
;
}
dzialajDlaWybranejWaluty
(
found
.
get
());
}
}
private
void
dzialajDlaWybranejWaluty
(
Waluta
waluta
)
{
System
.
out
.
println
(
"Wybrana waluta: "
+
waluta
.
getCode
()
+
" ("
+
waluta
.
getCurrency
()
+
")"
+
", kurs "
+
waluta
.
getMid
());
while
(
true
)
{
System
.
out
.
println
(
"\nPodaj kwotę do przeliczenia. 0 oznacza koniec:"
);
BigDecimal
kwota
=
scanner
.
nextBigDecimal
();
scanner
.
nextLine
();
if
(
kwota
.
compareTo
(
BigDecimal
.
ZERO
)
==
0
)
{
break
;
}
BigDecimal
wynikWaluta
=
waluta
.
przeliczNaWalute
(
kwota
);
BigDecimal
wynikPLN
=
waluta
.
przeliczNaZlote
(
kwota
);
System
.
out
.
println
(
kwota
+
" "
+
waluta
.
getCode
()
+
" = "
+
wynikPLN
+
" PLN"
);
System
.
out
.
println
(
kwota
+
" PLN = "
+
wynikWaluta
+
" "
+
waluta
.
getCode
());
}
}
}
PC28-RestKlient/src/main/java/waluty/PrzelicznikWalutOkno.java
0 → 100644
View file @
d419757d
package
waluty
;
import
java.awt.Color
;
import
java.awt.EventQueue
;
import
java.awt.Font
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
import
java.math.BigDecimal
;
import
java.time.LocalDate
;
import
java.util.Optional
;
import
javax.swing.ButtonGroup
;
import
javax.swing.DefaultComboBoxModel
;
import
javax.swing.GroupLayout
;
import
javax.swing.GroupLayout.Alignment
;
import
javax.swing.JButton
;
import
javax.swing.JComboBox
;
import
javax.swing.JFrame
;
import
javax.swing.JLabel
;
import
javax.swing.JPanel
;
import
javax.swing.JRadioButton
;
import
javax.swing.JTextField
;
import
javax.swing.LayoutStyle.ComponentPlacement
;
import
javax.swing.SwingConstants
;
import
javax.swing.SwingUtilities
;
import
javax.swing.SwingWorker
;
public
class
PrzelicznikWalutOkno
{
private
static
final
Font
FONT_LABEL
=
new
Font
(
"Times New Roman"
,
Font
.
PLAIN
,
24
);
private
static
final
Font
FONT_WARTOSC
=
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
);
private
static
final
Font
FONT_TXT
=
new
Font
(
"Dialog"
,
Font
.
BOLD
,
32
);
private
static
final
Font
FONT_WYNIK
=
new
Font
(
"Dialog"
,
Font
.
BOLD
,
32
);
private
static
final
Font
FONT_BUTTON
=
new
Font
(
"Dialog"
,
Font
.
BOLD
,
28
);
private
TabelaWalut
tabela
=
null
;
private
JFrame
frmPrzelicznikWalut
;
private
JTextField
txtKwota
;
private
JComboBox
<
String
>
comboBox_Waluta
;
private
JLabel
lbl_KodWaluty
;
private
JLabel
lbl_NazwaWaluty
;
private
JLabel
lbl_KursWaluty
;
private
JRadioButton
rdbtnWalutaNaZlote
;
private
JRadioButton
rdbtnZloteNaWalute
;
private
JButton
btnPrzelicz
;
private
JLabel
lblPrawdziwyWynik
;
private
final
ButtonGroup
buttonGroup
=
new
ButtonGroup
();
private
JPanel
panel_1
;
private
JLabel
lblTabela
;
private
JLabel
lbl_NumerTabeli
;
private
JLabel
lblData
;
private
JLabel
lbl_DataTabeli
;
private
JTextField
txtData
;
private
JButton
btnPobierzAktualne
;
private
Pobieranie
pobieranie
=
Pobieranie
.
utworz
(
"xml"
);
/**
* Launch the application.
*/
public
static
void
main
(
String
[]
args
)
{
EventQueue
.
invokeLater
(
new
Runnable
()
{
public
void
run
()
{
try
{
// com.formdev.flatlaf.FlatDarkLaf.setup();
PrzelicznikWalutOkno
window
=
new
PrzelicznikWalutOkno
();
window
.
txtData
.
setText
(
LocalDate
.
now
().
toString
());
window
.
frmPrzelicznikWalut
.
setVisible
(
true
);
window
.
pobierzAktualneKursy
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
});
}
/**
* Create the application.
*/
public
PrzelicznikWalutOkno
()
{
initialize
();
}
/**
* Initialize the contents of the frame.
*/
private
void
initialize
()
{
frmPrzelicznikWalut
=
new
JFrame
();
frmPrzelicznikWalut
.
setTitle
(
"Przelicznik Walut"
);
frmPrzelicznikWalut
.
setBounds
(
100
,
100
,
480
,
701
);
frmPrzelicznikWalut
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
JLabel
lblWybierzWalut
=
new
JLabel
(
"Wybierz walutę"
);
lblWybierzWalut
.
setFont
(
FONT_LABEL
);
comboBox_Waluta
=
new
JComboBox
<>();
comboBox_Waluta
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
ev
)
{
wyswietlDaneWaluty
();
}
});
comboBox_Waluta
.
setModel
(
new
DefaultComboBoxModel
<>(
new
String
[]
{
"--"
}));
comboBox_Waluta
.
setFont
(
FONT_WARTOSC
);
JPanel
panel
=
new
JPanel
();
txtKwota
=
new
JTextField
();
txtKwota
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
ev
)
{
przelicz
();
}
});
txtKwota
.
setText
(
"100"
);
txtKwota
.
setColumns
(
10
);
txtKwota
.
setFont
(
FONT_TXT
);
JLabel
lblPodajKwot
=
new
JLabel
(
"Podaj kwotę"
);
lblPodajKwot
.
setFont
(
FONT_LABEL
);
rdbtnWalutaNaZlote
=
new
JRadioButton
(
"waluta na złote"
);
rdbtnWalutaNaZlote
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
ev
)
{
przelicz
();
}
});
rdbtnWalutaNaZlote
.
setFont
(
FONT_LABEL
);
buttonGroup
.
add
(
rdbtnWalutaNaZlote
);
rdbtnZloteNaWalute
=
new
JRadioButton
(
"złote na walute"
);
rdbtnZloteNaWalute
.
setFont
(
FONT_LABEL
);
rdbtnZloteNaWalute
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
ev
)
{
przelicz
();
}
});
buttonGroup
.
add
(
rdbtnZloteNaWalute
);
btnPrzelicz
=
new
JButton
(
"Przelicz"
);
btnPrzelicz
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
ev
)
{
przelicz
();
}
});
btnPrzelicz
.
setFont
(
FONT_BUTTON
);
JLabel
lblWynik
=
new
JLabel
(
"Wynik"
);
lblWynik
.
setFont
(
FONT_LABEL
);
lblPrawdziwyWynik
=
new
JLabel
(
"0.00"
);
lblPrawdziwyWynik
.
setFont
(
FONT_WYNIK
);
lblPrawdziwyWynik
.
setForeground
(
Color
.
GRAY
);
panel_1
=
new
JPanel
();
JLabel
lblPodajDat
=
new
JLabel
(
"Podaj datę"
);
lblPodajDat
.
setFont
(
FONT_LABEL
);
txtData
=
new
JTextField
();
txtData
.
setHorizontalAlignment
(
SwingConstants
.
CENTER
);
txtData
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
PLAIN
,
20
));
txtData
.
setText
(
"data"
);
txtData
.
setColumns
(
10
);
JButton
btnZmieDat
=
new
JButton
(
"Zmień datę"
);
btnZmieDat
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
e
)
{
pobierzKursyDlaDaty
();
}
});
btnZmieDat
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
18
));
btnPobierzAktualne
=
new
JButton
(
"Pobierz aktualne"
);
btnPobierzAktualne
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
e
)
{
pobierzAktualneKursy
();
}
});
btnPobierzAktualne
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
18
));
GroupLayout
groupLayout
=
new
GroupLayout
(
frmPrzelicznikWalut
.
getContentPane
());
groupLayout
.
setHorizontalGroup
(
groupLayout
.
createParallelGroup
(
Alignment
.
LEADING
)
.
addGroup
(
groupLayout
.
createSequentialGroup
()
.
addContainerGap
()
.
addGroup
(
groupLayout
.
createParallelGroup
(
Alignment
.
TRAILING
)
.
addComponent
(
panel_1
,
Alignment
.
LEADING
,
GroupLayout
.
DEFAULT_SIZE
,
448
,
Short
.
MAX_VALUE
)
.
addComponent
(
btnPrzelicz
,
Alignment
.
LEADING
,
GroupLayout
.
DEFAULT_SIZE
,
448
,
Short
.
MAX_VALUE
)
.
addGroup
(
Alignment
.
LEADING
,
groupLayout
.
createSequentialGroup
()
.
addComponent
(
rdbtnWalutaNaZlote
)
.
addPreferredGap
(
ComponentPlacement
.
RELATED
,
108
,
Short
.
MAX_VALUE
)
.
addComponent
(
rdbtnZloteNaWalute
))
.
addGroup
(
Alignment
.
LEADING
,
groupLayout
.
createSequentialGroup
()
.
addComponent
(
lblPodajKwot
)
.
addPreferredGap
(
ComponentPlacement
.
RELATED
,
139
,
Short
.
MAX_VALUE
)
.
addComponent
(
txtKwota
,
GroupLayout
.
PREFERRED_SIZE
,
189
,
GroupLayout
.
PREFERRED_SIZE
))
.
addComponent
(
panel
,
Alignment
.
LEADING
,
GroupLayout
.
DEFAULT_SIZE
,
448
,
Short
.
MAX_VALUE
)
.
addComponent
(
lblWynik
,
Alignment
.
LEADING
)
.
addComponent
(
lblPrawdziwyWynik
,
Alignment
.
LEADING
)
.
addGroup
(
Alignment
.
LEADING
,
groupLayout
.
createSequentialGroup
()
.
addGroup
(
groupLayout
.
createParallelGroup
(
Alignment
.
TRAILING
,
false
)
.
addComponent
(
lblPodajDat
,
Alignment
.
LEADING
,
GroupLayout
.
DEFAULT_SIZE
,
GroupLayout
.
DEFAULT_SIZE
,
Short
.
MAX_VALUE
)
.
addComponent
(
lblWybierzWalut
,
Alignment
.
LEADING
,
GroupLayout
.
DEFAULT_SIZE
,
223
,
Short
.
MAX_VALUE
))
.
addPreferredGap
(
ComponentPlacement
.
UNRELATED
)
.
addGroup
(
groupLayout
.
createParallelGroup
(
Alignment
.
LEADING
)
.
addComponent
(
txtData
,
GroupLayout
.
DEFAULT_SIZE
,
207
,
Short
.
MAX_VALUE
)
.
addComponent
(
comboBox_Waluta
,
0
,
207
,
Short
.
MAX_VALUE
)))
.
addGroup
(
groupLayout
.
createSequentialGroup
()
.
addComponent
(
btnPobierzAktualne
,
GroupLayout
.
PREFERRED_SIZE
,
207
,
GroupLayout
.
PREFERRED_SIZE
)
.
addGap
(
34
)
.
addComponent
(
btnZmieDat
,
GroupLayout
.
DEFAULT_SIZE
,
207
,
Short
.
MAX_VALUE
)))
.
addContainerGap
())
);
groupLayout
.
setVerticalGroup
(
groupLayout
.
createParallelGroup
(
Alignment
.
TRAILING
)
.
addGroup
(
groupLayout
.
createSequentialGroup
()
.
addContainerGap
()
.
addComponent
(
panel_1
,
GroupLayout
.
PREFERRED_SIZE
,
103
,
GroupLayout
.
PREFERRED_SIZE
)
.
addPreferredGap
(
ComponentPlacement
.
UNRELATED
)
.
addGroup
(
groupLayout
.
createParallelGroup
(
Alignment
.
LEADING
,
false
)
.
addComponent
(
txtData
,
GroupLayout
.
PREFERRED_SIZE
,
GroupLayout
.
DEFAULT_SIZE
,
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
lblPodajDat
,
GroupLayout
.
DEFAULT_SIZE
,
GroupLayout
.
DEFAULT_SIZE
,
Short
.
MAX_VALUE
))
.
addPreferredGap
(
ComponentPlacement
.
RELATED
)
.
addGroup
(
groupLayout
.
createParallelGroup
(
Alignment
.
LEADING
)
.
addComponent
(
btnZmieDat
)
.
addComponent
(
btnPobierzAktualne
,
GroupLayout
.
PREFERRED_SIZE
,
32
,
GroupLayout
.
PREFERRED_SIZE
))
.
addPreferredGap
(
ComponentPlacement
.
RELATED
,
17
,
Short
.
MAX_VALUE
)
.
addGroup
(
groupLayout
.
createParallelGroup
(
Alignment
.
BASELINE
)
.
addComponent
(
comboBox_Waluta
,
GroupLayout
.
PREFERRED_SIZE
,
GroupLayout
.
DEFAULT_SIZE
,
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
lblWybierzWalut
))
.
addPreferredGap
(
ComponentPlacement
.
UNRELATED
)
.
addComponent
(
panel
,
GroupLayout
.
PREFERRED_SIZE
,
142
,
GroupLayout
.
PREFERRED_SIZE
)
.
addPreferredGap
(
ComponentPlacement
.
UNRELATED
)
.
addGroup
(
groupLayout
.
createParallelGroup
(
Alignment
.
BASELINE
)
.
addComponent
(
txtKwota
,
GroupLayout
.
PREFERRED_SIZE
,
GroupLayout
.
DEFAULT_SIZE
,
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
lblPodajKwot
))
.
addPreferredGap
(
ComponentPlacement
.
UNRELATED
)
.
addGroup
(
groupLayout
.
createParallelGroup
(
Alignment
.
BASELINE
)
.
addComponent
(
rdbtnWalutaNaZlote
)
.
addComponent
(
rdbtnZloteNaWalute
))
.
addPreferredGap
(
ComponentPlacement
.
RELATED
)
.
addComponent
(
btnPrzelicz
)
.
addPreferredGap
(
ComponentPlacement
.
UNRELATED
)
.
addComponent
(
lblWynik
)
.
addPreferredGap
(
ComponentPlacement
.
UNRELATED
)
.
addComponent
(
lblPrawdziwyWynik
)
.
addGap
(
33
))
);
lblTabela
=
new
JLabel
(
"Tabela"
);
lblTabela
.
setFont
(
FONT_LABEL
);
lbl_NumerTabeli
=
new
JLabel
(
"?"
);
lbl_NumerTabeli
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
lblData
=
new
JLabel
(
"Data:"
);
lblData
.
setFont
(
FONT_LABEL
);
lbl_DataTabeli
=
new
JLabel
(
"?"
);
lbl_DataTabeli
.
setFont
(
new
Font
(
"Dialog"
,
Font
.
BOLD
,
24
));
GroupLayout
gl_panel_1
=
new
GroupLayout
(
panel_1
);
gl_panel_1
.
setHorizontalGroup
(
gl_panel_1
.
createParallelGroup
(
Alignment
.
LEADING
)
.
addGroup
(
gl_panel_1
.
createSequentialGroup
()
.
addContainerGap
()
.
addGroup
(
gl_panel_1
.
createParallelGroup
(
Alignment
.
LEADING
)
.
addGroup
(
gl_panel_1
.
createSequentialGroup
()
.
addComponent
(
lblTabela
,
GroupLayout
.
PREFERRED_SIZE
,
95
,
GroupLayout
.
PREFERRED_SIZE
)
.
addGap
(
18
)
.
addComponent
(
lbl_NumerTabeli
,
GroupLayout
.
PREFERRED_SIZE
,
311
,
GroupLayout
.
PREFERRED_SIZE
))
.
addGroup
(
gl_panel_1
.
createSequentialGroup
()
.
addComponent
(
lblData
,
GroupLayout
.
PREFERRED_SIZE
,
95
,
GroupLayout
.
PREFERRED_SIZE
)
.
addGap
(
18
)
.
addComponent
(
lbl_DataTabeli
,
GroupLayout
.
PREFERRED_SIZE
,
311
,
GroupLayout
.
PREFERRED_SIZE
)))
.
addContainerGap
(
GroupLayout
.
DEFAULT_SIZE
,
Short
.
MAX_VALUE
))
);
gl_panel_1
.
setVerticalGroup
(
gl_panel_1
.
createParallelGroup
(
Alignment
.
LEADING
)
.
addGroup
(
gl_panel_1
.
createSequentialGroup
()
.
addContainerGap
()
.
addGroup
(
gl_panel_1
.
createParallelGroup
(
Alignment
.
LEADING
)
.
addComponent
(
lblTabela
,
GroupLayout
.
PREFERRED_SIZE
,
29
,
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
lbl_NumerTabeli
,
GroupLayout
.
PREFERRED_SIZE
,
29
,
GroupLayout
.
PREFERRED_SIZE
))
.
addGap
(
12
)
.
addGroup
(
gl_panel_1
.
createParallelGroup
(
Alignment
.
LEADING
)
.
addComponent
(
lblData
,
GroupLayout
.
PREFERRED_SIZE
,
29
,
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
lbl_DataTabeli
,
GroupLayout
.
PREFERRED_SIZE
,
29
,
GroupLayout
.
PREFERRED_SIZE
))
.
addContainerGap
(
21
,
Short
.
MAX_VALUE
))
);
panel_1
.
setLayout
(
gl_panel_1
);
JLabel
lblKod
=
new
JLabel
(
"Kod:"
);
lblKod
.
setFont
(
FONT_LABEL
);
JLabel
lblNazwa
=
new
JLabel
(
"Nazwa:"
);
lblNazwa
.
setFont
(
FONT_LABEL
);
JLabel
lblKurs
=
new
JLabel
(
"Kurs:"
);
lblKurs
.
setFont
(
FONT_LABEL
);
lbl_KodWaluty
=
new
JLabel
(
"kod"
);
lbl_KodWaluty
.
setFont
(
FONT_WARTOSC
);
lbl_NazwaWaluty
=
new
JLabel
(
"nazwa"
);
lbl_NazwaWaluty
.
setFont
(
FONT_WARTOSC
);
lbl_KursWaluty
=
new
JLabel
(
"1.0000"
);
lbl_KursWaluty
.
setFont
(
FONT_WARTOSC
);
GroupLayout
gl_panel
=
new
GroupLayout
(
panel
);
gl_panel
.
setHorizontalGroup
(
gl_panel
.
createParallelGroup
(
Alignment
.
LEADING
)
.
addGroup
(
gl_panel
.
createSequentialGroup
()
.
addContainerGap
()
.
addGroup
(
gl_panel
.
createParallelGroup
(
Alignment
.
LEADING
,
false
)
.
addComponent
(
lblKurs
,
Alignment
.
TRAILING
,
GroupLayout
.
DEFAULT_SIZE
,
GroupLayout
.
DEFAULT_SIZE
,
Short
.
MAX_VALUE
)
.
addComponent
(
lblNazwa
,
Alignment
.
TRAILING
,
GroupLayout
.
DEFAULT_SIZE
,
GroupLayout
.
DEFAULT_SIZE
,
Short
.
MAX_VALUE
)
.
addComponent
(
lblKod
,
Alignment
.
TRAILING
,
GroupLayout
.
DEFAULT_SIZE
,
95
,
Short
.
MAX_VALUE
))
.
addPreferredGap
(
ComponentPlacement
.
RELATED
,
18
,
GroupLayout
.
PREFERRED_SIZE
)
.
addGroup
(
gl_panel
.
createParallelGroup
(
Alignment
.
LEADING
)
.
addComponent
(
lbl_KodWaluty
,
Alignment
.
TRAILING
,
GroupLayout
.
DEFAULT_SIZE
,
171
,
Short
.
MAX_VALUE
)
.
addComponent
(
lbl_NazwaWaluty
,
Alignment
.
TRAILING
,
GroupLayout
.
DEFAULT_SIZE
,
171
,
Short
.
MAX_VALUE
)
.
addComponent
(
lbl_KursWaluty
,
Alignment
.
TRAILING
,
GroupLayout
.
DEFAULT_SIZE
,
171
,
Short
.
MAX_VALUE
))
.
addContainerGap
())
);
gl_panel
.
setVerticalGroup
(
gl_panel
.
createParallelGroup
(
Alignment
.
LEADING
)
.
addGroup
(
gl_panel
.
createSequentialGroup
()
.
addContainerGap
()
.
addGroup
(
gl_panel
.
createParallelGroup
(
Alignment
.
BASELINE
)
.
addComponent
(
lblKod
)
.
addComponent
(
lbl_KodWaluty
))
.
addPreferredGap
(
ComponentPlacement
.
UNRELATED
)
.
addGroup
(
gl_panel
.
createParallelGroup
(
Alignment
.
BASELINE
)
.
addComponent
(
lblNazwa
)
.
addComponent
(
lbl_NazwaWaluty
))
.
addPreferredGap
(
ComponentPlacement
.
UNRELATED
)
.
addGroup
(
gl_panel
.
createParallelGroup
(
Alignment
.
BASELINE
)
.
addComponent
(
lblKurs
)
.
addComponent
(
lbl_KursWaluty
))
.
addContainerGap
(
61
,
Short
.
MAX_VALUE
))
);
panel
.
setLayout
(
gl_panel
);
frmPrzelicznikWalut
.
getContentPane
().
setLayout
(
groupLayout
);
}
protected
void
przelicz
()
{
try
{
BigDecimal
kwota
=
new
BigDecimal
(
txtKwota
.
getText
());
String
kod
=
""
+
comboBox_Waluta
.
getSelectedItem
();
Waluta
waluta
=
tabela
.
findByCode
(
kod
).
get
();
BigDecimal
wynik
=
null
;
if
(
rdbtnWalutaNaZlote
.
isSelected
())
{
wynik
=
waluta
.
przeliczNaZlote
(
kwota
);
}
if
(
rdbtnZloteNaWalute
.
isSelected
())
{
wynik
=
waluta
.
przeliczNaWalute
(
kwota
);
}
lblPrawdziwyWynik
.
setText
(
""
+
wynik
);
lblPrawdziwyWynik
.
setForeground
(
Color
.
BLUE
);
}
catch
(
Exception
e
)
{
lblPrawdziwyWynik
.
setText
(
"błąd"
);
lblPrawdziwyWynik
.
setForeground
(
Color
.
RED
);
}
}
protected
void
wyswietlDaneWaluty
()
{
String
kod
=
""
+
comboBox_Waluta
.
getSelectedItem
();
Optional
<
Waluta
>
waluta
=
tabela
.
findByCode
(
kod
);
if
(
waluta
.
isPresent
())
{
lbl_KodWaluty
.
setText
(
waluta
.
get
().
getCode
());
lbl_NazwaWaluty
.
setText
(
waluta
.
get
().
getCurrency
());
lbl_KursWaluty
.
setText
(
""
+
waluta
.
get
().
getMid
());
if
(!
txtKwota
.
getText
().
isEmpty
())
{
przelicz
();
}
return
;
}
lblPrawdziwyWynik
.
setText
(
"0.00"
);
lblPrawdziwyWynik
.
setForeground
(
Color
.
GRAY
);
}
private
void
pobierzAktualneKursy
()
{
// Pobieranie aktualnych kursów w tle (to znaczy w innym wątku)
// Aby wykonać operację w tle, w Swingu najlepiej użyć klasy SwingWorker
SwingWorker
<
TabelaWalut
,
Void
>
worker
=
new
SwingWorker
<>()
{
protected
TabelaWalut
doInBackground
()
{
try
{
tabela
=
pobieranie
.
pobierzBiezaceKursy
();
}
catch
(
BladAplikacji
e
)
{
tabela
=
new
TabelaWalut
(
"Brak danych"
,
LocalDate
.
now
());
e
.
printStackTrace
();
}
return
tabela
;
}
protected
void
done
()
{
// tu piszemy "co ma zrobić okno, gdy operacja jest zakończona"
// to będzie wykonane przez wątek EDT
odswiezDaneTabeli
();
wyswietlDaneWaluty
();
}
};
worker
.
execute
();
}
private
void
pobierzKursyDlaDaty
()
{
SwingWorker
<
TabelaWalut
,
Void
>
worker
=
new
SwingWorker
<>()
{
protected
TabelaWalut
doInBackground
()
{
String
data
=
txtData
.
getText
();
try
{
tabela
=
pobieranie
.
pobierzArchiwalneKursy
(
data
);
SwingUtilities
.
invokeLater
(()
->
{
txtData
.
setForeground
(
Color
.
BLACK
);
});
}
catch
(
BladAplikacji
e
)
{
tabela
=
new
TabelaWalut
(
"Brak danych"
,
LocalDate
.
now
());
e
.
printStackTrace
();
}
return
tabela
;
}
protected
void
done
()
{
odswiezDaneTabeli
();
wyswietlDaneWaluty
();
}
};
worker
.
execute
();
}
private
void
odswiezDaneTabeli
()
{
lbl_NumerTabeli
.
setText
(
tabela
.
getNo
());
lbl_DataTabeli
.
setText
(
""
+
tabela
.
getEffectiveDate
());
Object
wybranaWaluta
=
comboBox_Waluta
.
getSelectedItem
();
comboBox_Waluta
.
setModel
(
new
DefaultComboBoxModel
<>(
tabela
.
codes
()));
comboBox_Waluta
.
setSelectedItem
(
wybranaWaluta
);
}
}
PC28-RestKlient/src/main/java/waluty/TabelaWalut.java
0 → 100644
View file @
d419757d
package
waluty
;
import
java.time.LocalDate
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Optional
;
public
class
TabelaWalut
{
private
final
String
no
;
private
final
LocalDate
effectiveDate
;
private
final
List
<
Waluta
>
rates
=
new
ArrayList
<>();
public
TabelaWalut
(
String
no
,
LocalDate
effectiveDate
)
{
this
.
no
=
no
;
this
.
effectiveDate
=
effectiveDate
;
}
public
TabelaWalut
(
String
no
,
String
effectiveDate
)
{
this
(
no
,
LocalDate
.
parse
(
effectiveDate
));
}
public
void
addRate
(
Waluta
waluta
)
{
rates
.
add
(
waluta
);
}
public
String
getNo
()
{
return
no
;
}
public
LocalDate
getEffectiveDate
()
{
return
effectiveDate
;
}
public
List
<
Waluta
>
getRates
()
{
// dostęp tylko do odczytu
return
Collections
.
unmodifiableList
(
rates
);
}
@Override
public
String
toString
()
{
return
"Tabela nr "
+
no
+
" z dnia "
+
effectiveDate
+
", "
+
rates
.
size
()
+
" walut"
;
}
public
Optional
<
Waluta
>
findByCode
(
String
code
)
{
for
(
Waluta
waluta
:
rates
)
{
if
(
waluta
.
getCode
().
equals
(
code
)
)
{
return
Optional
.
of
(
waluta
);
}
}
return
Optional
.
empty
();
}
public
String
[]
codes
()
{
return
rates
.
stream
()
.
map
(
Waluta:
:
getCode
)
.
toArray
(
String
[]::
new
);
}
}
PC28-RestKlient/src/main/java/waluty/Waluta.java
0 → 100644
View file @
d419757d
package
waluty
;
import
java.math.BigDecimal
;
import
java.math.RoundingMode
;
public
class
Waluta
{
private
final
String
code
;
private
final
String
currency
;
private
final
BigDecimal
mid
;
public
Waluta
(
String
code
,
String
currency
,
BigDecimal
mid
)
{
this
.
code
=
code
;
this
.
currency
=
currency
;
this
.
mid
=
mid
;
}
public
String
getCode
()
{
return
code
;
}
public
String
getCurrency
()
{
return
currency
;
}
public
BigDecimal
getMid
()
{
return
mid
;
}
@Override
public
String
toString
()
{
return
code
+
" ("
+
currency
+
"): "
+
mid
;
}
public
BigDecimal
przeliczNaZlote
(
BigDecimal
kwota
)
{
return
kwota
.
multiply
(
mid
).
setScale
(
2
,
RoundingMode
.
HALF_EVEN
);
}
public
BigDecimal
przeliczNaWalute
(
BigDecimal
kwota
)
{
return
kwota
.
divide
(
mid
,
2
,
RoundingMode
.
HALF_EVEN
);
}
}
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