Commit 09b85c99 by Patryk Czarnik

Dodatkowe klasy do obsługi kursów walut

parent f19fdff9
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);
}
}
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment