Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx_20230801
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
alx_20230801
Commits
c8362784
Commit
c8362784
authored
Aug 01, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Analiza 2
parent
ba693fc6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
0 deletions
+68
-0
AnalizaWalut2.java
ObslugaXML/src/main/java/przyklady_dom/AnalizaWalut2.java
+68
-0
No files found.
ObslugaXML/src/main/java/przyklady_dom/AnalizaWalut2.java
0 → 100644
View file @
c8362784
package
przyklady_dom
;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Element
;
import
org.w3c.dom.NodeList
;
import
org.xml.sax.SAXException
;
import
javax.swing.*
;
import
javax.swing.filechooser.FileNameExtensionFilter
;
import
javax.xml.parsers.DocumentBuilder
;
import
javax.xml.parsers.DocumentBuilderFactory
;
import
javax.xml.parsers.ParserConfigurationException
;
import
java.io.File
;
import
java.io.IOException
;
public
class
AnalizaWalut2
{
public
static
void
main
(
String
[]
args
)
{
JFileChooser
chooser
=
new
JFileChooser
(
"."
);
chooser
.
setFileFilter
(
new
FileNameExtensionFilter
(
"Pliki XML"
,
"xml"
));
chooser
.
setDialogTitle
(
"Wybierz plik z walutami"
);
int
wybor
=
chooser
.
showOpenDialog
(
null
);
if
(
wybor
!=
JFileChooser
.
APPROVE_OPTION
){
return
;
}
File
wybranyPlik
=
chooser
.
getSelectedFile
();
String
szukanyKod
=
JOptionPane
.
showInputDialog
(
"Podaj kod waluty"
,
"USD"
);
if
(
szukanyKod
==
null
)
{
return
;
}
DocumentBuilderFactory
factory
=
DocumentBuilderFactory
.
newInstance
();
try
{
DocumentBuilder
builder
=
factory
.
newDocumentBuilder
();
System
.
out
.
println
(
"Mam DocumentBuilder: "
+
builder
);
Document
doc
=
builder
.
parse
(
wybranyPlik
);
System
.
out
.
println
(
"Dokument wczytany: "
+
doc
);
Element
root
=
doc
.
getDocumentElement
();
System
.
out
.
println
(
"Nazwa elememtu głównego: "
+
root
.
getTagName
());
double
suma
=
0
;
int
ilosc
=
0
;
NodeList
rates
=
root
.
getElementsByTagName
(
"Rate"
);
final
int
nRates
=
rates
.
getLength
();
for
(
int
i
=
0
;
i
<
nRates
;
i
++)
{
Element
rate
=
(
Element
)
rates
.
item
(
i
);
// zakładamy, że każdy Rate na pewno posiada podelement Code; gdyby nie miał, to będzie wyjątek
Element
elementCode
=
(
Element
)
rate
.
getElementsByTagName
(
"Code"
).
item
(
0
);
String
code
=
elementCode
.
getTextContent
();
if
(
code
.
equalsIgnoreCase
(
szukanyKod
))
{
Element
elementMid
=
(
Element
)
rate
.
getElementsByTagName
(
"Mid"
).
item
(
0
);
double
mid
=
Double
.
parseDouble
(
elementMid
.
getTextContent
());
suma
+=
mid
;
ilosc
++;
}
}
double
srednia
=
suma
/
ilosc
;
JOptionPane
.
showMessageDialog
(
null
,
String
.
format
(
"Średni kurs waluty %s wynosi %.3f"
,
szukanyKod
,
srednia
));
}
catch
(
ParserConfigurationException
|
SAXException
|
IOException
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