Currency rate analyzer (Java + JavaScript web application)

Currency rate analyzer (Java + JavaScript web application)
22/08/2019 Ernestas Konopliovas
Short description

This is a web application that uses Lietuvos Bankas webservice of currency rate publishing and shows all available rates list in selected period. Request are beeing sent automatically, when changes are apply. When currency rates are not available, app shows alert about it. Incoming data is in XML format.

Java 8
Spring Boot

HTML + JavaScript + jQuery

Interesting code pieces

Thanks to freshly found Lombok library now it is way less pain creating simple pojo classes. Annotation @Data generates getters/setters, toString  for me in a just one line of code. Annotation @AllArgsConstructor is used to show Lombok that I will need all arguments contructor here. AccessLevel = public. What can be easier, huh?

@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class Rate {

	   	@NonNull private String date;

	   	@NonNull private String currency;

	    private Double rate;

And here is the code I used to get and use data from . Data comes as XML and is read with DocumentBuilder class from javax.xml.parsers. For this moment, I believe json is easier and much more clear format. But sometimes you shall stick to requirements set by others 🙂

public ArrayList<Rate> getRates(String currency,  String lowDate, String highDate){
		  ArrayList<Rate> rates = null;
		  try {
	            String URL = ""
	            		+ "getFxRatesForCurrency"
	            		+ "?tp=EU"
	            		+ "&ccy=" + currency
	            		+ "&dtFrom=" + lowDate
	            		+ "&dtTo=" + highDate;

	            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	            DocumentBuilder builder = factory.newDocumentBuilder();
	            Document doc = builder.parse(URL);
	            NodeList nodeList = doc.getElementsByTagName("FxRate");
	            rates = new ArrayList<>();

	            for (int i = 0; i < nodeList.getLength(); i++) {

	                Node node = nodeList.item(i);

	                if(node.getNodeType() == Node.ELEMENT_NODE) {
	                    Element elem = (Element) node;
	                    Rate rate = new Rate(
	                            elem.getElementsByTagName("Dt").item(0).getTextContent().replace('.', '-'),

	        } catch (Exception ex) {
		  return rates;

And finally, a small pearl for sorting HashMap:


 Map<String, String> sortedCurrienciesMap = currienciesMap.entrySet()
				    (oldValue, newValue) -> oldValue, LinkedHashMap::new));

Here I am returning LinkeHashMap because simple HashMap makes no guarantees about the iteration order.


Comments (0)

Leave a reply