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.

Back-end:
Java 8
Spring Boot
Thymeleaf
Maven
Lombok

Front-end:
HTML + JavaScript + jQuery

Demonstration
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?

@Data
@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 lb.lt . 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 = "https://www.lb.lt/webservices/FxRates/FxRates.asmx/"
	            		+ "getFxRatesForCurrency"
	            		+ "?tp=EU"
	            		+ "&ccy=" + currency
	            		+ "&dtFrom=" + lowDate
	            		+ "&dtTo=" + highDate;
	  

	            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	            DocumentBuilder builder = factory.newDocumentBuilder();
	            Document doc = builder.parse(URL);
	            doc.getDocumentElement().normalize();
	            
	            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('.', '-'),
	                            elem.getElementsByTagName("Ccy").item(1).getTextContent(),
	                            Double.parseDouble(elem.getElementsByTagName("Amt").item(1).getTextContent())
	                    );
	                    rates.add(rate);
	                }
	            }

	        } catch (Exception ex) {
	          System.out.println(ex.getMessage());
	        }
		  
		  return rates;
	  }

And finally, a small pearl for sorting HashMap:

 

 Map<String, String> sortedCurrienciesMap = currienciesMap.entrySet()
				  .stream()
				  .sorted(Map.Entry.comparingByValue())
				  .collect(Collectors.toMap(
				    Map.Entry::getKey, 
				    Map.Entry::getValue, 
				    (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

Your email address will not be published. Required fields are marked *

*