Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jvstd1
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
jvstd1
Commits
207198ca
Commit
207198ca
authored
Dec 10, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Obsługa walut w JAXB
parent
864e44ff
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
449 additions
and
0 deletions
+449
-0
pom.xml
pom.xml
+12
-0
LocalDateAdapter.java
src/main/java/p22_jaxb/konwersje/LocalDateAdapter.java
+18
-0
ArrayOfExchangeRatesTable.java
src/main/java/p22_jaxb/model/ArrayOfExchangeRatesTable.java
+54
-0
ExchangeRatesTable.java
src/main/java/p22_jaxb/model/ExchangeRatesTable.java
+124
-0
Rate.java
src/main/java/p22_jaxb/model/Rate.java
+100
-0
package-info.java
src/main/java/p22_jaxb/model/package-info.java
+6
-0
KursSredniJAXB.java
src/main/java/p22_jaxb/programy/KursSredniJAXB.java
+63
-0
OdczytajWaluty.java
src/main/java/p22_jaxb/programy/OdczytajWaluty.java
+39
-0
ZapiszPrzykladoweWaluty.java
src/main/java/p22_jaxb/programy/ZapiszPrzykladoweWaluty.java
+33
-0
No files found.
pom.xml
View file @
207198ca
...
...
@@ -27,6 +27,17 @@
<version>
5.11.3
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
jakarta.xml.bind
</groupId>
<artifactId>
jakarta.xml.bind-api
</artifactId>
<version>
4.0.2
</version>
</dependency>
<dependency>
<groupId>
org.glassfish.jaxb
</groupId>
<artifactId>
jaxb-runtime
</artifactId>
<version>
4.0.5
</version>
<scope>
runtime
</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
src/main/java/p22_jaxb/konwersje/LocalDateAdapter.java
0 → 100644
View file @
207198ca
package
p22_jaxb
.
konwersje
;
import
jakarta.xml.bind.annotation.adapters.XmlAdapter
;
import
java.time.LocalDate
;
public
class
LocalDateAdapter
extends
XmlAdapter
<
String
,
LocalDate
>
{
@Override
public
LocalDate
unmarshal
(
String
s
)
{
return
LocalDate
.
parse
(
s
);
}
@Override
public
String
marshal
(
LocalDate
d
)
{
return
d
.
toString
();
}
}
src/main/java/p22_jaxb/model/ArrayOfExchangeRatesTable.java
0 → 100644
View file @
207198ca
package
p22_jaxb
.
model
;
import
java.util.ArrayList
;
import
java.util.List
;
import
jakarta.xml.bind.annotation.XmlElement
;
import
jakarta.xml.bind.annotation.XmlRootElement
;
@XmlRootElement
(
name
=
"ArrayOfExchangeRatesTable"
)
public
class
ArrayOfExchangeRatesTable
{
@XmlElement
(
name
=
"ExchangeRatesTable"
)
private
List
<
ExchangeRatesTable
>
exchangeRatesTable
=
new
ArrayList
<>();
public
ArrayOfExchangeRatesTable
()
{
}
public
ArrayOfExchangeRatesTable
(
ExchangeRatesTable
exchangeRatesTable
)
{
this
.
exchangeRatesTable
.
add
(
exchangeRatesTable
);
}
public
List
<
ExchangeRatesTable
>
getExchangeRatesTable
()
{
return
exchangeRatesTable
;
}
@Override
public
String
toString
()
{
return
""
+
exchangeRatesTable
;
}
@Override
public
int
hashCode
()
{
final
int
prime
=
31
;
int
result
=
1
;
result
=
prime
*
result
+
((
exchangeRatesTable
==
null
)
?
0
:
exchangeRatesTable
.
hashCode
());
return
result
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
)
return
false
;
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
ArrayOfExchangeRatesTable
other
=
(
ArrayOfExchangeRatesTable
)
obj
;
if
(
exchangeRatesTable
==
null
)
{
if
(
other
.
exchangeRatesTable
!=
null
)
return
false
;
}
else
if
(!
exchangeRatesTable
.
equals
(
other
.
exchangeRatesTable
))
return
false
;
return
true
;
}
}
src/main/java/p22_jaxb/model/ExchangeRatesTable.java
0 → 100644
View file @
207198ca
package
p22_jaxb
.
model
;
import
java.time.LocalDate
;
import
java.util.*
;
import
jakarta.xml.bind.annotation.XmlElement
;
import
jakarta.xml.bind.annotation.XmlElementWrapper
;
import
jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter
;
import
p22_jaxb.konwersje.LocalDateAdapter
;
public
class
ExchangeRatesTable
{
@XmlElement
(
name
=
"Table"
)
private
final
String
table
;
@XmlElement
(
name
=
"No"
)
private
final
String
no
;
@XmlElement
(
name
=
"EffectiveDate"
)
@XmlJavaTypeAdapter
(
LocalDateAdapter
.
class
)
private
final
LocalDate
effectiveDate
;
@XmlElementWrapper
(
name
=
"Rates"
)
@XmlElement
(
name
=
"Rate"
)
private
final
List
<
Rate
>
rates
=
new
ArrayList
<>();
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
ExchangeRatesTable
(
String
table
,
String
no
,
LocalDate
effectiveDate
,
Collection
<
Rate
>
rates
)
{
this
(
table
,
no
,
effectiveDate
);
this
.
addRates
(
rates
);
}
public
String
getTable
()
{
return
table
;
}
public
String
getNo
()
{
return
no
;
}
public
LocalDate
getEffectiveDate
()
{
return
effectiveDate
;
}
public
Collection
<
Rate
>
getRates
()
{
return
Collections
.
unmodifiableList
(
rates
);
}
public
void
setRates
(
Collection
<
Rate
>
rates
)
{
rates
.
clear
();
addRates
(
rates
);
}
public
void
addRate
(
Rate
rate
)
{
this
.
rates
.
add
(
rate
);
}
public
void
addRates
(
Collection
<
Rate
>
rates
)
{
for
(
Rate
rate
:
rates
)
{
this
.
addRate
(
rate
);
}
}
@Override
public
int
hashCode
()
{
final
int
prime
=
31
;
int
result
=
1
;
result
=
prime
*
result
+
((
effectiveDate
==
null
)
?
0
:
effectiveDate
.
hashCode
());
result
=
prime
*
result
+
((
no
==
null
)
?
0
:
no
.
hashCode
());
result
=
prime
*
result
+
((
rates
==
null
)
?
0
:
rates
.
hashCode
());
result
=
prime
*
result
+
((
table
==
null
)
?
0
:
table
.
hashCode
());
return
result
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
)
return
false
;
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
ExchangeRatesTable
other
=
(
ExchangeRatesTable
)
obj
;
if
(
effectiveDate
==
null
)
{
if
(
other
.
effectiveDate
!=
null
)
return
false
;
}
else
if
(!
effectiveDate
.
equals
(
other
.
effectiveDate
))
return
false
;
if
(
no
==
null
)
{
if
(
other
.
no
!=
null
)
return
false
;
}
else
if
(!
no
.
equals
(
other
.
no
))
return
false
;
if
(
rates
==
null
)
{
if
(
other
.
rates
!=
null
)
return
false
;
}
else
if
(!
rates
.
equals
(
other
.
rates
))
return
false
;
if
(
table
==
null
)
{
if
(
other
.
table
!=
null
)
return
false
;
}
else
if
(!
table
.
equals
(
other
.
table
))
return
false
;
return
true
;
}
@Override
public
String
toString
()
{
return
"Tabela "
+
table
+
" nr "
+
no
+
" z dnia "
+
effectiveDate
+
" ("
+
rates
.
size
()
+
" walut)"
;
}
}
src/main/java/p22_jaxb/model/Rate.java
0 → 100644
View file @
207198ca
package
p22_jaxb
.
model
;
import
java.math.BigDecimal
;
import
java.math.RoundingMode
;
import
jakarta.xml.bind.annotation.XmlElement
;
/**
* @author patryk
*
* Obiekt tej klasy reprezentuje jeden wpis z tabeli kursów walutowych.
* Klasa immutable.
*/
public
class
Rate
{
@XmlElement
(
name
=
"Currency"
)
private
final
String
currency
;
@XmlElement
(
name
=
"Code"
)
private
final
String
code
;
@XmlElement
(
name
=
"Mid"
)
private
final
BigDecimal
mid
;
public
Rate
(
String
currency
,
String
code
,
BigDecimal
mid
)
{
this
.
currency
=
currency
;
this
.
code
=
code
;
this
.
mid
=
mid
;
}
public
Rate
()
{
// Dla zapewnienia zgodności z wzorcem JavaBean
currency
=
null
;
code
=
null
;
mid
=
null
;
}
public
String
getCurrency
()
{
return
currency
;
}
public
String
getCode
()
{
return
code
;
}
public
BigDecimal
getMid
()
{
return
mid
;
}
@Override
public
int
hashCode
()
{
final
int
prime
=
31
;
int
result
=
1
;
result
=
prime
*
result
+
((
code
==
null
)
?
0
:
code
.
hashCode
());
result
=
prime
*
result
+
((
currency
==
null
)
?
0
:
currency
.
hashCode
());
result
=
prime
*
result
+
((
mid
==
null
)
?
0
:
mid
.
hashCode
());
return
result
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
)
return
false
;
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
Rate
other
=
(
Rate
)
obj
;
if
(
code
==
null
)
{
if
(
other
.
code
!=
null
)
return
false
;
}
else
if
(!
code
.
equals
(
other
.
code
))
return
false
;
if
(
currency
==
null
)
{
if
(
other
.
currency
!=
null
)
return
false
;
}
else
if
(!
currency
.
equals
(
other
.
currency
))
return
false
;
if
(
mid
==
null
)
{
if
(
other
.
mid
!=
null
)
return
false
;
}
else
if
(!
mid
.
equals
(
other
.
mid
))
return
false
;
return
true
;
}
@Override
public
String
toString
()
{
return
code
+
" "
+
currency
+
" "
+
mid
;
}
public
BigDecimal
plnNaWalute
(
BigDecimal
kwota
)
{
return
kwota
.
divide
(
mid
,
2
,
RoundingMode
.
HALF_UP
);
}
public
BigDecimal
walutaNaPln
(
BigDecimal
kwota
)
{
return
kwota
.
multiply
(
mid
).
setScale
(
2
,
RoundingMode
.
HALF_UP
);
}
}
src/main/java/p22_jaxb/model/package-info.java
0 → 100644
View file @
207198ca
@XmlAccessorType
(
XmlAccessType
.
FIELD
)
package
p22_jaxb
.
model
;
import
jakarta.xml.bind.annotation.XmlAccessType
;
import
jakarta.xml.bind.annotation.XmlAccessorType
;
\ No newline at end of file
src/main/java/p22_jaxb/programy/KursSredniJAXB.java
0 → 100644
View file @
207198ca
package
p22_jaxb
.
programy
;
import
jakarta.xml.bind.JAXBContext
;
import
jakarta.xml.bind.JAXBException
;
import
jakarta.xml.bind.Unmarshaller
;
import
p22_jaxb.model.ArrayOfExchangeRatesTable
;
import
p22_jaxb.model.ExchangeRatesTable
;
import
p22_jaxb.model.Rate
;
import
java.io.File
;
public
class
KursSredniJAXB
{
public
static
void
main
(
String
[]
args
)
{
String
plik
=
"pliki/waluty2023.xml"
;
String
waluta
=
"EUR"
;
System
.
out
.
println
(
"Startujemy"
);
long
t1
=
System
.
nanoTime
();
try
{
JAXBContext
ctx
=
JAXBContext
.
newInstance
(
ArrayOfExchangeRatesTable
.
class
);
Unmarshaller
u
=
ctx
.
createUnmarshaller
();
ArrayOfExchangeRatesTable
array
=
(
ArrayOfExchangeRatesTable
)
u
.
unmarshal
(
new
File
(
"pliki/waluty2023.xml"
));
long
t2
=
System
.
nanoTime
();
double
min
=
Double
.
MAX_VALUE
,
max
=
0
,
sum
=
0
;
int
count
=
0
;
for
(
ExchangeRatesTable
table
:
array
.
getExchangeRatesTable
())
{
for
(
Rate
rate
:
table
.
getRates
())
{
if
(
rate
.
getCode
().
equals
(
waluta
))
{
count
++;
double
mid
=
rate
.
getMid
().
doubleValue
();
sum
+=
mid
;
if
(
mid
<
min
)
min
=
mid
;
if
(
mid
>
max
)
max
=
mid
;
break
;
}
}
}
long
t3
=
System
.
nanoTime
();
Runtime
runtime
=
Runtime
.
getRuntime
();
System
.
out
.
printf
(
"Czas wczytywania : %.6f s%n"
,
(
t2
-
t1
)
*
1
e
-
9
);
System
.
out
.
printf
(
"Czas liczenia : %.6f s%n"
,
(
t3
-
t2
)
*
1
e
-
9
);
System
.
out
.
printf
(
"Czas łączny : %.6f s%n"
,
(
t3
-
t1
)
*
1
e
-
9
);
System
.
out
.
printf
(
"Zajęta pamięć : %,d B%n"
,
runtime
.
totalMemory
()
-
runtime
.
freeMemory
());
System
.
gc
();
Thread
.
sleep
(
1000
);
System
.
out
.
printf
(
"Zajęta pamięć po gc: %,d B%n"
,
runtime
.
totalMemory
()
-
runtime
.
freeMemory
());
System
.
out
.
println
(
"Count: "
+
count
);
if
(
count
>
0
)
{
System
.
out
.
println
(
"Sum: "
+
sum
);
System
.
out
.
println
(
"Avg: "
+
(
sum
/
count
));
System
.
out
.
println
(
"Min: "
+
min
);
System
.
out
.
println
(
"Max: "
+
max
);
}
}
catch
(
JAXBException
|
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
src/main/java/p22_jaxb/programy/OdczytajWaluty.java
0 → 100644
View file @
207198ca
package
p22_jaxb
.
programy
;
import
jakarta.xml.bind.JAXBContext
;
import
jakarta.xml.bind.JAXBException
;
import
jakarta.xml.bind.Marshaller
;
import
jakarta.xml.bind.Unmarshaller
;
import
p22_jaxb.model.ArrayOfExchangeRatesTable
;
import
p22_jaxb.model.ExchangeRatesTable
;
import
p22_jaxb.model.Rate
;
import
java.io.File
;
import
java.math.BigDecimal
;
import
java.time.LocalDate
;
public
class
OdczytajWaluty
{
public
static
void
main
(
String
[]
args
)
{
try
{
JAXBContext
ctx
=
JAXBContext
.
newInstance
(
ArrayOfExchangeRatesTable
.
class
);
// Jeśli pakiet zawiera klasę ObjectFactory, to można też nazwę pakietu:
// JAXBContext ctx = JAXBContext.newInstance("p22_jaxb.model");
Unmarshaller
u
=
ctx
.
createUnmarshaller
();
ArrayOfExchangeRatesTable
array
=
(
ArrayOfExchangeRatesTable
)
u
.
unmarshal
(
new
File
(
"pliki/waluty2023.xml"
));
System
.
out
.
println
(
"Udało się wczytać, lista zawiera "
+
array
.
getExchangeRatesTable
().
size
()
+
" tabel."
);
String
kod
=
"EUR"
;
System
.
out
.
println
(
"Wypiszę kolejne kursy waluty "
+
kod
+
":"
);
for
(
ExchangeRatesTable
table
:
array
.
getExchangeRatesTable
())
{
System
.
out
.
print
(
table
.
getEffectiveDate
()
+
": "
);
table
.
getRates
().
stream
().
filter
(
rate
->
rate
.
getCode
().
equals
(
kod
)).
findFirst
().
ifPresent
(
rate
->
System
.
out
.
print
(
rate
.
getMid
()));
System
.
out
.
println
();
}
}
catch
(
JAXBException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
src/main/java/p22_jaxb/programy/ZapiszPrzykladoweWaluty.java
0 → 100644
View file @
207198ca
package
p22_jaxb
.
programy
;
import
jakarta.xml.bind.JAXBContext
;
import
jakarta.xml.bind.JAXBException
;
import
jakarta.xml.bind.Marshaller
;
import
p22_jaxb.model.ArrayOfExchangeRatesTable
;
import
p22_jaxb.model.ExchangeRatesTable
;
import
p22_jaxb.model.Rate
;
import
java.math.BigDecimal
;
import
java.time.LocalDate
;
public
class
ZapiszPrzykladoweWaluty
{
public
static
void
main
(
String
[]
args
)
{
ExchangeRatesTable
table
=
new
ExchangeRatesTable
(
"A"
,
"A/120/2024"
,
LocalDate
.
of
(
2024
,
5
,
25
));
table
.
addRate
(
new
Rate
(
"dolar"
,
"USD"
,
new
BigDecimal
(
"4.2000"
)));
table
.
addRate
(
new
Rate
(
"euro"
,
"EUR"
,
new
BigDecimal
(
"4.5000"
)));
ArrayOfExchangeRatesTable
array
=
new
ArrayOfExchangeRatesTable
(
table
);
System
.
out
.
println
(
array
);
System
.
out
.
println
();
try
{
JAXBContext
ctx
=
JAXBContext
.
newInstance
(
ArrayOfExchangeRatesTable
.
class
);
Marshaller
marshaller
=
ctx
.
createMarshaller
();
marshaller
.
setProperty
(
Marshaller
.
JAXB_FORMATTED_OUTPUT
,
Boolean
.
TRUE
);
marshaller
.
marshal
(
array
,
System
.
out
);
}
catch
(
JAXBException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
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